Ggplot does not work if it is inside a for loop, although it works outside of it too

I use the simple ggplot function, which works great outside the loop, but not inside, even if the iterative value does not interfere with the ggplot function. Why is this so?

Here is my code

x=1:7 y=1:7 df = data.frame(x=x,y=y) ggplot(df,aes(x,y))+geom_point() 

It works! But if ggplot is inside the for ... loop

 for (i in 1:5) { ggplot(df,aes(x,y))+geom_point() } 

it no longer works, what am I missing?

+101
for-loop r ggplot2
Mar 28 '13 at 9:29
source share
1 answer

When in a for loop you must explicitly print create your ggplot object:

 for (i in 1:5) { print(ggplot(df,aes(x,y))+geom_point()) } 
+192
Mar 28 '13 at 9:46
source share



All Articles