我有几个多点要创建,其中它们之间的差异只是多个时隙的四条曲线中3条的x和y值。 所以一个多重时段看起来像这样:
png(file=paste("~/Documents/plot1.png", sep=""), width=800, height=800 )
plot(xplot1, yplot1, xlab="x1", ylab="y1", type="p", pch=20, cex=0.5, col="yellow", main = "Plot1")
legend("topleft", c("Original curve","Cut curve", "Shifted curve" ), pch=20, cex=0.7, col=c("yellow", "black", "green"))
points(xcutplot1, ycutplot1, type="p", pch=20, cex=0.5, col="black")
points(xsampleplot, ysampleplot, type="p", pch=20, cex=0.05, col="red")
lm.r = lm(ysampleplot ~ xsampleplot)
abline(lm.r)
points(xcutplot1 + shiftX1, ycutplot1 + shiftY1, col="green", type = "p", pch = 20, cex = 0.5)
dev.off()
完美无缺。我现在的目标是创建一个函数,在其中放置多个图的结构,然后根据每个的x和y值调用函数。所以像这样:
getPlot = function(xplot, yplot, xcutplot, ycutplot, shiftX, shiftY){
plot(xplot, yplot, xlab="x", ylab="y", type="p", pch=20, cex=0.5, col="yellow", main = "Plot1")
legend("topleft", c("Original curve","Cut curve", "Shifted curve" ), pch=20, cex=0.7, col=c("yellow", "black", "green"))
points(xcutplot, ycutplot, type="p", pch=20, cex=0.5, col="black")
points(xsampleplot, ysampleplot, type="p", pch=20, cex=0.05, col="red")
lm.r = lm(ysampleplot ~ xsampleplot)
abline(lm.r)
points(xcutplot + shiftX, ycutplot + shiftY, col="green", type = "p", pch = 20, cex = 0.5)
}
然后将每个多时隙的函数调用为:
png(file=paste("~/Documents/plot1.png", sep=""), width=800, height=800 )
getPlot(xplot1, yplot1, xcutplot1, ycutplot1, shiftX1, shiftY1)
dev.off()
png(file=paste("~/Documents/plot2.png", sep=""), width=800, height=800 )
getPlot(xplot2, yplot2, xcutplot2, ycutplot2, shiftX2, shiftY2)
dev.off()
......等等。
你看到我的问题是放在return()中的问题,因为我不知道如何将图形放在一种“变量”中,然后将其他曲线添加到这个“变量”... < / p>
如果我没有把任何东西放回来(),它会返回一个只有第一条曲线的情节。
我在互联网上找不到任何解决方案,这就是为什么我在这里提出问题以获得更多想法......
先谢谢你的帮助。
答案 0 :(得分:1)
这是完整的解决方案。
创建多个多点的功能:
getPlot = function(xplot, yplot, xcutplot, ycutplot, shiftX, shiftY){
plot(xplot, yplot, xlab="x", ylab="y", type="p", pch=20, cex=0.5, col="yellow", main = "Plot1")
legend("topleft", c("Original curve","Cut curve", "Shifted curve" ), pch=20, cex=0.7, col=c("yellow", "black", "green"))
points(xcutplot, ycutplot, type="p", pch=20, cex=0.5, col="black")
points(xsampleplot, ysampleplot, type="p", pch=20, cex=0.05, col="red")
lm.r = lm(ysampleplot ~ xsampleplot)
abline(lm.r)
points(xcutplot + shiftX, ycutplot + shiftY, col="green", type = "p", pch = 20, cex = 0.5)
}
调用该函数的一些数据:
xplot1 = c(1,2,13,4,15)
yplot1 = c(2,5,14,8,19)
xcutplot1 = c(4,5,6)
ycutplot1 = c(5,8,9)
xsampleplot = c(3,7,4,4,0,4,7,2,11,5,8)
ysampleplot = c(5,7,9,3,2,4,6,7,5,8,10)
shiftX1 = 0.5
shiftY1 = 0.8
函数调用:
png(file=paste("~/Documents/plot1.png", sep=""), width=800, height=800 )
getPlot(xplot1, yplot1, xcutplot1, ycutplot1, shiftX1, shiftY1)
dev.off()