Mark Dalphin
2010-Dec-15 22:46 UTC
[R] Problems drawing a colored 'rug' in the Lattice 'densityplot'
Hi All, I'm trying to add a 'rug' representation of my data to a plot created with densityplot(). While I can do this in the simple case, I can't do it properly when I include the "groups" argument. I have an example below. I am running a reasonably new version of R. print(sessionInfo()) R version 2.12.0 Patched (2010-11-07 r53537) Platform: i686-pc-linux-gnu (32-bit) locale: [1] LC_CTYPE=en_NZ.UTF-8 LC_NUMERIC=C LC_TIME=en_NZ.UTF-8 LC_COLLATE=en_NZ.UTF-8 [5] LC_MONETARY=C LC_MESSAGES=en_NZ.UTF-8 LC_PAPER=en_NZ.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_NZ.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] lattice_0.19-13 R code to show the problem: ## Setup - load package, set random seed & create fake dataset library(lattice) set.seed(1234) d <- data.frame(Type=rep(LETTERS[1:4], times=250), Category=rep(LETTERS[22:26], times=200), Value=c(rnorm(500), rnorm(300, 0.5), rnorm(200, 1)) ) ## Basic "densityplot" using 'points' to show the data densityplot(~Value|Type, data=d) ## And I can plot a 'rug' for the simple density plot densityplot(~Value|Type, data=d, plot.points='rug') ## Now add a "groups" selector to show sub-grouping of data by 'Category' ## Note: the data points are in color densityplot(~Value|Type, group=Category, data=d) ## Finally, with the groups, and with a rug. ## Note: no color for the rug densityplot(~Value|Type, group=Category, data=d, plot.points='rug') So, I can draw a rug (which is an improvement over version 2.9.1 of R when I got no rug), however, the color associated with the 'group' doesn't seem to propagate through to the rug. Is there something I am doing wrong here or is this a bug? Anyone have suggestions to work around this? Regards, Mark --
Phil Spector
2010-Dec-15 23:35 UTC
[R] Problems drawing a colored 'rug' in the Lattice 'densityplot'
Mark - If I understand what you want, it can be done with a custom panel function: mypanel = function(x,subscripts,groups,...){ panel.densityplot(x,plot.points=FALSE,groups=groups,subscripts=subscripts,...) panel.rug(x,col=trellis.par.get('superpose.line')$col[groups[subscripts]]) } Then I think you'll get the result you want if you use densityplot(~Value|Type, group=Category, data=d, panel=mypanel) - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Thu, 16 Dec 2010, Mark Dalphin wrote:> Hi All, > > I'm trying to add a 'rug' representation of my data to a plot created with > densityplot(). While I can do this in the simple case, I can't do it properly > when I include the "groups" argument. I have an example below. I am running a > reasonably new version of R. > > print(sessionInfo()) > R version 2.12.0 Patched (2010-11-07 r53537) > Platform: i686-pc-linux-gnu (32-bit) > > locale: > [1] LC_CTYPE=en_NZ.UTF-8 LC_NUMERIC=C LC_TIME=en_NZ.UTF-8 > LC_COLLATE=en_NZ.UTF-8 [5] LC_MONETARY=C > LC_MESSAGES=en_NZ.UTF-8 LC_PAPER=en_NZ.UTF-8 LC_NAME=C > [9] LC_ADDRESS=C LC_TELEPHONE=C > LC_MEASUREMENT=en_NZ.UTF-8 LC_IDENTIFICATION=C > attached base packages: > [1] stats graphics grDevices utils datasets methods base > other attached packages: > [1] lattice_0.19-13 > > R code to show the problem: > > ## Setup - load package, set random seed & create fake dataset > library(lattice) > set.seed(1234) > d <- data.frame(Type=rep(LETTERS[1:4], times=250), > Category=rep(LETTERS[22:26], times=200), > Value=c(rnorm(500), rnorm(300, 0.5), rnorm(200, 1)) > ) > > ## Basic "densityplot" using 'points' to show the data > densityplot(~Value|Type, data=d) > > ## And I can plot a 'rug' for the simple density plot > densityplot(~Value|Type, data=d, plot.points='rug') > > ## Now add a "groups" selector to show sub-grouping of data by 'Category' > ## Note: the data points are in color > densityplot(~Value|Type, group=Category, data=d) > > ## Finally, with the groups, and with a rug. > ## Note: no color for the rug > densityplot(~Value|Type, group=Category, data=d, plot.points='rug') > > > So, I can draw a rug (which is an improvement over version 2.9.1 of R when I > got no rug), however, the color associated with the 'group' doesn't seem to > propagate through to the rug. Is there something I am doing wrong here or is > this a bug? Anyone have suggestions to work around this? > > Regards, > Mark > -- >