# How to get a plot generated by R returned in an output cell?

It seems we'll never have built in two axis plots and list plots in Mathematica but you can make them in R.

Needs["RLink"]
InstallR[];
REvaluate["{
x <- 1:5
y1 <- rnorm(5)
y2 <- rnorm(5,20)
par(mar=c(5,4,4,5)+.1)
plot(x,y1,type=\"l\",col=\"red\")
par(new=TRUE)
plot(x,y2,,type=\"l\",col=\"blue\",xaxt=\"n\",yaxt=\"n\",xlab=\"\",\ ylab=\"\")
axis(4)
mtext(\"y2\",side=4,line=3)
legend(\"topleft\",col=c(\"red\",\"blue\"),lty=1,legend=c(\"y1\",\"\
y2\"))}"]


Which returns this plot in a popup window: How can I get the plot that is generated from this code to appear in an Output cell?

• The answers here mathematica.stackexchange.com/questions/627/1-plot-2-scale-axis show some methods of achieving 2 axis plots in Mathematica, if that is of help. – image_doctor Dec 21 '12 at 10:27
• there are plenty of two axis code around. My point was that it is not built in to Mma and not even a package. Linking to R seems like a more sensible alternative since the link is built in and the code is native R. Excel would probably be better but an Excel add on is not built in. – Mike Honeychurch Dec 21 '12 at 12:30

## 1 Answer

Wolfram website What's New? has quite a few cool examples of new features. We are interested here in

I will give it here to apply to your specific code.

"This example illustrates some means of code composition supported by RLink, which allow smooth and productive combined R-Mathematica workflows. Load RLink."

Needs["RLink"]
InstallR[]


"The following creates a generic R wrapper that creates the plot and saves it to a file, and defines certain parameters for the resulting image."

mathematicaRPlotWrapper = RFunction["function(filename, plotfun){
pdf(filename)
plotfun()
dev.off()
}"];


"Create a Mathematica counterpart wrapper that automates file import and adds error-checking."

Clear[getRPlot];
getRPlot[plotFun_RFunction] :=
With[{tempfile = FileNameJoin[{$TemporaryDirectory, "temp.pdf"}]}, If[FileExistsQ[tempfile], DeleteFile[tempfile]]; mathematicaRPlotWrapper[tempfile, plotFun]; If[! FileExistsQ[tempfile], Return[$Failed]];
Import[tempfile]];


Now, the above functions worked with all examples I tried. So I will apply it to your specific case here:

Show[#, ImageSize -> Medium, PlotRange -> All] &@
getRPlot[RFunction["function(){
x <- 1:5
y1 <- rnorm(5)
y2 <- rnorm(5,20)
par(mar=c(5,4,4,5)+.1)
plot(x,y1,type=\"l\",col=\"red\")
par(new=TRUE)
plot(x,y2,,type=\"l\",col=\"blue\",xaxt=\"n\",yaxt=\"n\",xlab=\"\"\
,\ ylab=\"\")
axis(4)
mtext(\"y2\",side=4,line=3)
legend(\"topleft\",col=c(\"red\",\"blue\"),lty=1,legend=c(\"y1\",\
\"y2\"))
}"]] • cool. Just installed 9 and am experimenting! – Mike Honeychurch Dec 21 '12 at 1:59
• Stack Exchange Eg - Love this link - wolfram.com/mathematica/new-in-9/built-in-integration-with-r/… – rselva Dec 21 '12 at 8:36
• Can't resist to upvote this one, +1 :) – Leonid Shifrin Dec 21 '12 at 10:13
• I couldn't find this example in the documentation centre. Is it there? It seems like it should be part of the RLink tutorial. – Mike Honeychurch Dec 21 '12 at 21:23
• @MikeHoneychurch As far as I know this example lives only on that webpage I linked. – Vitaliy Kaurov Dec 21 '12 at 22:04