Loop(ggplot2)列表中的多个图 - 列表元素被覆盖 [英] List for Multiple Plots from Loop (ggplot2) - List elements being overwritten

查看:161
本文介绍了Loop(ggplot2)列表中的多个图 - 列表元素被覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



目标:一个循环,允许我绘制多个地图,显示密度数据(D )用于网格单元,跨越多个月和季节。每月,季节等的数据分为8个独立的栏目;循环将遍历数据框(DF)的列



试过:将循环的每次迭代中的图添加到列表中,以便可以调用所有图

  out<  -  NULL 
for(i in 1:8){

< -dfplot()+ geom_polygon(data = DF,aes(x = long,y = lat,group = Name,fill = D),color =lightgrey)+ labs(title = x)

out [[i]]< - p

print(p)

}



<问题:即使print(p)为每次迭代生成正确的图,列表out中的图仅显示最终循环中的数据。所以,当我尝试使用grid.arrange和out中的图时,所有图都显示了相同的数据(从第8列开始)。然而,情节确实保留了正确的标题。当我尝试调用每个情节 - 例如,print(out [[1]])时,显示与print(out [[8]])不同的情节 - 标题标签除外。



似乎列表中的前一个元素正在被每个循环覆盖?然而,图的标题似乎正确显示。



我在构建out列表时是否存在明显的错误?我怎样才能避免每个以前的情节覆盖?非常感谢!

解决方案

问题不在于每个项目都被重写,问题在于ggplot()等待直到您打印出图解决aes()命令中的变量。循环将所有fill =参数分配给D,并且D的值改变每个循环。但是,在循环结束时,D将只有最后一个值。因此,列表中的每个图将使用相同的颜色进行打印。



这也会再现相同的问题

 require(ggplot2)

#sample data
dd <-data.frame(x = 1:10,y = runif(10),g1 =样本(字母[1:2],10,替换= T),g2 =样本(字母[3:4],10,替换= T))

plot< -list()
g <-dd $ g1
plots [[1]] <-ggplot(data = dd,aes(x = x,y = y,color = g))+ geom_point()

g <-dd $ g2
plots [[2]] <-ggplot(data = dd,aes(x = x,y = y,color = g))+ geom_point()

#both将使用相同的组进行打印。
print(plots [[1]])
print(plots [[2]])

通过使用aes_string(),解决此问题的方法之一就是(@baptiste也提到)。这会解决变量的价值更快所以这应该工作

 图< -list()
g< ; - g1
plot [[1]] <-ggplot(data = dd,aes(x = x,y = y))+ geom_point(aes_string(color = g))
g < ; - g2
plots [[2]] <-ggplot(data = dd,aes(x = x,y = y))+ geom_point(aes_string(color = g))

#不同的分组(根据需要)
打印(图[[1]])
打印(图[[2]])

这与aes()函数最直接相关,因此您设置标题的方式非常好,这就是您看到不同标题的原因。


(Very much a novice, so please excuse any confusion/obvious mistakes)

Goal: A loop that allows me to plot multiple maps, displaying density data (D) for grid cells, across multiple months and seasons. The data for each month, season, etc., are in 8 separate columns; the loop would run through the columns of the data frame (DF)

Tried: Adding the plot from each iteration of the loop to a list so all plots can be called up to be displayed in a multipanel figure.

out <- NULL
 for(i in 1:8){

  D <- DF[,i]
  x <- names(DF)[i]

  p <-ggplot() + geom_polygon(data=DF, aes(x=long, y=lat, group=Name, fill=D), colour    = "lightgrey") +labs(title=x)

out[[i]]<- p

print(p)

}

Problem: Even though the "print(p)" yields the correct plot for each iteration, the plots in the list "out" display the data from the final loop only.

So, when I try to use grid.arrange with plots in "out", all plots show the same data (from the 8th column); however, the plots do retain the correct title. When I try to call up each plot - e.g., print(out[[1]]), shows the same plot - except for the title label - as print(out[[8]]).

It seems that the previous elements in the list are being overwritten with each loop? However, the title of the plots seem to display correctly.

Is there something obviously wrong with how I'm constructing the "out" list? How can I avoid having each previous plot overwritten? Many thanks in advance!

解决方案

The problem isn't that each item is over written, the problem is that ggplot() waits until you print the plot to resolve the variables in the aes() command. The loop is assigning all fill= parameters to D. And the value of D changes each loop. However, at the end of the loop, D will only have the last value. Thus each of the plots in the list will print with the same coloring.

This also reproduces the same problem

require(ggplot2)

#sample data
dd<-data.frame(x=1:10, y=runif(10), g1=sample(letters[1:2], 10, replace=T), g2=sample(letters[3:4], 10, replace=T))

plots<-list()
g<-dd$g1
plots[[1]]<-ggplot(data=dd, aes(x=x, y=y, color=g)) + geom_point()

g<-dd$g2
plots[[2]]<-ggplot(data=dd, aes(x=x, y=y, color=g)) + geom_point()

#both will print with the same groups.
print(plots[[1]])
print(plots[[2]])

One way around this as ( @baptiste also mentioned ) is by using aes_string(). This resolves the value of the variable "more quickly" So this should work

plots<-list()
g<-"g1"
plots[[1]]<-ggplot(data=dd, aes(x=x, y=y)) + geom_point(aes_string(color=g))
g<-"g2"
plots[[2]]<-ggplot(data=dd, aes(x=x, y=y)) + geom_point(aes_string(color=g))

#different groupings (as desired)
print(plots[[1]])
print(plots[[2]])

This is most directly related to the aes() function, so the way you are setting the title is just fine which is why you see different titles.

这篇关于Loop(ggplot2)列表中的多个图 - 列表元素被覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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