Reputation: 95
I am trying to add several loess line to each panel in lattice plot. Each loess line represent the different level of Spe column. Here is the link the my data set:
https://gist.github.com/plxsas/4756fc8d8e50f62acf4d
Would you be able to help me, please?
my.col1<- c("white", "darkgray", "black", "lightgray", "ivory2")
my.col2<- c("white", "darkgray", "black", "lightgray", "ivory2")
labels<- c("H", "A", "E", "Q", "T")
xyplot(Total~Months|Site,data=data, groups=Spe, layout=c(3,1), index.cond=list(c(1,2,3)),
par.settings = list(superpose.polygon = list(col=c(my.col1, my.col2))), superpose.line=list(col=c(my.col1, my.col2)),
ylab="Individuals", xlab="Months",
scales=list(x=list(rot=90, alternating=1,labels=c("Jan-12", "Feb-12", "Mar-12", "Apr-12", "May-12", "Jun-12",
"Jul-12", "Aug-12", "Sep-12", "Oct-12", "Nov-12", "Dec-12", "Jan-13"))),
auto.key=list(space="top", columns=3, cex=.8,between.columns = 1,font=3,
rectangles=FALSE, points=TRUE, labels=labels),
panel = function(x, y, ...){
panel.xyplot(x, y, ...)
panel.loess(x, y, span = 1/2)
})
Upvotes: 1
Views: 1141
Reputation: 206586
As @BondedDust pointed out, the panel.superpose
function is helpful here For example
#sample data
data<-expand.grid(Months=1:13, Site=paste("Site", 1:3), Spe=labels)
data$Total<-sort(runif(nrow(data), 100,10000))+rnorm(nrow(data),50, 20)
my.col1<- c("white", "darkgray", "black", "lightgray", "ivory2")
my.col2<- c("white", "darkgray", "black", "lightgray", "ivory2")
mlabels<- c("Jan-12", "Feb-12", "Mar-12", "Apr-12", "May-12", "Jun-12",
"Jul-12", "Aug-12", "Sep-12", "Oct-12", "Nov-12", "Dec-12", "Jan-13")
labels<- c("H", "A", "E", "Q", "T")
xyplot(Total~Months|Site,data=data, groups=Spe,
layout=c(3,1), index.cond=list(c(1,2,3)),
par.settings = list(
superpose.polygon = list(col=c(my.col1, my.col2))),
superpose.line=list(col=c(my.col1, my.col2)),
ylab="Individuals", xlab="Months",
scales=list(x=list(rot=90, alternating=1,labels=mlabels)),
auto.key=list(space="top", columns=3, cex=.8,between.columns = 1,font=3,
rectangles=FALSE, points=TRUE, labels=labels),
panel = panel.superpose,
panel.groups = function(x,y,...) {
panel.xyplot(x, y, ...)
panel.loess(x, y, ...)
}
)
With this method, you set your main panel function to panel.superpose
then you set a panel.groups
parameter that gets passed to panel.superpose
for each group. The panel.superpose
takes care of figuring out the right color for each group and passing the correct x
and y
values. So you just put in the things you would like to plot and pass though everything that panel.superpose
calculated for you in the ...
's.
Upvotes: 6