简体   繁体   English

在循环内的R中的一个图上绘制多个图

[英]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. 我想在R中的for循环的一个图中绘制一些曲线,并将其另存为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. 当我使用par(new = False)执行此操作时,轴会变为粗体,因为它是针对所有曲线绘制的,因此我会关闭除第一个图以外的所有坐标轴,但这似乎是一个非常微妙的解决方案。

What would be a more R-like way to do so? 这样做更像R的方式是什么? 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)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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