[R] Lattice Legend/Key by row instead of by column

Duncan Mackay dulcalma at bigpond.com
Thu Oct 31 23:47:47 CET 2013


Hi Richard

If you cannot get a better suggestion this example from Deepayan Sarkar may
help. 
It is way back in the archives and I do not have a reference for it.

I have used it about a year ago as a template to do a complicated key

fl <- grid.layout(nrow = 2, ncol = 6, 
                  heights = unit(rep(1, 2), "lines"),
                  widths = unit(c(2, 1, 2, 1, 2, 1), 
 
c("cm","strwidth","cm","strwidth","cm","strwidth"), 
                  data = list(NULL,"John",NULL,"George",NULL,"The
Beatles"))) 

foo <- frameGrob(layout = fl)
foo <- placeGrob(foo, 
                 pointsGrob(.5, .5, pch=19, 
                            gp = gpar(col="red", cex=0.5)), 
                 row = 1, col = 1) 
foo <- placeGrob(foo, 
                 linesGrob(c(0.2, 0.8), c(.5, .5), 
                           gp = gpar(col="blue")), 
                 row = 2, col = 1) 
foo <- placeGrob(foo, 
                 linesGrob(c(0.2, 0.8), c(.5, .5), 
                           gp = gpar(col="green")), 
                 row = 1, col = 3) 
foo <- placeGrob(foo, 
                 linesGrob(c(0.2, 0.8), c(.5, .5), 
                           gp = gpar(col="orange")), 
                 row = 2, col = 3) 
foo <- placeGrob(foo, 
                 rectGrob(width = 0.6, 
                          gp = gpar(col="#FFFFCC", 
                          fill = "#FFFFCC")), 
                 row = 1, col = 5) 
foo <- placeGrob(foo, 
                 textGrob(lab = "John"), 
                 row = 1, col = 2) 
foo <- placeGrob(foo, 
                 textGrob(lab = "Paul"), 
                 row = 2, col = 2) 
foo <- placeGrob(foo, 
                 textGrob(lab = "George"), 
                 row = 1, col = 4) 
foo <- placeGrob(foo, 
                 textGrob(lab = "Ringo"), 
                 row = 2, col = 4) 
foo <- placeGrob(foo, 
                 textGrob(lab = "The Beatles"), 
                 row = 1, col = 6) 

xyplot(1 ~ 1, legend = list(top = list(fun = foo)))

In my case I changed  "strwidth" to "cm" for the text as I was cramped for
space

HTH

Duncan

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Richard Kwock
Sent: Friday, 1 November 2013 06:42
To: R help
Subject: [R] Lattice Legend/Key by row instead of by column

Hi All,

I am having some trouble getting lattice to display the legend names by row
instead of by column (default).

Example:

library(lattice)
set.seed(456846)
data <- matrix(c(1:10) + runif(50), ncol = 5, nrow = 10) dataset <-
data.frame(data = as.vector(data), group = rep(1:5, each = 10), time = 1:10)

xyplot(data ~ time, group = group, dataset, t = "l",
  key = list(text = list(paste("group", unique(dataset$group)) ),
    lines = list(col = trellis.par.get()$superpose.symbol$col[1:5]),
    columns = 4
  )
)

What I'm hoping for are 4 columns in the legend, like this:
Legend row 1: "group 1", "group 2", "group 3", "group 4"
Legend row 2: "group 5"

However, I'm getting:
Legend row 1: "group 1", "group 3", "group 5"
Legend row 2: "group 2", "group 4"

I can see how this might work if I include blanks/NULLs in the legend as
placeholders, but that might get messy in data sets with many groups.

Any ideas on how to get around this?

Thanks,
Richard

	[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list