for loop within custom function to create ggplot time series plots

前端 未结 2 501
一整个雨季
一整个雨季 2020-12-12 02:33

I\'m having a hard time trying to create a function with a for loop that takes the columns in a data frame and creates different plots based on the column name.. I have read

2条回答
  •  借酒劲吻你
    2020-12-12 02:49

    The following works.
    You were looping along all columns including column "DATE", which is supposed to be the x axis and the loop variable was integer so in the plot's aesthetic y = i was an integer, not a column name.

    Note that I call windows() to open a new graphic window. Remove this if not needed.

    Plot_Graph <- function(DF, na.rm = TRUE){
      nm = names(DF)[-1]
      for (i in nm) {
        g <- ggplot(DF, aes(x = DATE, y = get(i))) +
                geom_point()
        windows()
        print(g)
      }
    }
    
    Plot_Graph(philly_df_new)
    

提交回复
热议问题