简体   繁体   中英

Plot multiple graphs on one plot in R within loop

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM