Skip to content

Instantly share code, notes, and snippets.

@keithmcnulty
Created September 1, 2020 10:49
Show Gist options
  • Save keithmcnulty/57330545df23169a02a554544cbdff0f to your computer and use it in GitHub Desktop.
Save keithmcnulty/57330545df23169a02a554544cbdff0f to your computer and use it in GitHub Desktop.
# generic function for generating a simple scatter plot in ggplot2
scatter_fn <- function(df, col1, col2, title) {
df %>%
ggplot2::ggplot(aes(x = {{col1}}, y = {{col2}})) +
ggplot2::geom_point() +
ggplot2::geom_smooth() +
ggplot2::labs(title = title)
}
# run function across species and store plots in a list column
penguin_scatters <- penguins %>%
dplyr::nest_by(species) %>%
dplyr::mutate(plot = list(scatter_fn(data, bill_length_mm, bill_depth_mm, species)))
penguin_scatters
# A tibble: 3 x 3
# Rowwise: species
species data plot
<fct> <list<tbl_df[,7]>> <list>
1 Adelie [152 × 7] <gg>
2 Chinstrap [68 × 7] <gg>
3 Gentoo [124 × 7] <gg>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment