[R] Ticks and labels on plots

Marc Schwartz marc_schwartz at comcast.net
Thu Mar 22 15:39:38 CET 2007


<snip>

On Thu, 2007-03-22 at 10:03 +0100, Martin Maechler wrote:
> >>>>> "Marc" == Marc Schwartz <marc_schwartz at comcast.net>

>     Marc> To get a feel for how axis() creates the default tick positions when
>     Marc> 'at' is the default NULL, see ?axTicks, which provides functionality
>     Marc> similar to the internal C routine.
> 
> yes, partly not only similar but "the same" when it works (by
> default) with par("axp")
> 
>     Marc> You could also look at ?pretty
> 
> Yes.  *However* there's one important thing which I think hasn't
> been mentioned yet.
> 
> We have now been talking how and where axis() {i.e. its internal
> code} chooses to place tick marks.
> The clue is that it draws labels at all tick marks by default or
> explicitly with axis(*, at= ., labels = .)
> BUT the labels are not shown on your plot as soon as they
> would get too much crammed together.
> 
> You can see this nicely, interactively, by the following:
> Use
>      graphics.off()
>      plot(1:11)
> 
> This will show ticks *and* labels  at  2 , 4 , 6 , 8, 10
> and now use your mouse, drag to make the graphics window
> narrower (e.g. keeping height constant), in small steps,
> releasing the mouse again to let R redraw the graphic.
> For quite a while, the labels remain until there's not enough
> room, the 5 ticks remain, but only the
>  labels  2 , 4, 6, 8  are drawn
>  then    2 , 6 , 10 
>  then    2 , 6
>  then    2 , 8  ( a little surprise to me)
>  then    2
> 
> you always see all ticks but labels are only drawn when they
> don't get in each other's way.
> 
> Of course, things like
> 
>  plot(1:11, xaxt="n")
>  axis(1, at=1:11, labels = paste("Lab", 1:11))
> 
> show even more when the window is widened or narrowed,
> and yes, it depends on the device and on the fonts and its
> sizes, see e.g.,
> 
>  plot(1:11, xaxt="n")
>  axis(1, at=1:11, labels = paste("Lab", 1:11), cex.axis = 0.5)
> 
> ----- -----
> 
> If you don't like this --- almost always very desirable ---
> builtin smartness, you need to descend slightly lower level and use
> mtext(), e.g., the analogue of the above is
> 
>   plot(1:11, xaxt="n")
>   mtext(side=1, at=1:11, text = paste("Lab", 1:11), cex = 0.5)
> 
> or rather leave traditional graphics which really gets messy in
> such cases {par() .. etc} and upgrade to using the "grid"
> package,  or also packages built on grid, "lattice" or "ggplot".
> But infact, someone else needs to tell you how to play similar
> goes with these.

Excellent points Martin.

My initial reply and example, where I had the tick marks at each bar,
but the labels every 5 bars, was implicitly (at least in my own mind ;-)
a consequence of the spacing issues that you raise.

Best regards,

Marc



More information about the R-help mailing list