在格子图中为每个面板添加几条黄土线

时间:2014-05-22 10:51:36

标签: r lattice

我正在尝试在格子图中为每个面板添加几条黄土线。每条黄土线代表不同级别的Spe列。这是我的数据集的链接:

https://gist.github.com/plxsas/4756fc8d8e50f62acf4d

请问你能帮帮我吗?

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)
   })

1 个答案:

答案 0 :(得分:4)

正如@BondedDust指出的那样,panel.superpose函数在这里很有用例如

#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, ...)
    }
)

使用此方法,您可以将主面板功能设置为panel.superpose,然后为每个组设置一个传递给panel.groups的{​​{1}}参数。 panel.superpose负责确定每个组的正确颜色并传递正确的panel.superposex值。所以你只需要输入你想要绘制的内容,然后传递ypanel.superpose中为你计算的所有内容。

output plot