Paul Würtz - 7 months ago 66

R Question

I want to plot some curves in one plot in a for loop in R and save it as a png.

When I do this with par(new=False) the axis get bold, because it is rendered for every of the curves, so I turn off the axis for every but the first plot, but this seems like a very inelegant solution.

What would be a more R-like way to do so?

Here is all of my code until now:

`x<-matrix(rnorm(20000,5,3), nrow=200, ncol=100)`

y<-matrix(0, nrow=200, ncol=100)

for (i in 1:200) {

for (j in 1:100) {

y[i,j] <- mean(x[i,1:j])

}

}

png(filename="./a1.png")

#here is the ugly bit

plot(1:100,y[1,1:100],type="l", ylim=range(c(10,0)))

par(new = TRUE)

for (j in 2:200) {

plot(1:100,y[j,1:100],type="l", ylim=range(c(10,0)), xaxt='n', yaxt='n', ann=FALSE)

par(new = TRUE)

}

graphics.off()

Answer

```
plot(1:100, y[1,1:100], type="l", ylim=range(c(10,0)))
for (j in 2:200) lines(1:100, y[j,1:100])
```

or,

```
matplot(1:100, t(y[,1:100]), t="l", lty=1, ylim=range(c(10,0)))
```

Source (Stackoverflow)