Chris Grobler
2006-Sep-03 18:39 UTC
[R] How can I fit the secondary y axis legend on my graph?
Dear All, Having a bit of trouble with plotting two y variables on the same graph. I cannot manage to get the secondary y axis label on to the right of the axis - it gets plotted beyond the graphic window I assume?! The way I constructed the graph is thus: plot(data[,3],data[,2],axes=F, type="b") ## plots my data from two data colums without axes - fine axis(1, at=data[,3]) ## adds my primary axis - fine mtext("Year", side=1, line=2) ## Adds the title "Year" - fine axis(2) mtext("Index of population size", side=2, line=2) ## Puts the primary y axis on and names it correctly points(data[,3],data[,1],pch=16) ## puts my secondary y points on - also fine axis(4) mtext("Number of sun hours in April", side=4, line=2) ## if I enter "line=1" it writes over my inex numbers but if I put "line=2" it disappears. I assume that perhaps the graph automatically fills the space and does not leave any for 'odd' axes. My questions therefore are: how can I get the legend on that axis, how do I remove ylab and xlab to free the space that they take (as they spaced by default at "line=3" and thus waste space - I would like to set them to "line=2") and perhaps how do I scrunch up the x-axis so that it leaves more space at either side? Sorry for the ignorance! Chris
Prof Brian Ripley
2006-Sep-03 18:51 UTC
[R] How can I fit the secondary y axis legend on my graph?
?par, specifically mar and mai, xaxt and yaxt. Note that there are my default on 2.1 lines in the right margin and 4.1 in the left one. You should find the description in 'An Introduction to R' helpful. On Sun, 3 Sep 2006, Chris Grobler wrote:> Dear All, > > Having a bit of trouble with plotting two y variables on the same > graph. I cannot manage to get the secondary y axis label on to the right > of the axis - it gets plotted beyond the graphic window I assume?! The > way I constructed the graph is thus: > > plot(data[,3],data[,2],axes=F, type="b") ## plots my data from two > data colums without axes - fine > axis(1, at=data[,3]) ## adds my > primary axis - fine > mtext("Year", side=1, line=2) ## Adds the title > "Year" - fine > axis(2) > mtext("Index of population size", side=2, line=2) ## Puts the primary > y axis on and names it correctly > points(data[,3],data[,1],pch=16) ## puts my secondary y > points on - also fine > axis(4) > mtext("Number of sun hours in April", side=4, line=2) ## if I enter > "line=1" it writes over my inex numbers but if I put > > "line=2" it disappears. > > I assume that perhaps the graph automatically fills the space and does > not leave any for 'odd' axes. My questions therefore are: how can I get > the legend on that axis, how do I remove ylab and xlab to free the space > that they take (as they spaced by default at "line=3" and thus waste > space - I would like to set them to "line=2") and perhaps how do I > scrunch up the x-axis so that it leaves more space at either side? > > Sorry for the ignorance! > > Chris > > ______________________________________________ > 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. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Jeff Bricker
2006-Sep-03 18:52 UTC
[R] How can I fit the secondary y axis legend on my graph?
I suspect you may need to tweak your margins. See the "mar" argument to the "par" command for guidance. Something like par(mar=c(5,4,4,4)) should probably get you started, though. On 9/3/06, Chris Grobler <bjorn.grobler at zoo.ox.ac.uk> wrote:> Dear All, > > Having a bit of trouble with plotting two y variables on the same > graph. I cannot manage to get the secondary y axis label on to the right > of the axis - it gets plotted beyond the graphic window I assume?! The > way I constructed the graph is thus: > > plot(data[,3],data[,2],axes=F, type="b") ## plots my data from two > data colums without axes - fine > axis(1, at=data[,3]) ## adds my > primary axis - fine > mtext("Year", side=1, line=2) ## Adds the title > "Year" - fine > axis(2) > mtext("Index of population size", side=2, line=2) ## Puts the primary > y axis on and names it correctly > points(data[,3],data[,1],pch=16) ## puts my secondary y > points on - also fine > axis(4) > mtext("Number of sun hours in April", side=4, line=2) ## if I enter > "line=1" it writes over my inex numbers but if I put > > "line=2" it disappears. > > I assume that perhaps the graph automatically fills the space and does > not leave any for 'odd' axes. My questions therefore are: how can I get > the legend on that axis, how do I remove ylab and xlab to free the space > that they take (as they spaced by default at "line=3" and thus waste > space - I would like to set them to "line=2") and perhaps how do I > scrunch up the x-axis so that it leaves more space at either side? > > Sorry for the ignorance! > > Chris > > ______________________________________________ > 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. >
hadley wickham
2006-Sep-03 19:02 UTC
[R] How can I fit the secondary y axis legend on my graph?
> Having a bit of trouble with plotting two y variables on the same > graph. I cannot manage to get the secondary y axis label on to the right > of the axis - it gets plotted beyond the graphic window I assume?! The > way I constructed the graph is thus:Chris, I would strongly suggest you find a different way of visualising your data than using two overlayed line plots. This type of graphic is not an effective way of comparing two time series as it can be so easily misread and misused. See http://junkcharts.typepad.com/junk_charts/2006/06/illusion_of_suc.html and http://junkcharts.typepad.com/junk_charts/2006/05/the_crossover_l.html for examples of this. Hadley
Gabor Grothendieck
2006-Sep-04 02:47 UTC
[R] How can I fit the secondary y axis legend on my graph?
There is an example here: http://zoonek2.free.fr/UNIX/48_R/04.html#1.2 (Scroll down until you see the graphic with the two y axes. The code is in the box just above it.) On 9/3/06, Chris Grobler <bjorn.grobler at zoo.ox.ac.uk> wrote:> Dear All, > > Having a bit of trouble with plotting two y variables on the same > graph. I cannot manage to get the secondary y axis label on to the right > of the axis - it gets plotted beyond the graphic window I assume?! The > way I constructed the graph is thus: > > plot(data[,3],data[,2],axes=F, type="b") ## plots my data from two > data colums without axes - fine > axis(1, at=data[,3]) ## adds my > primary axis - fine > mtext("Year", side=1, line=2) ## Adds the title > "Year" - fine > axis(2) > mtext("Index of population size", side=2, line=2) ## Puts the primary > y axis on and names it correctly > points(data[,3],data[,1],pch=16) ## puts my secondary y > points on - also fine > axis(4) > mtext("Number of sun hours in April", side=4, line=2) ## if I enter > "line=1" it writes over my inex numbers but if I put > > "line=2" it disappears. > > I assume that perhaps the graph automatically fills the space and does > not leave any for 'odd' axes. My questions therefore are: how can I get > the legend on that axis, how do I remove ylab and xlab to free the space > that they take (as they spaced by default at "line=3" and thus waste > space - I would like to set them to "line=2") and perhaps how do I > scrunch up the x-axis so that it leaves more space at either side? > > Sorry for the ignorance! > > Chris > > ______________________________________________ > 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. >