This isn't strange behavior, this is exactly how aes()
is supposed to work. It delays evaluation of the parameters until the plotting actually runs. This is problematic if you include expressions to variable outside your data.frame (like i
) and functions (like [,]
). These are only evaulated when you actually "draw" the plot.
If you want to force evaulation of your parameters, you can use aes_
. This will work for you
for (i in 1:nrow(b)) {
a <- a + geom_segment(arrow=arrow(),
mapping = aes_(x=b[i,1],y=b[i,2],xend=b[i,3],yend=b[i,4]))
}
plot(a)
Now within the loop the parameters for x=
and y=
, etc are evaluated in the environment and their value are "frozen" in the layer.
Of course, it would be better not to build layers in loops and just procide a proper data object as pointed out in @eipi10's answer.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…