R:two axis label
Posted by changjx on April 8, 2008
http://www.apsnet.org/education/AdvancedPlantPath/Topics/RModules/Doc1/01_Nematode.html ## Set up the vector of nematode populations npop <- c(220,180,150,250,270,300,500,580,580,1000,380,100) ## Set up vector of associated months names(npop) <-c ( "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ) ## Set up the bar plots of the nematode populations, try help(barplot) for more information barplot( npop, axes=FALSE, main="Nematode Population", ylim=c(0,1200), col="orange", xlab="Month", ylab="Nematodes per 100 cc soil") axis(2,seq(0,1200,by=200) ) ## Set up the new plot to be drawn on top of the barplot, try help(par) for more information par(new=T) # This sets the average monthly temperature # for the line above the population y <-c (15,17,20,24,26,28,30,31,30,28,19,16) # This positions the points on the X axis # correctly in the middle over the top of the bar chart. y2 <-c (0.5,1.45,2.45,3.5,4.5,5.35,6.5,7.6,8.5,9.55,10.65,11.5) ## Plot the temperature points plot( y2, y, ylim=c(0,35), xlab=NA, ylab=NA, xlim=c(0,12), axes=FALSE, type="o", pch=22, col="mediumblue" ) axis( 4, seq(0,35,by=5), labels=FALSE ) ## Lay out the points for temperature points( y2, y, pch=22, col="mediumblue" ) ## Label the right axis, try help(mtext) for more information mtext( c("0C","5C","10C","15C","20C","25C","30C","35C"), 4, at=seq(0,35,by=5), line=.25 ) ## Label the right axis, try search(mtext) for more information mtext( "Average Soil Temperature", 4, line=1, cex=1 ) ## Create a legend legend( "topleft", c("Soil Temperature","Nematode Population"), pch=c(22,NA), lty=c(1,NA), col=c("mediumblue",NA), inset=0.01 ) ## Create an orange rectangle next to the Nematode Population label for the legend rect(-0.2,32.4,0.4,33.4,col="orange")