I'm trying to make a transition from S-plus under Windows to R under Linux. My immediate aim is to produce a scatter plot with two y-axes with different scales. That can be done in S-plus with a command of the following form: guiPlot( PlotType="Scatter", DataSet="execon", Columns="years,hstart,ship", AxisType="Multiple Y" ), where years, hstart, and ship are columns in the dataset called execon. Is there an equivalent command in R? I'd be most grateful for any suggestions. --- John P. Burkett Department of Economics University of Rhode Island 10 Chafee Road, Suite 3 Kingston, RI 02881-0808 phone: (401) 874-4122 fax: (401) 874-2858 -------------- next part -------------- An HTML attachment was scrubbed... URL: https://stat.ethz.ch/pipermail/r-help/attachments/20000907/005b6474/attachment.html
On Thu, 7 Sep 2000, John P. Burkett wrote:> I'm trying to make a transition from S-plus under Windows to R under > Linux. My immediate aim is to produce a scatter plot with two y-axes > with different scales. That can be done in S-plus with a command of the > following form: > guiPlot( PlotType="Scatter", DataSet="execon", > Columns="years,hstart,ship", AxisType="Multiple Y" ), > where years, hstart, and ship are columns in the dataset called execon. > Is there an equivalent command in R? I'd be most grateful for any > suggestions.No. You can add a second set of axis labels with axis(), but there is no built-in way to plot using the second y axis. -thomas -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hi John, As it happens, I once had the same problem myself. I didn't have R then, but it was kind of fun seeing how much easier it is. You will probably want to dress this up a bit as it is very basic. Jim -------------- next part -------------- plot2ax<-function(x1,y1,xlim1,ylim1,x2,y2,xlim2,ylim2,y2lab=NULL,type="b",pch1=1,pch2=2,...) { if(!missing(y1)) { oldmar<-par("mar") par(mar=c(5,4,4,4)) if(missing(x1)) x1<-1:length(y1) if(missing(xlim1)) xlim1<-range(x1) if(missing(ylim1)) ylim1<-range(y1) plot(x1,y1,xlim=xlim1,ylim=ylim1,pch=pch1,...) if(!missing(y2)) { if(missing(x2)) x2<-1:length(y2) if(missing(xlim2)) xlim2<-range(x2) if(missing(ylim2)) ylim2<-range(y2) ax2inc<-(ylim2[2] - ylim2[1])/6 ax2val<-c(ylim2[1],ylim2[1]+ax2inc,ylim2[1]+2*ax2inc,ylim2[1]+3*ax2inc, ylim2[2]-2*ax2inc,ylim2[2]-ax2inc,ylim2[2]) axis(4,labels=as.character(ax2val)) xmult<-(xlim1[2] - xlim1[1])/(xlim2[2] - xlim2[1]) ymult<-(ylim1[2] - ylim1[1])/(ylim2[2] - ylim2[1]) points((x2-xlim2[1])*xmult+xlim1[1],(y2-ylim2[1])*ymult+ylim1[1],pch=pch2) mtext(y2lab,4,2) } par(oldmar) } else cat("Usage: plot2ax(x1,y1,xlim1,ylim1,x2,y2,xlim2,ylim2,y2lab,type=\"b\",pch1=1,pch2=2)\n") }
Thanks to Uwe Ligges, who found a couple of bugs in the function I posted last week. Here's a (hopefully) debugged version. Jim -------------- next part -------------- plot2ax<-function(x1,y1,xlim1,ylim1,x2,y2,xlim2,ylim2,y2lab,type="b",pch1=1,pch2=2,...) { if(!missing(y1)) { oldmar<-par("mar") par(mar=c(5,4,4,4)) if(missing(x1)) x1<-1:length(y1) if(missing(xlim1)) xlim1<-range(x1) if(missing(ylim1)) ylim1<-range(y1) plot(x1,y1,xlim=xlim1,ylim=ylim1,pch=pch1,type=type,...) if(!missing(y2)) { if(missing(x2)) x2<-1:length(y2) if(missing(xlim2)) xlim2<-range(x2) if(missing(ylim2)) ylim2<-range(pretty(y2)) ax2val<-pretty(y2) xmult<-(xlim1[2] - xlim1[1])/(xlim2[2] - xlim2[1]) ymult<-(ylim1[2] - ylim1[1])/(ylim2[2] - ylim2[1]) axis(4,(ax2val-ylim2[1])*ymult+ylim1[1],labels=as.character(ax2val)) points((x2-xlim2[1])*xmult+xlim1[1],(y2-ylim2[1])*ymult+ylim1[1], pch=pch2,type=type) if(!missing(y2lab)) mtext(y2lab,4,2) } par(oldmar) } else cat("Usage: plot2ax(x1,y1,xlim1,ylim1,x2,y2,xlim2,ylim2,y2lab,type=\"b\",pch1=1,pch2=2)\n") }