22

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:

enter image description here

How can I get the plot that is generated from this code to appear in an Output cell?

Mike Honeychurch
  • 37,541
  • 3
  • 85
  • 158
  • The answers here http://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 Answers1

16

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

Using Higher-Order Functions and Closures to Structure Code: Flexible Plotting Routines

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\"))
      }"]]

enter image description here

Vitaliy Kaurov
  • 73,078
  • 9
  • 204
  • 355
  • 1
    cool. Just installed 9 and am experimenting! – Mike Honeychurch Dec 21 '12 at 01:59
  • 1
    Stack Exchange Eg - Love this link - http://www.wolfram.com/mathematica/new-in-9/built-in-integration-with-r/exploring-statistics-of-beta-sites-of-the-stackexc.html – rselva Dec 21 '12 at 08:36
  • 1
    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
  • That's unusual and somewhat annoying. Having installed 9 I was searching the docs for information -- not the webpage. These examples should be part of the RLink tutorial or alternatively should be "How Tos". Can you please pass on that suggestion. – Mike Honeychurch Dec 21 '12 at 22:08
  • @MikeHoneychurch Sure, Mike, I will. – Vitaliy Kaurov Dec 21 '12 at 22:17
  • Do you think all those new in 9 examples will be available as a bundled download? ...if they are not in the docs it saves us having to type this stuff out. – Mike Honeychurch Dec 21 '12 at 22:37
  • @MikeHoneychurch You don't have to type it. Click on code - a box will pop up where from you can copy the code. – Vitaliy Kaurov Dec 21 '12 at 22:40
  • yeah I know that -- by "type" I guess I mean that generically. It is a lot of copy and pasting when you consider the total number of examples. Plus notebook versions would presumably also contain the narrative (which also has to be copied and pasted). – Mike Honeychurch Dec 21 '12 at 22:59
  • @MikeHoneychurch I'll pass this along, but I think you too should contact customer service and express your wishes. It really can help. – Vitaliy Kaurov Dec 21 '12 at 23:01
  • will do that right now. – Mike Honeychurch Dec 21 '12 at 23:06
  • @MikeHoneychurch You could have pinged me directly about those examples, I am the one responsible for them and their placement. The thing is, I made these extra ones after the main docs were done, and there wasn't enough time to add them to the docs. I will make an effort to get them into V9.0.1 though. – Leonid Shifrin Dec 22 '12 at 16:36
  • @LeonidShifrin I did not know it was you who made those examples. Examples like that and associated materials are not always written by the developer -- quite often not -- e.g. I have written stuff for them -- and Wolfram have an entire group that is supposed to create these materials. – Mike Honeychurch Dec 22 '12 at 21:23
  • @MikeHoneychurch In this case, I wrote all those examples (as well as RLink's documentation, of course), just because I was the one most familiar with the topic, so it was much easier for me than for anyone else there. – Leonid Shifrin Dec 22 '12 at 22:16
  • @LeonidShifrin in 9.0.1 I think they probably belong in the "How Tos" section (with links from the RLink tutorial) or perhaps part of the RLink tutorial. In the meantime can these notebook be put somewhere for download? Do you have a Dropbox? thanks Mike – Mike Honeychurch Dec 22 '12 at 22:27
  • @MikeHoneychurch I will have to first talk to the TCS team, to make sure that they approve public upload of the notebooks, and then I will upload them on the http://download.wolfram.com/. I could send the file with examples to you personally though, with a request that you don't distribute it as publicly endorsed by WRI, if you give me your e-mail. You can email me at lshifr at gmail. I might not reply until tomorrow though, since I am mostly off for today. – Leonid Shifrin Dec 22 '12 at 23:10