ggarrange result - all graphs are identical
2
1
Entering edit mode
3.3 years ago
emi ▴ 80

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"))) +
    theme_classic()+ 
    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 • 4.5k views
ADD COMMENT
1
Entering edit mode
3.3 years ago
emi ▴ 80

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:

https://stackoverflow.com/questions/46417470/saving-ggplot-in-a-list-gives-me-the-same-graph

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.

ADD COMMENT
0
Entering edit mode
3.3 years ago
zx8754 12k

Use lapply instead:

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

ggpubr::ggarrange(plotlist = g)
ADD COMMENT

Login before adding your answer.

Traffic: 2794 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

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

Powered by the version 2.3.6