Skip to content

Instantly share code, notes, and snippets.

@brunaw
Created June 29, 2022 16:58
Show Gist options
  • Save brunaw/eb01253b0834be77d97fff55e3d4632a to your computer and use it in GitHub Desktop.
Save brunaw/eb01253b0834be77d97fff55e3d4632a to your computer and use it in GitHub Desktop.
library(tidyverse)
library(patchwork)
iris_species <- unique(iris$Species)
colours <- c("pink", "red", "blue")
plots <- purrr::map2(
iris_species, colours,
~{
p <- iris |>
dplyr::filter(Species == .x) |>
ggplot(aes(x = Sepal.Width, y = Sepal.Length)) +
geom_point(colour = .y) +
ggtitle(paste0(.x))
p
})
# create a layout "automatically"
plot_code <- paste(
paste(paste0("plots[[", 1:length(plots), "]] +"), collapse = ""),
" plot_layout(ncol = 3)")
eval(str2lang(plot_code))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment