[R] I want axes that cross
Charles C. Berry
cberry at tajo.ucsd.edu
Fri Feb 13 21:42:45 CET 2009
On Fri, 13 Feb 2009, Paul Johnson wrote:
> On Fri, Feb 13, 2009 at 1:51 PM, Marc Schwartz
> <marc_schwartz at comcast.net> wrote:
>> on 02/13/2009 01:25 PM Paul Johnson wrote:
>>> Hello, everybody.
>>> A student asked me a howto question I can't answer. We want the
>>> length of the drawn axes to fill the full width and height of the
>>> plot, like so:
>> I am guessing that you want:
>> x <- rnorm(100)
>> z <- gl(2,50)
>> y <- rnorm(100, mean= 1.8*as.numeric(z))
>> plot(x,y,type="n", axes=F)
>> points(x,y, pch="$",cex=0.7, col=z)
>> axis(1, col="green", col.axis="green")
>> axis(2, col="red", col.axis="red")
>> # Draw the box like an "L" on the bottom and left only
>> box(bty = "l")
>> Note that you can specify which sides the 'box' is created upon by using
>> the 'bty' argument. See ?box for more information.
> Thanks, I did not find bty under ?box, but found it under par after
> you pointed it out.
> That does not get the correct output, however, because the black box
> covers over my 2 different colored axes.
If you use
axis( ... , at = c(low,high), xpd=TRUE, col='green',
for suitably chosen values of low and high (and ... ), I think you can
get what you want. It will overwrite your axis, but if you choose the same
color, this won't be noticed.
> Even if I weren't color-conscious, it gives me this:
> not crossed axes, which I want:
> I'm putting in a seed so we will both see the same things in this example.
> x <- rnorm(100)
> z <- gl(2,50)
> y <- rnorm(100, mean= 1.8*as.numeric(z))
> plot(x,y,type="n", axes=F)
> points(x,y, pch="$",cex=0.7, col=z)
> axis(1, col="green", col.axis="green")
> axis(2, col="red", col.axis="red")
> # MS recomends:
> # Draw the box like an "L" on the bottom and left only
> box(bty = "l")
>> Also, by default, the axes extend the range of 'x' and 'y' by 4%. You
>> can use 'xaxs = i' and 'yaxs = i' in the plot() call to restrict the
>> axes to the true ranges of 'x' and 'y'. This would be important, for
>> example, when you want the lower left hand corner of the plot to be at
>> exact coordinates such as 0,0.
> I would be delighted if the axes really did reach 4% outside the data.
> But they don't. I've seen that same thing you are referring to in the
> documentation, but there's something wrong about it, In my example
> code, we should see the same thing now I've put in a seed. The axes
> are smaller than the data range, not equal to 1.04 times the data
> range. I see several observations in the graph that are "off the
> charts", they are above the highest value of the y axis, or below the
> lowest axis value. Similarly, there are observations smaller than the
> low end of the x axis and bigger than the largest x axis value.
> The 4% may be the plot region's size, but it is surely not the length
> of the axis that is drawn?
>> See ?par for more information.
>> Marc Schwartz
> Paul E. Johnson
> Professor, Political Science
> 1541 Lilac Lane, Room 504
> University of Kansas
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Charles C. Berry (858) 534-2098
Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
More information about the R-help