0

There is a loop generate the multiple plots. My question is I want the title name change for each iteration. for example "plot1","plot2","plot3".........

what if I want a sentence for the plot title, say"This is the 1 plot we have","This is the 2 plot we have"

library(tidyverse)

# create a list with a specific length 
plot_lst <- vector("list", length = 8)

for (i in 1:8) {
  g <- ggplot(data = mtcars, aes(x = hp, y = wt)) +
    geom_point()+ggtitle("plot1")
  plot_lst[[i]] <- g
}

# Combine all plots
cowplot::plot_grid(plotlist = plot_lst, nrow = 4)

AI Kidds
  • 29
  • 6

1 Answers1

1

Try this:

library(tidyverse)

# create a list with a specific length 
plot_lst <- vector("list", length = 8)

for (i in 1:8) {
  g <- ggplot(data = mtcars, aes(x = hp, y = wt)) +
    geom_point()+ggtitle(paste0("plot",i))
  plot_lst[[i]] <- g
}

# Combine all plots
cowplot::plot_grid(plotlist = plot_lst, nrow = 4)

Output:

enter image description here

Duck
  • 39,058
  • 13
  • 42
  • 84