[R] Combine two lattice plots including both plot keys

Naresh Gurbuxani n@re@h_gurbux@n| @end|ng |rom hotm@||@com
Sun Sep 6 15:14:55 CEST 2026


This solves my problem.

Now a features request.  The legend at the top shows plot character separately from line.  Is it possible to show plot character superimposed on line?  This feature is already available in some other lattice functions.  For example:

mydt[, xyplot(popstart + popend ~ year, type = c("o", "g"), auto.key = (list(columns = 2, space = "bottom")))]  # key shows plot points superimposed on plot lines

Thanks again,
Naresh


On Sep 5, 2026, at 10:59 AM, Deepayan Sarkar <deepayan.sarkar using gmail.com> wrote:



On Sat, 5 Sept 2026 at 19:58, Naresh Gurbuxani <naresh_gurbuxani using hotmail.com<mailto:naresh_gurbuxani using hotmail.com>> wrote:

When I combine two lattice plots, only the first plot keys are
displayed.  Is it possible to show both keys?

In principle yes, but clearly +.trellis doesn't try hard enough.

You could do

p = popbar + popdot
p$legend = c(popbar$legend, popdot$legend)
p

or more succintly

update(popbar + popdot, legend = c(popbar$legend, popdot$legend))

Best,
-Deepayan



Thanks,
Naresh

library(data.table)
library(lattice)
library(latticeExtra)

mydt <- data.table(year = rep(2020:2025, 2), bird = rep(c("duck",
"goose"), c(6, 6)))
mydt[, let(popstart = 100 + sample(-10:10, 12, replace = TRUE), popend =
120 + sample(-10:10, 12, replace = TRUE))]
mydt[, let(flights = rep(seq(200, by = 10, length.out = 6), 2) +
sample(-20:20, 12, replace = TRUE))]
mydt1 <- copy(mydt)[, let(obspot = "lake")]
mydt2 <- copy(mydt)[, let(obspot = "river")]
mydt <- rbindlist(list(mydt1, mydt2))
mydtmelt <- melt(mydt, id.vars = c("year", "bird", "obspot"),
measure.vars = c("popstart", "flights"))
popbar <- mydtmelt[, barchart(value ~ as.character(year) | bird +
obspot, groups = variable, horizontal = FALSE, stack = TRUE, auto.key =
list(columns = 2, space = "bottom")) |> useOuterStrips()]
popdot <-  mydt[, dotplot(popend ~ as.character(year) | bird + obspot,
type = "o", panel = function(x, y, ...){panel.dotplot(x, y, ...,
col.line = "transparent")}, key = simpleKey("popend", space = "top",
lines = TRUE, points = TRUE)) |> useOuterStrips()]

popbar # shows key at the bottom
popdot # shows key at the top

popbar + popdot # bottom key only

______________________________________________
R-help using r-project.org<mailto:R-help using r-project.org> mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html<https://www.r-project.org/posting-guide.html>
and provide commented, minimal, self-contained, reproducible code.


	[[alternative HTML version deleted]]



More information about the R-help mailing list