Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot using purrr map() to plot same x with multiple y's

I want to create multiple plots that have the same x but different y's using purrr package methodology. That is, I would like to use the map() or walk() functions to perform this.

Using mtcars dataset for simplicity.

ggplot(data = mtcars, aes(x = hp, y = mpg)) + geom_point()
ggplot(data = mtcars, aes(x = hp, y = cyl)) + geom_point()
ggplot(data = mtcars, aes(x = hp, y = disp)) + geom_point()

edit So far I have tried

y <- list("mpg", "cyl", "disp")
mtcars %>% map(y, ggplot(., aes(hp, y)) + geom_point()
like image 399
tictacjoe Avatar asked Dec 24 '22 16:12

tictacjoe


1 Answers

This is one possibility

ys <- c("mpg","cyl","disp")
ys %>% map(function(y) 
    ggplot(mtcars, aes(hp)) + geom_point(aes_string(y=y)))

It's just like any other map function, you just need to configure your aesthetics properly in the function.

like image 115
MrFlick Avatar answered Dec 28 '22 07:12

MrFlick