Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
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
add comment

1 Answer 1

up vote 13 down vote accepted

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

share|improve this answer
1  
cool. Just installed 9 and am experimenting! –  Mike Honeychurch Dec 21 '12 at 1:59
1  
Stack Exchange Eg - Love this link - wolfram.com/mathematica/new-in-9/built-in-integration-with-r/… –  rselva Dec 21 '12 at 8: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
show 12 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.