[R] Plot ticks and tick labels: thickness, colour?

Marc Schwartz MSchwartz at medanalytics.com
Thu Aug 7 00:36:54 CEST 2003


On Wed, 2003-08-06 at 16:30, Dirk Eddelbuettel wrote: 
> I am displaying several series in one plot, and would like to make
> them distinct without having to employ a legend. 
> 
> I managed to color tickmarks, but have been unsuccessful with either one of 
> 
> a) making tickmarks thicker (without increasing the axis at the same time).
>    From reading ?axis:
>        lty, lwd: line type, width for the axis line and the tick marks.
>    in would appear that I cannot obtain the one _without_ the other; or
>    
> b) displaying the tick label in a different colour. Again, ?axis reads
>      col: color for the axis line and the tick marks. [..]
>    indicates that I can only set the tick mark, not the annotation.
>    
> Any ideas or suggestions?
> 
> Thanks in advance,  Dirk


Dirk,

One possible option:

# Generic plot with no tick marks or annotation
plot(1:10, 1:10, axes = FALSE, ann = FALSE)

# First get par("usr") to get x and y axis ranges
usr <- par("usr")

# Now draw tick marks, using axTicks() to get
# default locations
# First, x axis
at.x <- axTicks(1)
segments(at.x, usr[3], at.x,
         usr[3] - ((usr[4] - usr[3]) * 0.01), 
         lwd = 2, col = "red", xpd = TRUE)

# Draw x axis tick mark labels
mtext(at.x, at = at.x, side = 1, line = 1, 
      col = "red")

# Now do the same for the y axis
at.y <- axTicks(2)
segments(usr[1], at.y,
         usr[1] - ((usr[2] - usr[1]) * 0.01), 
         at.y,
         lwd = 2, col = "blue", xpd = TRUE)

# Draw y axis tick mark labels
mtext(at.y, at = at.y, side = 2, line = 1, 
      col = "blue")

# put a box around the plot region
box()

In the above calls to segments(), adjust the '* 0.01' to be the tick
mark length you wish as a proportion of the axis range and of course the
'lwd' and 'col' to your preference.

If you want the tick marks inside the plot region, rather than outside,
change the initial subtractions to additions. Note the use of 'xpd =
TRUE', so that the tick marks are not clipped at the plot region for
'outside'.

If you need to use something other than the default tick mark locations,
you can always adjust 'at.x' and 'at.y' to whatever you choose.

HTH,

Marc Schwartz




More information about the R-help mailing list