0

When arranging multiple plots from a plotlist with cowplot, the stat_compare_means values get cut off in the grid (see picture). When I am removing the plots titles it will still be cut off. Is there a way to fix this? Please find reproducible code below:

library(tidyverse)
library(ggplot2)
library(ggpubr)
library(cowplot)


plotlist = list()
u=3
for (i in 0:3) {
  single_plot <- iris %>%
    ggplot(aes(x = Species, y = Sepal.Width , group=Species)) + #create a plot, specify x,y, parameters
    geom_point(aes(shape = Species)) + # create a 
    stat_summary(fun = mean, # calculate SEM, specify type and width of the resulting bars
                 fun.min = function(x) mean(x) - sd(x)/sqrt(length(x)), #calculate ymin SEM
                 fun.max = function(x) mean(x) + sd(x)/sqrt(length(x)), #calculate ymax SEM
                 geom = 'errorbar',  width = 0.2) + #specify type of stat_summary and its size
    stat_summary(fun = mean, fun.min = mean, fun.max = mean, #calculate mean, specify type, width and size (fatten) of the resulting bars
                 geom = 'errorbar',  width = 0.4, size=1.2) +#specify type of stat_summary and its size
    labs(x = "x_label", y = "y_label") +#set the x- and y-axis labels
    ggtitle("plot_title")+#set the plot title
    theme_classic() +#adjust the basic size of the plot
    theme(
      legend.position = "none", #do not use a plot legend
    )+
    stat_compare_means(method="kruskal.test")
  plotlist <- append(plotlist, list(single_plot))
  i=i+1
}


plot_grid(plotlist = plotlist,
          labels = "AUTO"
)

enter image description here

immuen
  • 43
  • 4

3 Answers3

0

increase the y axis scale and expand the limits

For this particular plot, I found this combination of parameters to work well.

+ scale_y_continuous(limits = c(2, 5), expand = c(0, 0.3))

enter image description here

Rich Pauloo
  • 7,734
  • 4
  • 37
  • 69
  • Thank you for you answer Rich. Unfortunately, the given example does not really fit to my real data. It had to be something that automatically solves the problem when creating plots in a for loop and then storing them in a list before arranging them with cowplot. But you are right, for the code that I provided it works just fine. :) – immuen May 13 '22 at 10:05
0

I now figured out the solution. The y-axis has to be scaled using scale_y_continious with a relative expansion using expand(expand = expansion(mult = c(x_expasion, y_expansion). See reproducible code below.

library(tidyverse)
library(ggplot2)
library(ggpubr)
library(cowplot)


plotlist = list()
u=3
for (i in 0:3) {
  single_plot <- iris %>%
    ggplot(aes(x = Species, y = Sepal.Width , group=Species)) + #create a plot, specify x,y, parameters
    geom_point(aes(shape = Species)) + # create a 
    stat_summary(fun = mean, # calculate SEM, specify type and width of the resulting bars
                 fun.min = function(x) mean(x) - sd(x)/sqrt(length(x)), #calculate ymin SEM
                 fun.max = function(x) mean(x) + sd(x)/sqrt(length(x)), #calculate ymax SEM
                 geom = 'errorbar',  width = 0.2) + #specify type of stat_summary and its size
    stat_summary(fun = mean, fun.min = mean, fun.max = mean, #calculate mean, specify type, width and size (fatten) of the resulting bars
                 geom = 'errorbar',  width = 0.4, size=1.2) +#specify type of stat_summary and its size
    scale_y_continuous(expand = expansion(mult = c(0,.1)))  +#create continouos y scale and set relative expansion 
    labs(x = "x_label", y = "y_label") +#set the x- and y-axis labels
    ggtitle("plot_title")+#set the plot title
    theme_classic() +#adjust the basic size of the plot
    theme(
      legend.position = "none", #do not use a plot legend
    )+
    stat_compare_means(method="kruskal.test")
  plotlist <- append(plotlist, list(single_plot))
  i=i+1
}


print(plot_grid(plotlist = plotlist,
          labels = "AUTO"
))

enter image description here

immuen
  • 43
  • 4
0

the following method can be used to avoid the cutoff (already supported in the release version of ggplot2)

+ coord_cartesian(clip = "off")

before: enter image description here

after: enter image description here

reference here

lanyusea
  • 143
  • 2
  • 14