library(reshape) ## the easy way
xx <- melt(x)
head(xx)
densityplot(~ value | cat1 * cat2, xx, groups = paste(variable, which),
plot.points=FALSE, auto.key=list(columns=2),
par.settings = simpleTheme(col =
c("red","blue"), lty=c(1,1,2,2)))
Hint: using multiple terms in the formula as you did actually maps
into the "groups" argument. It is generally not a good a idea to do
that when you also specify "groups" directly.
2009/12/3 Joel Thomas <joeliol at gmail.com>:> Hi,
> I have two data sets; one is a population and the other a sample of
> that population. I am trying to plot both on the same trellis display.
>
> # Example data set with two numerical attributes and two categorical
> pop <- data.frame(var1=rnorm(2000, 2000, 500), var2=rnorm(2000, 2000,
500))
>
> cat<-(runif(2000)<=.5)+0
> for(i in 1:length(cat)){
> ? if(cat[i] == 0){
> ? ? ?pop[i,"cat1"] = "this"
> ? }
> ? else{
> ? ? ?pop[i,"cat1"] = "that"
> ? }
> }
> cat<-(runif(2000)<=.5)+0
> for(i in 1:length(cat)){
> ? if(cat[i] == 0){
> ? ? ?pop[i,"cat2"] = "here"
> ? }
> ? else{
> ? ? ?pop[i,"cat2"] = "there"
> ? }
> }
>
> #Extract sample
> sam <- pop[sample(1:2000,50),]
>
> #Combine data sets
> x <- make.groups(pop, sam)
>
> #Create trellis display
> densityplot(~var1 + var2 | cat1 * cat2, data=x, groups=which,
> plot.points=FALSE, auto.key=list(columns=2))
>
>
> This does not produce what I want. I would like four density plots in
> each panel; var1 and var2 from the sample data as solid lines and var1
> and var2 from the population data as dotted lines with matching
> colours.
>
> densityplot(~ var1 + var2 | cat1 * cat2, data=x, groups=which,
> ? ? ?panel=panel.superpose,
> ? ? ?panel.groups = function(x, y,...){
> ? ? ? ?panel.densityplot(x, ...)
> ? ? ? ?})
>
> ... gives the same result.
>
> I've read "The panel function demystified" section of the
book many
> times and still find the panel function mystifying. Could someone
> please explain how to do this and also how it works. Thankyou
>
> ______________________________________________
> 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.
>
--
Felix Andrews / ???
Postdoctoral Fellow
Integrated Catchment Assessment and Management (iCAM) Centre
Fenner School of Environment and Society [Bldg 48a]
The Australian National University
Canberra ACT 0200 Australia
M: +61 410 400 963
T: + 61 2 6125 4670
E: felix.andrews at anu.edu.au
CRICOS Provider No. 00120C
--
http://www.neurofractal.org/felix/