0

I am trying to save several plots with this loop:

set.seed(123)
x <- rnorm(5,1,0.5)
y <- rnorm(5,3,0.5)
df <- data.frame(x,y)

for(i in 1:5){
+ mypath <- file.path("C:","R",paste("plot", i, ".jpg", sep = ""))
+ jpeg(file=mypath)
+ ggplot(df[1:i,], aes(x=x, y=y) ) + geom_point() + coord_cartesian(xlim = c(0,2), ylim = c(0,4))
+ dev.off()
+ }

What I would like is to add a new line of data frame to each new plot. So, plot1.jpg shows only the first line of data frame, plot2.jpg shows lines 1 and 2, and so on.

The loop works, and make files, but are blank. What is wrong with this code?

Thanks in advance.

camille
  • 16,432
  • 18
  • 38
  • 60
giltrapo
  • 83
  • 7

0 Answers0