1

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()
0

1 Answer 1

2
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)))
Sign up to request clarification or add additional context in comments.

1 Comment

actually, this is sufficient: matplot(t(y), type = "l", ylim=range(c(10,0)), add = FALSE)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.