Home > front end >  Multiple ggplots in one page using for Loop in R
Multiple ggplots in one page using for Loop in R

Time:11-23

I am trying to plot multiple "ggplot" in the same page using for loop. The for loop is used because the number of the plots is non-determined "dynamic". The x-axis of the plots will be changed within each iteration. I read so many articles about creating an empty list then add each plot in specific index of the list, then use "multiplot" function to display all the plots of the list in one page. However, This not working !! The problems is the program ends-up by printing only the last plot information saved at the last index of the list with different labels! the code and the figure below indicates the idea. `

Howmany <- readline(prompt="Specify the number of the independent variables: ") 
Howmany <- as.numeric(Howmany)
plot_lst <- vector("list", length = Howmany) #' an empty list
   for ( i in 1:Howmany){
        plot_lst[[i]] <- ggplot(data=data, aes(x=data[, c(i)], y=data$gender))   
        geom_point(aes(size = 5))      
           scale_color_discrete(name = "dependent_variable"   labs(
            title = (paste("Logistic Regression Fitting Model",i)), 
            x = names(data)[i],
            y =  "gender")
        
   }
 multiplot(plotlist = plot_lst, cols = 1)

The output of the code is provided here

I really appreciate any suggestion.

I also tried what were suggested in this link: show multiple plots from ggplot on one page in r

However, still am facing the same problem.

CodePudding user response:

Because ggplot's aes() is using lazy evaluation you need to force evaluation in each iteration of the loop (otherwise all plots will be the same on the last position of i).

One way to do this is by wrapping the righthand side of the assignment in local() and use i <- i:

The labs(x = ...) seemed not to be correct so I rewrote it as: x = names(data)[i], please check if that works for you.

plot_lst <- vector("list", length = Howmany) #' an empty list
for (i in 1:Howmany) {
  
  plot_lst[[i]] <- local({
    i <- i 
    
    ggplot(data=data, aes(x=data[, c(i)], y=data$gender))  
    geom_point(aes(size = 5))      
    scale_color_discrete(name = "dependent_variable")  
      labs(
        title = (paste("Logistic Regression Fitting Model", i)), 
        x = names(data)[i],
        y = "gender")
    })
}

Below is one example using the iris data set. If we print plot_lst we can see three different plots.

I assume the function multiplot is from the scatter package, which is not working with the latest R version, so I can't reproduce if this is working correctly.

Howmany <- readline(prompt="Specify the number of the independent variables: ") 
Howmany <- as.numeric(Howmany)
plot_lst <- vector("list", length = Howmany) #' an empty list

for ( i in 1:Howmany){
  
  plot_lst[[i]] <- local({ 
    i <- i
    
    ggplot(data  = iris,
           aes(x = iris[, c(i)],
               y = iris$Species))  
    geom_point(aes(size = 5))      
    scale_color_discrete(name = "dependent_variable")  
    labs(
      title = paste("Logistic Regression Fitting Model", i), 
      x = names(data)[i],
      y =  "species"
    )
    
  })
}

plot_lst
  • Related