rggplot2gridassign

Storing ggplots into sequentially named objects


I'm trying to loop through a series of columns (variable number of columns in each dataset), generate a time series for each and then store this plot as a ggplot object that I can then arrange into a grid. My code so far:

for (i in 2:ncol(df)){

  x <- df[,1]
  y[,i] <- df[,i]
  
  assign(paste("curve",i,sep=""), ggplot(data = y, aes(x = x, y = y[,i])) + 
    geom_point()  + geom_smooth(method = 'lm', formula = y~poly(x,3))

  }

This way, I explicitly name each ggplot a different name that I should be able to arrange into a grid. The problem is that only the most recent ggplot is stored, and all of my objects return exactly the same plot despite being generated from different data.

I had previously tried a solution that stored the ggplots into a list, with the same result.

Sample dataframe:

x   a   b   c   d
1   2   3   8   10
2   4   6   7   14
3   6   8   6   24
4   8   3   5   34
5   10  3   4   43
6   12  1   4   32
7   14  5   3   43
8   16  7   1   64
9   18  8   3   75
10  20  3   2   23

Solution

  • EDIT. This will work.

    for (i in 2:ncol(df)) {
      col_name <- colnames(df)[i]
      x <- df$x
      y <- df[, i]
    
      assign(
        paste("curve_", col_name, sep = ""),
        ggplot(data = data.frame(x = x, y = y), aes(x = x, y = y)) +
          geom_point() +
          geom_smooth(method = 'lm', formula = y ~ poly(x, 3)) +
          labs(title = paste("curve", col_name))
      )
    }
    
    print(curve_a)
    

    Simply store the individual plots in the list using the column names as keys. I am not entirely sure about the assign and paste approach you tried, in the following I attempted to recreate the output you are after cleaning the code a bit and performing operations one after the other.

    library(ggplot2)
    
    # sample dataframe
    df <- data.frame(
      x = 1:10,
      a = c(2, 4, 6, 8, 10, 12, 14, 16, 18, 20),
      b = c(3, 6, 8, 3, 3, 1, 5, 7, 8, 3),
      c = c(8, 7, 6, 5, 4, 4, 3, 1, 3, 2),
      d = c(10, 14, 24, 34, 43, 32, 43, 64, 75, 23)
    )
    
    # initialize an empty list to store ggplot objects
    plot_list <- list()
    
    # loop through columns and create ggplot objects
    for (i in 2:ncol(df)) {
      col_name <- colnames(df)[i]
      x <- df$x
      y <- df[, i]
      
      p <- ggplot(data = data.frame(x = x, y = y), aes(x = x, y = y)) +
        geom_point() +
        geom_smooth(method = 'lm', formula = y ~ poly(x, 3)) +
        labs(title = paste("curve", col_name))
        
      plot_list[[col_name]] <- p
    }
    
    # print the plots from the list
    print(plot_list)
    

    enter image description here