简体   繁体   English

R函数创建多个多重绘图

[英]R Function to create several multiple plots

I have several multiplots to create, where the difference between them it's just the x and y values of 3 of the four curves of the multiplot. 我要创建多个多重绘图,它们之间的区别只是多重绘图的四个曲线中的3个的x和y值。 So one multiplot looks like this: 所以一个多图看起来像这样:

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

That works perfectly. 那很好。 My goal now is to create a function where to put the structure of the multiplot above, and then call the function depending on the x and y values of each. 我现在的目标是创建一个函数,将多重图的结构放在上面,然后根据每个函数的x和y值调用该函数。 So something like this: 所以像这样:

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

And then call the function for each multiplot as: 然后为每个多图调用函数:

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

...and so on. ...等等。

You see that my problem is what to put in return(), since I don't know how to put the plot within a kind of "variable" and then add the other curves to this "variable"... 您会看到我的问题是放在return()中的内容是什么,因为我不知道如何将绘图放在一种“变量”中,然后将其他曲线添加到该“变量”中...

If I don't put anything in return(), it returns me a plot with only the first curve. 如果我没有在return()中放置任何内容,它将返回仅包含第一条曲线的图。

I've not found any solution on the internet, that's why I put the question here to get more ideas... 我在互联网上找不到任何解决方案,这就是为什么我在此处提出问题以获取更多想法的原因...

Thank you in advanced for your help. 在此先感谢您的帮助。

Here is the full solution. 这是完整的解决方案。

Function to create several multiplots: 创建多个多图的功能:

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

Some data to call the function: 调用该函数的一些数据:

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

Function call: 函数调用:

png(file=paste("~/Documents/plot1.png", sep=""), width=800, height=800 )
getPlot(xplot1, yplot1, xcutplot1, ycutplot1, shiftX1, shiftY1)
dev.off()

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

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