[R] Draw two separate legends in xyplot

Gesmann, Markus Markus.Gesmann at lloyds.com
Mon Sep 16 19:02:13 CEST 2013


Here is an example using grid functions, based on an example from Deepayan (https://stat.ethz.ch/pipermail/r-help/2005-April/069459.html)
I hope this helps.

library(grid)
library(lattice)

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

foo <- frameGrob(layout = ft)
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="brown")), 
                 row = 2, col = 3)
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)
fb <-
  grid.layout(nrow = 1, ncol = 2,
              heights = unit(1, "lines"),
              widths =
                unit(c(2, 1),
                     c("cm", "strwidth"),
                     data = list(NULL, "The Beatles")))

boo <- frameGrob(layout = fb)
boo <- placeGrob(boo,
                 rectGrob(width = 0.6, 
                          gp = gpar(col="orange",
                                    fill = "orange")), 
                 row = 1, col = 1)

boo <- placeGrob(boo,
                 textGrob(lab = "The Beatles"), 
                 row = 1, col = 2)

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


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Jun Shen
Sent: 16 September 2013 17:27
To: R-help
Subject: [R] Draw two separate legends in xyplot

Hi all,

I wonder if there is a way to draw two separate legends in xyplot as I would like to separate the legend for data and the legend for reference lines I add. I can use key argument to draw one legend with everything together. What I really want is to put one legend at the bottom and the other on the top. Thanks.

Jun

	[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://urldefense.proofpoint.com/v1/url?u=https://stat.ethz.ch/mailman/listinfo/r-help&k=VTIXiGvdT7U4yPSpeHcrHQ%3D%3D%0A&r=dUkLGPeM%2BYkyyiRRq50yGs%2BmEf8kG%2FyCNQPwZn%2FaQD0%3D%0A&m=GyDRn8AygWnkEHHpZi6XchB6xBiliVWVxVbKN7b8MsE%3D%0A&s=7806cd15bf1d4ce1282cd1e8b2b1328f73cb36ee69b3ecb8640a64bb2e457ce1
PLEASE do read the posting guide https://urldefense.proofpoint.com/v1/url?u=http://www.r-project.org/posting-guide.html&k=VTIXiGvdT7U4yPSpeHcrHQ%3D%3D%0A&r=dUkLGPeM%2BYkyyiRRq50yGs%2BmEf8kG%2FyCNQPwZn%2FaQD0%3D%0A&m=GyDRn8AygWnkEHHpZi6XchB6xBiliVWVxVbKN7b8MsE%3D%0A&s=35157497e0e6679cdcd3116ad32a43ca58adf3889ab0caa0d6a530b87001a008
and provide commented, minimal, self-contained, reproducible code.

----------------------------------------------------------------------
The information in this E-Mail and in any attachments is...{{dropped:19}}



More information about the R-help mailing list