[R] matlab style plotting in R

Matthew Keller mckellercran at gmail.com
Tue Feb 13 18:50:05 CET 2007


Hi Maria,

I'm interested in the responses you get. The way I do this is to use
par(new=TRUE), which tells R not to clean the frame before plotting
the next plot. So, eg

###Overlaid plot
op <- par(mar = c(5, 4, 4, 5) + 0.1, las = 2)
plot(x=1:5,y=rnorm(5)+1:5,type='b',xlab="xlab",ylab="ylab",main="Title")
par(new=TRUE)
plot(x=1:5,y=rnorm(5),axes=FALSE,ylab="",xlab="",type='b',col="red")
axis(4,at=c(-2,-1,0,1,2),labels=c(6,7,8,9,10))
par(las=0)
mtext("Other Y",side=4,line=3)


The trick is this: if your 2nd set of Y variables are on a different
scale than your 1st set of Y-variables, you'll need to transform the
second set so that they'll be on the same scale - otherwise they won't
show up on the old plot. You'll also, of course, need to
back-transform your labels for the 2nd set of Y variables so that they
read appropriately.

A terrific site for R graphics, with lots of worked examples
(including ones similar to the one I just did), is here:
http://zoonek2.free.fr/UNIX/48_R/all.html




On 2/13/07, Maria Vatapitakapha <xrysoflis at gmail.com> wrote:
> Hello
>
> I was wondering how I can achieve matlab style plotting in R,
> in the sense that matlab allows you to plot multiple sets of variables
> within the same
> x-y axes. plot in R does not seem to cater for this. I tried 'overplot' from
> the gplots package but this assumes different y axes for the variables.
>
> any suggestions would be very appreciated
>
> Maria
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>


-- 
Matthew C Keller
Postdoctoral Fellow
Virginia Institute for Psychiatric and Behavioral Genetics



More information about the R-help mailing list