[R] 2-Y-axes on same plot
    Robert W. Baer, Ph.D. 
    rbaer at atsu.edu
       
    Thu Dec 11 21:31:28 CET 2008
    
    
  
> Joe Trubisz wrote:
>> Hi...
>>
>> Is this possible in R?
>>
>> I have 2-sets of data, that were collected simultaneously using 
>> 2-different data acquisition schemes.
>> The x-values are the same for both.
>> The y-values have different ranges (16.4-37.5 using one method, 557-634 
>> using another).
>>
>> In theory, if you plot both plots on top of each other, the graphs should 
>> overlap. The problem I'm having is trying to have to different sets of 
>> y-values appear in the same graph, but scaled in the same vertical space. 
>> I've seen this done in publications, but not sure if it can be done in R.
Here's a brute force graph with two sets of y-axis
# set up some fake test data
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()
# 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)
# Add Legend
legend(5,7000,legend=c("Beta Gal","Cell 
Density"),text.col=c("black","red"),pch=c(16,15),col=c("black","red"))
HTH,
Rob Baer
    
    
More information about the R-help
mailing list