ggarrange result - all graphs are identical
Entering edit mode
7 weeks ago
emi ▴ 40

I am wondering if anyone else had experienced the same problem as I can't find any post about it and don't know how to resolve it.

I am plotting 8 graphs and wanted to create a plot grid using ggarrange with the same legend.

below is my code:

#gg3 is the data frame containing all my data. I created a for loop for all my plots

splot <- list()
for(i in 1:8){

  splot[[i]] <- ggplot(gg3, aes(x=D50, y=gg3[ ,i],colour=group)) +
    geom_point(aes(shape=group, color=group)) + 
    scale_shape_manual(values=c(16, 16),labels=c(paste("\u03B1"),paste("\u03B2"))) +
    scale_color_manual(values=c("firebrick1","dodgerblue1"),labels=c(paste("\u03B1"),paste("\u03B2"))) +
    geom_smooth(method=lm, se=F)+ labs(y=colnames(gg3)[i])

ggarrange(plotlist = splot,common.legend = T)

below is the ggarrange result. I managed to combine all of them, and each graph has different y-axis label (so they must have been different graphs) but the actual graphs are identical in the ggarrange object. I've checked my dataframe (gg3) and the values are all different.

enter image description here

Is this a bug in ggarrange?

p/s: I've tried grid.arrange too and same results

Thank you.

ggarrange ggplot2 R grid.arrange • 265 views
Entering edit mode
7 weeks ago
emi ▴ 40

Hi all,

I did a bit more searching and managed to answered my own question. Will leave this here as it may help someone else.

I found this Q&A which suggests that storing plot in a list using a for loop can lead to a similar problem:

So I've tried their suggestion and used grid.arrange instead which worked for me. What I still can't figure out is this: I used the same code to save my list of plots into PDF and it worked. So I still don't understand why it couldn't work using ggarrange.

Entering edit mode
7 weeks ago
zx8754 10k

Use lapply instead:

g <- lapply(split(mtcars, mtcars$cyl), function(i){
  ggplot(i, aes(mpg, wt)) + geom_point()

ggpubr::ggarrange(plotlist = g)

Login before adding your answer.

Traffic: 2473 users visited in the last hour
Help About
Access RSS

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6