0

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.

Andy
  • 49,085
  • 60
  • 166
  • 233
LRD
  • 351
  • 3
  • 13
  • Plotting functions are used for their side effect (of plotting). They don't need a return value. You could return `invisible(NULL)` (like `plot.default` does). – Roland Jun 12 '14 at 09:53
  • @Roland Thanks for your answer, but this doesn't solve my problem. As I said, when I don't put a return value, or if I put invisible(NULL), it returns me just the plot with the first curve. It doesn't work as it does out of the function... – LRD Jun 12 '14 at 13:53
  • A return value won't help you. You have a different problem, but since you example is not reproducible due to lack of data, it's too much work to find the problem. E.g., `xsampleplot` is not defined in your function, so it must be defined in the global environment if the code runs without errors. – Roland Jun 12 '14 at 13:59
  • As @Roland said, your function doesn't need to return anything at all. Base graphics commands simply update the graphics device. If you need to add more to the plot, just continue to draw on the graphics device. It is unclear what your problem is. Please provide a [minimual, reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with data and enough code that we can run the command and see the same problem you are experiencing. – MrFlick Jun 12 '14 at 14:26
  • If you answer your own question, it's better to actually post it as an answer rather than edit the original question to include the answer. This way the question will appear as "answered" and not remain "open" – MrFlick Jun 14 '14 at 04:32
  • @MrFlick Ok, thank you. I didn't know this, sorry. I've just post the answer. Thanks again! – LRD Jun 18 '14 at 14:23

1 Answers1

1

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()
LRD
  • 351
  • 3
  • 13