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

Christoph Meyer christoph.meyer at uni-ulm.de
Tue Apr 1 22:34:20 CEST 2008

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)



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 <steiner.erin at gmail.com> 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
> ______________________________________________
> 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.

View this message in context: http://www.nabble.com/Overlay-plots-from-different-data-sets-using-the-Lattice-package-tp14824421p16418703.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list