[英]Plot multiple graphs on one plot in R within loop
我想在R中的for循環的一個圖中繪制一些曲線,並將其另存為png。 當我使用par(new = False)執行此操作時,軸會變為粗體,因為它是針對所有曲線繪制的,因此我會關閉除第一個圖以外的所有坐標軸,但這似乎是一個非常微妙的解決方案。
這樣做更像R的方式是什么? 到目前為止,這是我的所有代碼:
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])
要么,
matplot(1:100, t(y[,1:100]), t="l", lty=1, ylim=range(c(10,0)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.