使用循环在 ggplot 中使用不同的 Y 轴值创建多个图 [英] Creating multiple plots in ggplot with different Y-axis values using a loop

查看:36
本文介绍了使用循环在 ggplot 中使用不同的 Y 轴值创建多个图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 ggplot 中创建多个散点图,它们具有相同的结构但具有不同的 Y 值.我需要将它们分开(因此不使用 facet_wrap),因为在后面的步骤中,我使用 grid_arrange 将不同的图形组合排列到单个布局上.

I am trying to create multiple scatter plot graphs in ggplot that have the same structure but with a different Y-value. I need them to be separate (and therefore not use facet_wrap) because in a later step I use grid_arrange to arrange different combinations of the graphs onto a single layout.

因此,我需要为每个绘图创建新名称以反映正在绘制的 y 值.下面是示例代码,其中月份是 x 轴上的变量,我想要三个单独的月份图与三个附加变量(lag1_var、lag3_var 和 lag9_var).

Because of this, I need to create new names for each plot that reflect the y-value being plotted. Below is sample code, where month is the variable on the x-axis and I want three separate plots of month vs. the three additional variables (lag1_var, lag3_var and lag9_var).

df <- data.frame (month= c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), 
                lag1_var=  c (10, 20, 30, 40, 10, 40, 30, 50, 70, 90, 100, 100),
                lag3_var= c(90, 70, 50, 40, 70, 50, 20, 50, 70, 90, 10, 10),
                lag9_var = c(50, 20,90, 100, 90, 10, 40, 90, 100, 20, 30, 70))

我的方法是创建一个包含 y 值之间不同的值的列表,然后循环遍历该列表,如下所示:

My approach was to create a list of the values that differ between the y-values and loop over that list like below:

loop.list <- c("1", "3", "9")

for (val in loop.list) {

  yval<- paste0("lag", val, "_var")

  ptitle <-paste0("graph plot lag", val, "_Var")

  assign(paste0("plot", val), ggplot(data=df, aes(x=month, y=get(yval))) 

+geom_point(color="red", size=2) + ggtitle(ptitle))

    }

当我这样做时,我得到三个具有三个不同名称(plot1、plot3、plot9)和正确标题的图(所以图 1 的标题为graph plot lag1",图 3 的标题为graph plot lag3"等),但它们都是相同的图.所以循环适用于绘图名称和绘图标题,但不适用于 y 值.它只输出最后一个循环的值(对于变量 lag9_var).

when I do this, I get three plots with three different names (plot1, plot3, plot9) and the correct titles (so plot 1 has the title "graph plot lag1" and plot 3 has the title "graph plot lag3", etc.), but they are all identical plots. So the loop is working for the plot name and for the plot title, but not for the y-value. It just outputs the values from the last loop (for the variable lag9_var).

我无法弄清楚为什么会发生这种情况,以及为什么它只发生在 Y 值而不是标题或情节名称上.我一直在 SAS 中编程并且是 R 的新手,所以我认为我是从 SAS 的角度来解决这个问题,而不是以R"的方式来考虑它.

I cannot figure out why this is happening, and why it only happens to the Y-value and not the title or plot name. I have always programmed in SAS and am new to R, so I think I am approaching this from a SAS prospective instead of thinking about it in the "R" way.

注意:在上面的代码中,我在 ggplot 语句之外创建了对象yval"和ptitle",但只是为了帮助排除故障.如果我将它们包含在 ggplot 语句中,则会发生同样的事情,如下所示:

Note: in the code above I create the objects "yval" and "ptitle" outside of the ggplot statement, but only to help troubleshoot. the same thing happens if I include them in ggplot statement like below:

 for (val in loop.list) {

      assign(paste0("plot", val), ggplot(data=df,aes(x=month,y=get(paste0("lag", val, "_var")))) + 

    geom_point(color="red", size=2) + 

    ggtitle(paste0("graph plot lag", val, "_Var")))

        }

感谢您的帮助!

推荐答案

我认为您遇到的问题可能是 ggplot 在您调用以显示每个图时试图重建它,并且它从给定的最后一个参考中检索数据,而不是创建每个图时给出的参考.我不完全理解它,所以如果其他人可以阐明该主题,那就太好了.

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 )
}

请注意 grep 调用正在查找用于该图的列号,然后将该列与其余列分开作为其自己的数据框.

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.

我无法解释为什么 ggplot 不适用于您使用的方法,但这似乎是一种解决方法,所以我希望它有所帮助.

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.

这篇关于使用循环在 ggplot 中使用不同的 Y 轴值创建多个图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆