Home > Blockchain >  ggpubr::arrange() doesn't work if number of plots in a list are unknown
ggpubr::arrange() doesn't work if number of plots in a list are unknown

Time:12-31

I' like to create the plots using the arrange function of ggpubr package in a loop with two levels. First, I need a plot for each places, and inside each plot, I need to create a plot for each dates. I have tried to use the arrange function, but:

# Packages
library(ggplot2)
library(ggpubr)

# My artificial data
places <-c("A","A","A","B","B","B","B","A","A","A","B","B","B","B","A","A","A","B","B","B","B","A","A","A","B","B","B","B")
dates <-c("2019-01-01","2019-02-01","2019-03-01","2019-01-01","2019-02-01","2019-03-01","2019-04-01",
"2019-01-01","2019-02-01","2019-03-01","2019-01-01","2019-02-01","2019-03-01","2019-04-01",
"2019-01-01","2019-02-01","2019-03-01","2019-01-01","2019-02-01","2019-03-01","2019-04-01",
"2019-01-01","2019-02-01","2019-03-01","2019-01-01","2019-02-01","2019-03-01","2019-04-01")
areas <-rnorm(56,mean=100,sd=10)
my.ds <- data.frame(places,dates,areas)

# Plot evolution of the attack areas by stand in each date

all_areas <- unique(my.ds$places)

for (a in 1:length(all_areas)) {

Figure1 <- list()

my.ds.sub<-my.ds[my.ds$places==all_areas[a],]

for (d in 1:length(unique(my.ds.sub$dates))) {
    my.ds.sub.date<-my.ds.sub[my.ds.sub$date==my.ds.sub$date[d],]
    Figure1[[d]] <- ggplot()  
        geom_point(data=my.ds.sub.date, 
                 aes(x=dates, y=areas)) 
        theme_bw()  
}

ggarrange(Figure1[[1]]:Figure1[[length(Figure1)]]   rremove("x.text"),
          font.label = list(size = 18, color = "black", face = "plain", family = "serif"),
          widths = c(rep(15,10)),
          heights = c(rep(15,10)),
          ncol = ceiling(sqrt(length(Figure1))), nrow = ceiling(sqrt(length(Figure1))),
          common.legend = TRUE, 
          legend="bottom")
ggsave(paste0(all_stands[g],".svg"), dpi=600, width = 15, height = 15, units = "in") 
}

Error in Figure1[[1]]:Figure1[[length(Figure1)]] : NA/NaN argument
In addition: Warning messages:
1: In Figure1[[1]]:Figure1[[length(Figure1)]] :
  numerical expression has 9 elements: only the first used
2: In Figure1[[1]]:Figure1[[length(Figure1)]] :
  numerical expression has 9 elements: only the first used

I'don find any way to solve this problem a cause of ggarrange don't accept a list of plots (eg. Figure1[[1]]:Figure1[[length(Figure1)]]) (I need to automated that because a have not regular number of dates by places) in a : syntax.

Please, could you help me to solve this problem?

CodePudding user response:

For this use case ggpubr::ggarrange offers a plotlist argument which could be used to pass a list plots. Also note that I refactored your code to use lapply instead of for loops:

library(ggplot2)
library(ggpubr)

set.seed(123)

all_areas <- unique(my.ds$places)

lapply(all_areas, function(a) {
  my.ds.sub <- my.ds[my.ds$places == a, ]

  dates <- unique(my.ds.sub$dates)

  Figure1 <- lapply(dates, function(d) {
    my.ds.sub.date <- my.ds.sub[my.ds.sub$date == d, ]

    ggplot()  
      geom_point(data = my.ds.sub.date, aes(x = dates, y = areas))  
      theme_bw()  
      rremove("x.text")
  })

  ggarrange(
    plotlist = Figure1,
    font.label = list(size = 18, color = "black", face = "plain", family = "serif"),
    widths = c(rep(15, 10)),
    heights = c(rep(15, 10)),
    ncol = ceiling(sqrt(length(Figure1))), nrow = ceiling(sqrt(length(Figure1))),
    common.legend = TRUE,
    legend = "bottom"
  )
})
#> [[1]]

#> 
#> [[2]]

  • Related