Using planel.groups in lattice
[This article was first published on mages' blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Last Tuesday I attended the LondonR user group meeting, where Rich and Andy from Mango argued about the better package for multivariate graphics with R: lattice vs. ggplot2. Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
As part of their talk they had a little competition in visualising London Underground performance data, see their slides. Both made heavy use of the respective panelling / faceting capabilities. Additionally Rich used the
panel.groups
argument of xyplot
to fine control the content of each panel. Brilliant! I had never used this argument before. So, here is a silly example with the iris
data set to remind myself of panel.groups
in the future.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(lattice) | |
my.settings <- list( | |
superpose.symbol=list(pch=19), | |
plot.line=list(col="black"), | |
plot.polygon=list(col=adjustcolor(col="steelblue", | |
alpha.f=0.5), | |
border="white") | |
) | |
xyplot(Sepal.Length ~ Sepal.Width, groups=Species, data=iris, | |
main="Lattice example with panel.groups", | |
par.settings = my.settings, | |
panel=panel.superpose, | |
panel.groups=function(x,y, group.number,...){ | |
specie <- levels(iris$Species)[group.number] | |
if(specie %in% "setosa"){ | |
panel.xyplot(x,y,...) | |
panel.abline(lm(y~x)) | |
} | |
if(specie %in% "versicolor"){ | |
panel.barchart(x, y) | |
} | |
if(specie %in% "virginica"){ | |
panel.rug(x, y) | |
} | |
} | |
) |
To leave a comment for the author, please follow the link and comment on their blog: mages' blog.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.