Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable geom_text is overwritten when plots saved in list [duplicate]

Tags:

r

ggplot2

ggpubr

I am trying to organize several dozens of plots using ggarrange, so I have setup a loop in which I save each plot in a list. Each plot differs from each other with different data, title, etc. Everything works perfectly until I try to use geom_text to place some text inside the plot. When the plots are saved in the list, each plot inherits the geom_text from the last plot in the list. I don't know how to avoid this.

my.list=vector("list", length = 2);
dt=data.table(x=c(1,100,100000),y=c(1,100,100000))
plotname=c('first','second')

for (i in 1:length(my.list)) {
 my.list[[i]]=ggplot(data = dt, aes(x = x, y = y ))  + geom_point(size=1.5,aes(color=c('red'))) + labs(x=NULL, y=NULL) 
+ scale_color_manual(values='red') 
+ theme_bw() + theme(panel.background = element_rect(fill='light grey', colour='black'),legend.position = "none") 
+ geom_text(inherit.aes=FALSE,aes(x=500, y=100000, label=paste0('NRMSE:',i))) + ggtitle(paste0(plotname[i])) + coord_equal() 
+ geom_abline(slope=1) 
+ scale_y_log10(breaks = c(1,10,100,1000,10000,100000),limits=c(1,100000)) 
+ scale_x_log10(breaks = c(1,10,100,1000,10000,1000000),limits=c(1,100000)) 
+ labs(x=NULL, y=NULL) 
+ theme_bw() + theme(panel.background = element_rect(fill='light grey', colour='black'),legend.position = "none")
}

after this I do

plotosave=ggarrange(plotlist=my.list)

enter image description here

like image 722
Herman Toothrot Avatar asked Nov 22 '25 07:11

Herman Toothrot


1 Answers

Using lapply instead of forloop works fine:

my.list <- lapply(1:2, function(i) {
  ggplot(data = dt, aes(x = x, y = y ))  + 
    geom_point(size=1.5) + 
    labs(x=NULL, y=NULL) + 
    theme_bw() + 
    theme(panel.background = element_rect(fill='light grey', colour='black'),
          legend.position = "none") + 
    geom_text(inherit.aes=FALSE,aes(x=50000, y=100000, 
                                    label=paste0('NRMSE:',i))) + 
    ggtitle(paste0(plotname[i]))
  })

ggarrange(plotlist = my.list)

Note: the issue is not with ggarrange.


Roland:

The plot is build when you print the ggplot object. Anything that is not part of the data passed will be taken from the enclosing environment at exactly that time point. If you use the iterator of a for loop in the plot, it has its last value then (or any value you change it to later on). lapply avoids the issue because of the stuff explained in the Note in its documentation.


Related post:

the problem is that ggplot() waits until you print the plot to resolve the variables in the aes() command.

like image 112
zx8754 Avatar answered Nov 24 '25 21:11

zx8754



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!