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

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

问题描述

我试图在ggplot中创建具有相同结构但具有不同Y值的多个散点图。我需要它们是分开的(因此不使用facet_wrap),因为在后面的步骤中,我使用grid_arrange将图形的不同组合排列到单个布局上。因此,我需要为每个绘图创建新的名称,以反映绘制的y值。下面是示例代码,其中month是x轴上的变量,我想要三个独立的月份图与另外三个变量(lag1_var,lag3_var和lag9_var)。

<数据帧(月= c(1,2,3,4,5,6,7,8,9,10,11,12),$ b(code> df < $ b 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值不同的值列表, :

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

for(val in loop.list){

yval < - paste0(lag,val,_var)

ptitle< -paste0( (plot,val),ggplot(data = df,aes(x = month,y = get(yval)))

+ geom_point(color =r ed,size = 2)+ ggtitle(ptitle))

}

当我这样做的时候,我得到了三个不同的名字(plot1,plot3,plot9)和正确的标题(所以plot1有标题graph plot lag1,plot3有标题graph plot lag3等等),但他们都是相同的情节。因此,循环正在为情节名称和情节标题工作,但不是为y值。它只是输出最后一个循环的值(对于变量lag9_var)。



我不知道为什么会发生这种情况,为什么只发生在Y值而不是标题或情节名称。我一直使用SAS进行编程,而且对R来说是新的,所以我认为我正在从SAS的角度来解决这个问题,而不是用R的方式来思考它。注意:在上面的代码中,我在ggplot语句之外创建了对象yval和ptitle,但仅用于帮助排除故障。如果我将它们包含在ggplot语句中,就会发生同样的情况:

  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(plot plot lag,val,_Var)))

}

感谢您的帮助!

解决方案

我想你遇到的问题可能是ggplot试图重建每个图表,当你打电话来显示它,并从最后的参考文献中检索数据,而不是每个绘图创建时的参考。我不完全了解它,所以如果有人能说明这个问题,这将是很好的。



无论哪种方式,按照这个推理,我试着分离每个数据绘制成它自己的数据框,似乎已经得到它的工作:

$ p $ library(data.table)
library (ggplot2)
loop.list< -c(1,3,9)
for(val in loop.list){
col< - grep( pastel(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 (数据= get(frameval),aes(x =月,y = var))+
geom_point(color (plot,val),plotval)
}
$ / code >

注意 grep 调用找到了co我们不能解释为什么ggplot不适用于这个情节,然后把这个列从其余的数据框中分离出来。


$你已经使用过的方法,但这似乎是一个解决方法,所以我希望它可以帮助。


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.

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

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

    }

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

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.

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

        }

Thank you for any help!

解决方案

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.

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

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