循环中的多个绘图列表

循环中的多个绘图列表

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

问题描述

(非常新手,所以请原谅任何混淆/明显错误)

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

目标:一个循环,允许我绘制多个地图,显示网格单元格的密度数据 (D),跨越多个月份和季节.每个月、每个季节等的数据都在 8 个单独的列中;循环将遍历数据框 (DF) 的列

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)

}

问题:即使 print(p) 为每次迭代生成正确的绘图,列表 out 中的绘图仅显示来自最终循环的数据.

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.

因此,当我尝试将 grid.arrange 与out"中的图一起使用时,所有图都显示相同的数据(来自第 8 列);然而,情节确实保留了正确的标题.当我尝试调用每个图时 - 例如,print(out[[1]]),显示相同的图 - 除了标题标签 - 作为 print(out[[8]]).

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.

我构建 out 列表的方式有什么明显错误吗?如何避免之前的每个图都被覆盖?

Is there something obviously wrong with how I'm constructing the out list? How can I avoid having each previous plot overwritten?

推荐答案

问题不在于每一项都被覆盖了,问题在于 ggplot() 一直等到你将绘图打印到解析 aes() 命令中的变量.循环将所有 fill= 参数分配给 D.并且 D 的值改变每个循环.然而,在循环结束时,D 将只有最后一个值.因此,列表中的每个图都将以相同的颜色打印.

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.

这也重现了同样的问题

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

解决此问题的一种方法(@baptiste 也提到)是使用 aes_string().这可以更快地"解析变量的值所以这应该有效

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

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

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.

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

08-13 02:32