[R] Adding title to colorkey

Deepayan Sarkar deepayan.sarkar at gmail.com
Mon Jun 18 13:32:59 CEST 2012


On Sat, Jun 16, 2012 at 2:10 PM, Stephen Eglen
<S.J.Eglen at damtp.cam.ac.uk> wrote:
>
> A recent paper on visualisation (in Neuron, a leading neuroscience
> journal) surveyed how well previous articles in this journal labelled their
> graphs (e.g. axis labelling and describing their error bars).  Of
> particular interest is that (only) 40% of plots labelled what their
> colorkey was showing (variable and units).
>
> The paper is at http://dx.doi.org/10.1016/j.neuron.2012.05.001
>
> R is not yet that prominent (compared to matlab) in Neuroscience, so I
> doubt many of the graphs were generated by levelplot() and friends.
> However, how can the colorkey be labelled?  I notice that this topic has
> been raised before, e.g.
>
>  http://tolstoy.newcastle.edu.au/R/e16/help/11/11/2281.html
>
> For now, I've done:
>
> library(lattice)
> library(grid)
> levelplot(matrix(1:9,3,3),
>          par.settings = list(layout.widths = list(axis.key.padding = 4)))
> grid.text('title here', y=unit(0.5, "npc"),
>          rot=90, x=unit(0.88, "npc"))
>
> i.e. adding some space between levelplot and colorkey.  The
> x,y positions of the grid.text call need fine-tuning once the plot is
> close to finalised.
>
> Does anyone have a better solution for vertical colorkeys?  e.g. can the
> plot objected be interrogated to work out what the central x,y value is?

This is slightly simpler:

levelplot(matrix(1:9,3,3), ylab.right = "title here",
          par.settings = list(layout.widths = list(axis.key.padding = 0,
                                                   ylab.right = 2)))

There really should be a function allowing easy construction of
complex legends combining simpler ones. There is currently only
mergedTrellisLegendGrob in latticeExtra (not very robust) which can be
used as follows:

library(latticeExtra)

p <- levelplot(matrix(1:9,3,3))
p$legend$right <-
    list(fun = mergedTrellisLegendGrob(p$legend$right,
                                       list(fun = textGrob,
                                            args = list("title here",
rot = -90)),
                                       vertical = FALSE))
p

-Deepayan



More information about the R-help mailing list