On Jul 27, 2011, at 9:15 AM, Sébastien Bihorel wrote:

Hi

I would like to superimpose group-specific densityplots on top of an overall histogram using panel.histogram and panel.densityplot. Furthermore, I would like to automatically adjust the range of the y-axis to take into account the ranges of both histogram and densityplot. This last part is where I have a problem. I believe using the prepanel argument of histogram is typically the way to go, but I did not have very much success with my attempts. Is this the way to go, or should I calculate the range outside the histogram call using some stat function (density?) on each group and use it as a ylim
argument?

Any advise would be greatly appreciated.

Thank you

Sebastien

require(lattice)
set.seed(12345)

foo1 <- data.frame(x=rnorm(100,0,0.1),grp=1)
foo2 <- data.frame(x=rnorm(100,2,1),grp=2)

foo <- rbind(foo1,foo2)

#
# Some code to calculate ylim...
#

histogram(~x,data=foo,groups=grp,
 #prepanel=function(x,type,groups,...){???},
 panel=function(x,type,groups,...){
   panel.histogram(x,col='transparent',...)
   panel.densityplot(x,groups=groups,...)
 },
 #ylim=ylim,
 type='density')

Use densityplot instead of histogram as the wrapping function so its more extreme ranges are respected. You do get an error when you do that saying that 'breaks' is invalid, but if you read the ?histogram page, it suggests that setting breaks=NULL might produce acceptable default behavior, and that seems to be so in this case:

densityplot(~x,data=foo,groups=grp,
 #prepanel=function(x,type,groups,...){???},
 panel=function(x,type,groups,...){
         panel.densityplot(x,groups=groups,...)
         panel.histogram(x,col='transparent', breaks = NULL, ...)

 } )


        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

David Winsemius, MD
West Hartford, CT

______________________________________________
R-help@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.

Reply via email to