Skip to content

Instantly share code, notes, and snippets.

@keithmcnulty
Last active July 17, 2020 05:49
Show Gist options
  • Select an option

  • Save keithmcnulty/4643fcb9930acecf0c5c6f2933e61b58 to your computer and use it in GitHub Desktop.

Select an option

Save keithmcnulty/4643fcb9930acecf0c5c6f2933e61b58 to your computer and use it in GitHub Desktop.
library(patchwork)
scatter_plot <- function(df, xvar, yvar) {
ggplot(df) +
geom_point(aes(x = {{xvar}}, y = {{yvar}}))
}
box_plot <- function(df, xvar, yvar, groupvar) {
ggplot(df) +
geom_boxplot(aes(x = {{xvar}}, y = {{yvar}}, group = {{groupvar}}))
}
plots <- mtcars %>%
dplyr::nest_by(cyl) %>%
dplyr::mutate(
plotcol = list(scatter = scatter_plot(data, wt, mpg) + labs(title = paste("cyl:", cyl))),
boxcol = list(box = box_plot(data, gear, disp, gear))
)
plots
#> # A tibble: 3 x 4
#> # Rowwise: cyl
#> cyl data plotcol boxcol
#> <dbl> <list<tbl_df[,10]>> <named list> <named list>
#> 1 4 [11 × 10] <gg> <gg>
#> 2 6 [7 × 10] <gg> <gg>
#> 3 8 [14 × 10] <gg> <gg>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment