Re: [R] Overlay plots from different data sets using the Lattice package

From: Christoph Meyer <>
Date: Tue, 01 Apr 2008 09:00:09 -0700 (PDT)

Hi all,
I'm a newbie to lattice graphics. I think I have a very similar problem trying to overlay plots from different data sets. I tried a number of different things but don't seem to get it to work. I am using xYplot from library Hmisc to produce a lattice plot with Median plus error bands plotted as lines for each panel. This works fine. Now I want to add specific points to each panel, however, the data for those points are in a different data frame. I tried something along the lines of Deepayan's example, combining the two data sets using make.groups, but that doesn't seem to work. My code:

data=read.table("all species.txt",header=T) data.nonrandom=read.table("all species_non-random.txt",header=T)


xYplot(Cbind(Median,Lower,Upper)~species.pool|data.set.location, data=combined,subset=response.variable=="sr",groups=which,method="bands",col="black",type="l",

panel.groups=function(x,y,group.number,...) {

	else panel.points(x,y,...)

This gives me the 3 lines per panel that I want (Median plus error bands) but what I want to add as points is instead also shown as a line with the group labels ("data" and "data.nonrandom") next to it. Any ideas what I am doing wrong? Any help and suggestions would be much appreciated! Many thanks in advance.
Best regards,

Deepayan Sarkar wrote:
> On 1/14/08, Erin Steiner <> wrote:

>> #After spending the entire day working on this question, I have
>> decided to reach out for support:
>> #I am trying to overlay a densityplot from one data set over a
>> histogram of another, if I were to plot the two individually, they
>> would look like:
>> # data frame construction
>> data.frame.A <- data.frame(rnorm(12*8), c(rep("one", 4), rep("two",
>> 4), rep("three", 4)), c("red", "orange", "yellow", "green"))
>> names(data.frame.A) <- c("vals", "factor.1", "factor.2")
>> data.frame.B <- data.frame(rnorm(12*15), c(rep("one", 4), rep("two",
>> 4), rep("three", 4)), c("red", "orange", "yellow", "green"))
>> names(data.frame.B) <- names(data.frame.A)

> The first step would be to combine the two data sources:
> df.comb <- make.groups(data.frame.A, data.frame.B)
> I would then just overlay two density plots:
> densityplot(~vals | factor.1 * factor.2, df.comb,
> groups = which, plot.points=FALSE,
> auto.key = TRUE)
> but you could do a histogram and a densityplot too:
> histogram(~vals | factor.1 * factor.2, df.comb,
> type = "density",
> groups = which,
> panel = panel.superpose,
> panel.groups = function(x, group.number, col, ...) {
> if (group.number == 1)
> panel.histogram(x, ...)
> else
> panel.densityplot(x, ..., plot.points = FALSE)
> })
> -Deepayan
> ______________________________________________
> mailing list
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.
View this message in context:
Sent from the R help mailing list archive at

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Tue 01 Apr 2008 - 16:05:34 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 01 Apr 2008 - 16:30:25 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive