[R] Lattice: multiple data sets on same plot

Felix Andrews felix at nfrac.org
Fri Dec 4 04:32:18 CET 2009


As a non-statistician, here is my naive method:

densityplot(~ value | cat1 * cat2, xx, groups = paste(variable, which),
    from = 0, to = 360, bw = 100,
    panel = function(x, ...) panel.densityplot(c(x-360,x,x+360), ...),
    plot.points = FALSE, auto.key = list(columns=2),
    par.settings = simpleTheme(col = rep(c("red","blue"), each=2), lty=1:2)

Note the density is on the wrong scale now.


2009/12/4 Joel Thomas <joeliol at gmail.com>:
> Thank you! Worked perfectly.
> Is it also possible to do the same but for circular data, for example,
> a density plot of circular object from the circular package using a
> trellis display. I have the same set of data but var1 and var2 are
> azimuth directions 0-360 so a standard density plot doesn't make much
> sense.
>
> Example data set is same as before except,
>  pop <- data.frame(var1=runif(2000, 0, 360), var2=runif(2000, 0, 360))
>
> Thanks
> Joel
>
> 2009/12/3 Felix Andrews <felix at nfrac.org>:
>> 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/
>>
>



-- 
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/




More information about the R-help mailing list