Skip to content

Instantly share code, notes, and snippets.

@jackbrookes
Created March 8, 2018 22:22
Show Gist options
  • Save jackbrookes/3bbcc47cdd246b3d8a9173a4b9bacf43 to your computer and use it in GitHub Desktop.
Save jackbrookes/3bbcc47cdd246b3d8a9173a4b9bacf43 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(gridExtra)
plot_cars_var <- function(col_name){
ggplot(mtcars, aes_string(x = "mpg", y = col_name)) +
geom_smooth() +
geom_point()
}
mtcars %>%
dplyr::select(-mpg, everything()) %>%
colnames() %>%
purrr::map(plot_cars_var) %>%
grid.arrange(grobs = ., ncol = 1)
@dhbrand
Copy link

dhbrand commented Mar 8, 2018

Thanks for the extra example. I ran 2 ways and the gather function ended up being quicker:

> tic()
> df1 %>% 
+     dplyr::select(-Week, everything()) %>% 
+     colnames() %>% 
+     purrr::map(plot_cars_var) %>% 
+     grid.arrange(grobs = ., ncol = 5)

> toc()
11.551 sec elapsed
> tic()
> ggplot(gather(df1, key, value, -Week), aes(Week, value)) + 
+     geom_smooth() + geom_point() +
+     facet_wrap(~ key, scales="free_y", ncol=5)
> toc()
4.039 sec elapsed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment