简体   繁体   中英

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. 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. 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"...

If I don't put anything in return(), it returns me a plot with only the first curve.

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

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