[R] custom tick labels on image

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Jul 6 19:55:08 CEST 2006


On Thu, 6 Jul 2006, Pieter Provoost wrote:

> Prof Brian Ripley schreef:
>> On Thu, 6 Jul 2006, Pieter Provoost wrote:
>> 
>>> Hi,
>>> 
>>> I'm trying to visualise a matrix using image(), and I would like to add
>>> the row and column names as tick labels. I tried adding an axis, but the
>>> problem is that this axis goes from 0 to 1 which results in labels being
>>> added starting at the far right end of the axis.
>>> 
>>> image(t(data), col = terrain.colors(20), axes=FALSE)
>>> axis(3, at = 1:length(colnames(data)), labels=colnames(data))
>>> 
>>> How can I do this better?
>> 
>> It is probably easiest to supply x and y when calling image, as in the 
>> final example on the help page.  (And BTW, ncol() gives the number of 
>> columns more readily than length(colnames()).)
>> 
>
> Thanks, this worked fine. One additional question: how can I rotate the 
> labels so they all can be displayed? Srt doesn't seem to work.

?axis says

      ...: other graphical parameters may also be passed as arguments to
           this function, particularly, 'cex.axis', 'col.axis' and
           'font.axis' for axis annotation, 'mgp' and 'xaxp' or 'yaxp'
           for positioning, 'tck' or 'tcl' for tick mark length and
           direction, 'las' for vertical/horizontal label orientation,
           or 'fg' instead of 'col', see 'par' on these.

so you need to use las and accept 0/90/etc.


>
> x <- (1:nrow(data))
> y <- (1:ncol(data))
> image(y, x, t(data), col = heat.colors(20), 
> axes=FALSE,xlab="",ylab="",srt=45)
> axis(3, at = 1:ncol(data), labels=colnames(data),srt=45,tick=FALSE)
> axis(2, at = 1:nrow(data), labels=rownames(data),srt=45,tick=FALSE)
>
>

-- 
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



More information about the R-help mailing list