R assigning ggplot objects to list in loop

The answers so far are very close, but unsatisfactory in my opinion. The problem is the following – after your for loop: myplots[[1]]$mapping #* x -> 1:dfmsize #* y -> dfrm[, i] myplots[[1]]$plot_env #<environment: R_GlobalEnv> myplots[[2]]$mapping #* x -> 1:dfmsize #* y -> dfrm[, i] myplots[[2]]$plot_env #<environment: R_GlobalEnv> i #[1] “B” As the other answers … Read more

Cowplot made ggplot2 theme disappear / How to see current ggplot2 theme, and restore the default?

Note: this is longer an issue in current releases of cowplot, where the default theme is not changed. Original answer below: You can use theme_get() to see the current “default” theme. You can use theme_set() to change the “default” theme. Theme settings do not carry over sessions. Usually, your default will be theme_grey, but cowplot … Read more

ggplot – Multiple legends arrangement

The idea is to create each plot individually (color, fill & size) then extract their legends and combine them in a desired way together with the main plot. See more about the cowplot package here & the patchwork package here library(ggplot2) library(cowplot) # get_legend() & plot_grid() functions library(patchwork) # blank plot: plot_spacer() data <- seq(1000, … Read more

Merge and Perfectly Align Histogram and Boxplot using ggplot2

You can use either egg, cowplot or patchwork packages to combine those two plots. See also this answer for more complex examples. library(dplyr) library(ggplot2) plt1 <- my_df %>% select(value) %>% ggplot(aes(x=””, y = value)) + geom_boxplot(fill = “lightblue”, color = “black”) + coord_flip() + theme_classic() + xlab(“”) + theme(axis.text.y=element_blank(), axis.ticks.y=element_blank()) plt2 <- my_df %>% select(id, … Read more

Multiple plots in for loop ignoring par

Here is one way to do it with cowplot::plot_grid. The plot_duo function uses tidyeval approach in ggplot2 v3.0.0 # install.packages(“ggplot2”, dependencies = TRUE) library(rlang) library(dplyr) library(ggplot2) library(cowplot) plot_duo <- function(df, plot_var_x, plot_var_y) { if (is.character(plot_var_x)) { print(‘character column names supplied, use ensym()’) plot_var_x <- ensym(plot_var_x) } else { print(‘bare column names supplied, use enquo()’) plot_var_x … Read more