Creating multiple plots in ggplot with different Y-axis values using a loop

不想你离开。 提交于 2019-12-04 16:38:00

I think the problem you're having might be ggplot trying to rebuild each plot when you call to show it, and it retrieving the data from the last reference given, rather than the reference given when each plot was created. I don't fully understand it, so it would be great if someone else can illuminate that subject.

Either way, following that reasoning, I tried separating the data for each plot into its own data frame, and seem to have gotten it working:

library(data.table)
library(ggplot2)
loop.list <- c("1", "3", "9")
for (val in loop.list) {
    col <- grep( paste0("lag", val, "_var"), colnames(df) )
    yval <- df[,c(1,col)]
    setnames( yval, c( "month", "var" ) )
    frameval <- paste0("frame", val)
    assign( paste0("frame", val), yval )
    ptitle <-paste0("graph plot lag", val, "_Var")

    plotval <- ggplot( data = get(frameval), aes(x=month,y=var) ) +
           geom_point( color="red", size=2) +
               ggtitle(ptitle)
    assign( paste0("plot",val), plotval )
}

Notice the grep call is finding the column number to use for that plot, then separating that column out from the rest as its own data frame.

I can't explain why ggplot doesn't work with the method you've used, but this seems to be a workaround, so I hope it helps.

The code above works with one change I used names(yval)<-c("month", "var") instead of setNames. For some reason setNames wasn't working so the ggplot statement had no y-value to plot because the variable name in each frame was still lag3_var, lag6_var and lag9_var. Thank you!!!

library(data.table)
library(ggplot2)
loop.list <- c("1", "3", "9")
for (val in loop.list) {
    col <- grep( paste0("lag", val, "_var"), colnames(df) )
    yval <- df[,c(1,col)]
    **names(yval)<-  c( "month", "var")** 
    frameval <- paste0("frame", val)
    assign( paste0("frame", val), yval )
    ptitle <-paste0("graph plot lag", val, "_Var")

    plotval <- ggplot( data = get(frameval), aes(x=month,y=var) ) +
           geom_point( color="red", size=2) +
               ggtitle(ptitle)
    assign( paste0("plot",val), plotval )
}

The code below shows how to do that using the 'multiplot()' function, the source of which is provided here: http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2):

plotAllCounts <- function (dt){   
  plots <- list();
  for(i in 1:ncol(dt)) {
    strX = names(dt)[i]
    print(sprintf("%i: strX = %s", i, strX))
    plots[[i]] <- ggplot(dt) + xlab(strX) +
      geom_point(aes_string(strX),stat="count")
  }

  columnsToPlot <- floor(sqrt(ncol(dt)))
  multiplot(plotlist = plots, cols = columnsToPlot)
}

Now run the function - to get Counts for all variables printed using ggplot on one page:

dt = ggplot2::diamonds
plotAllCounts(dt)

This is one of the first steps I always do when analyzing a new data-set. Hope you'll find it useful.

One things to note is that: using aes(get(strX)), which you would normally use in loops when working with ggplot , in the above code instead of aes_string(strX) will NOT draw the desired plots. Instead, it will plot the last plot many times. I have not figured out why - it may have to do the aes and aes_string are called in ggplot.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!