强制ggplot来评估计数器变量 [英] force ggplot to evaluate counter variable
问题描述
我可以想到的最简单的例子是重现这一点,就是如何/何时变量
在ggplot构造中被评估。
跟随(应该将点1到10放置在一个图上):
$ $ p $ df = data。 (x = 1:10,y = 1:10)
panel = ggplot()+ xlim(-1,11)+ ylim(-1,11)
:$ {
panel = panel + geom_point(aes(x = df $ x [i],y = df $ y [i]))
}
print(panel)
这会产生一个点,即 i = 10
如果我给出 i 另一个值(范围从1到10)并重复
打印(面板)命令,那么该特定点将如果我在 i < - c(1:10)后加上打印(面板)那么所有的十点都会被绘制出来,就像我发布了矢量化版本一样:
ggplot(aes(x = X,Y = x),data = df)+ geom_point()
在我看来, i 仅在 print(panel)命令发布时进行评估。
我在一个非常复杂的情节中遇到了<
所以,她的问题是:
我循环遍历列表中的元素,而向量化版本不实用。有没有办法强制ggplot评估循环中每一步的 i ?
aes()
具体可以防止发生。如果你想要推测,你可以使用标准评估版本 aes _()
panel = ggplot()+ xlim(-1,11)+ ylim(-1,11)
for(i in c(1:10)){
panel = panel + geom_point( aes_(x = df $ x [i],y = df $ y [i]))
}
print(panel)
I ran into an interesting problem regarding how/when variables are evaluated in ggplot constructs.
The simplest example I can think of to reproduce this is the following (which is supposed to place the points 1 to 10 on a plot):
df=data.frame(x=1:10,y=1:10)
panel=ggplot() + xlim(-1,11) + ylim(-1,11)
for (i in c(1:10)) {
panel=panel+geom_point(aes(x=df$x[i],y=df$y[i]))
}
print(panel)
This will generate a plot with one point, i.e. the one for i=10 If I give i another value (in the range 1 to 10) and repeat the print(panel) command then that particular point will be plotted.
And if I do i <- c(1:10) followed by print(panel) then all the ten points will be plotted, just as if I had issued the vectorized version:
ggplot(aes(x=x,y=x),data=df)+geom_point()
It seems to me that here i is only evaluated when the print(panel) command is issued.
I ran into this in a very complicated plot where i was looping through the elements of a list, and a vectorized version is not practical.
So, the question her is: Is there a way to force ggplot to evaluate i for each step in the loop?
The aes()
specifically prevents evulation. If you want evaulation, you can use the standard-evaluation version aes_()
panel=ggplot() + xlim(-1,11) + ylim(-1,11)
for (i in c(1:10)) {
panel=panel+geom_point(aes_(x=df$x[i],y=df$y[i]))
}
print(panel)
这篇关于强制ggplot来评估计数器变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!