# [R] Stacked plots with common x-axis and different y-axis

Jason Rupert jasonkrupert at yahoo.com
Wed Aug 5 05:49:59 CEST 2009

```time <- seq(0,72,12)
betagal.abs <- c(0.05,0.18,0.25,0.31,0.32,0.34,0.35)
cell.density <- c(0,1000,2000,3000,4000,5000,6000)

#add extra space to right margin of plot within frame par(mar=c(5, 4, 4, 4) + 0.1)

# Plot first set of data and draw its axis
plot(time, betagal.abs, pch=16, axes=F, ylim=c(0,1), xlab="", ylab="", type="b",col="black", main="Mike's test data")
axis(2, ylim=c(0,1),col="black")
mtext("Beta Gal Absorbance",side=2,line=2.5)
box()
I found the following example, but it is not quite what I am looking for.  Ideally, these one of these plots would be above the other plot, but keeping the same x-axis.

It is good to see that others have had similar issues and tried to work through methods to address it.

Thank you again for any feedback that might be provided.

http://tolstoy.newcastle.edu.au/R/e5/help/08/12/9856.html

# Allow a second plot on the same graph
par(new=T)

# Plot the second plot and put axis scale on right
plot(time, cell.density, pch=15, xlab="", ylab="", ylim=c(0,7000), axes=F, type="b", col="red")
mtext("Cell Density",side=4,col="red",line=2.5)
axis(4, ylim=c(0,7000), col="red",col.axis="red")

# Draw the time axis
axis(1,pretty(range(time),10))
mtext("Time (Hours)",side=1,col="black",line=2.5)

legend(5,7000,legend=c("Beta Gal","Cell Density"),text.col=c("black","red"),pch=c(16,15),col=c("black","red"))

--- On Tue, 8/4/09, Jason Rupert <jasonkrupert at yahoo.com> wrote:

> From: Jason Rupert <jasonkrupert at yahoo.com>
> Subject: Stacked plots with common x-axis and different y-axis
> To: R-help at r-project.org
> Cc: jasonkrupert at yahoo.com
> Date: Tuesday, August 4, 2009, 4:33 PM
> Is there a place that shows how to
> create two plots that are stacked on top of each other where
> they share a common x-axis scale, but have differnt y-axis
> scale?
>
> Say have the following data: airquality
> Stack plot(airquality\$Day, airquality\$Wind) on top of
> plot(airquality\$Day, airquality\$Temp).
>
> I am interested in stacking the two on top of each other
> with no seam, or plotting the two lines with two different
> y-axis scales on the same plot.
>
> Thanks for any feedback and insights.
>
>
>
>
>

```