[R] side bars on dendrograms with latticeExtra

deepayan.sarkar at r-project.org deepayan.sarkar at r-project.org
Mon Sep 17 17:18:52 CEST 2007


On 9/17/07, audrey <audrey at ebi.ac.uk> wrote:
> Dear all,
>
> I am using the heatmap representations of latticeExtra package and I
> would be interested to draw color side bars representing the groups of a
> factor of interest. From my understanding of :
>
>  > help(dendrogramGrob)
> ...
>      The 'add' argument can be used for additional annotation at the
>      base of the dendrogram.  It should be a list with one component
>      for each row, with names specifying the type of annotation and
>      components specifying the contents.  Currently, the only supported
>      name is '"rect"' (which can be repeated), producing rectangles.
>      The components in such a case is a list of graphical parameters,
>      possibly vectorized, that are passed on to 'gpar'.
> ...
>
> I should use the argument add. Unfortunately I am not very familiar with
> 'gpar' from grid package, and I cannot find the correct syntax to use
> this 'add' argument.
>
> Does anyone has an example of how to draw side bars next to a dendrogram
> produced with latticeExtra?

Here's one:


library(latticeExtra)

hc1 <- hclust(dist(USArrests, method = "canberra"))
hc1 <- as.dendrogram(hc1)
ord.hc1 <- order.dendrogram(hc1)
hc2 <- reorder(hc1, state.region[ord.hc1])
ord.hc2 <- order.dendrogram(hc2)

region.colors <- trellis.par.get("superpose.polygon")$col

levelplot(t(scale(USArrests))[, ord.hc2],
          scales = list(x = list(rot = 90)),
          colorkey = FALSE,
          legend =
          list(right =
               list(fun = dendrogramGrob,
                    args =
                    list(x = hc2, ord = ord.hc2,
                         side = "right", size = 10, size.add= 0.5,
                         add = list(rect =
                         list(col = "transparent",
                              fill = region.colors[state.region])),
                         type = "rectangle"))))

-Deepayan



More information about the R-help mailing list