我想在xyplot中表示来自不同单个单元格的数据,然后根据不同的类别为它们提供颜色。但是,当我能用点表示这个时:
cell<-rep(x = c("A","B","C","D"),50)
signal<-rep(sample(seq(from = 0, to = 50, by = 1), size = 50, replace = TRUE),4)
time<-sort(rep(seq(1,50),4),decreasing = F)
treatment<-rep(c("hard","soft"),50*2)
color<-rep(c("red","orange"),50*2)
data<-data.frame(cell,signal,time,treatment,color)
my.panel2 <- function(x, y, subscripts, col, pch,cex,sd,fill.color,...) {
low95 <- y-sd[subscripts]
up95 <- y+sd[subscripts]
fill=fill.color [subscripts]
panel.xyplot(x, y, col=fill.color, pch=pch,cex=cex, ...)
panel.arrows(x, low95, x, up95, angle=90, code=3,lwd=3,
length=0.05, alpha=0.2,col=col)
}
xyplot(signal~time|as.factor(treatment), groups=as.factor((data$cell)),
data=data, type='l',
color.line=as.character((data$color)))
但是以这种方式在视觉上跟踪细胞是不可能的。因此,我想对线条和多边形做同样的错误区域,但每次尝试时都会发生格子覆盖应该分配颜色的第二组(或者我无法正确地告诉格子。这是我的这样做的方法:
{{1}}
感谢; 桑蒂
答案 0 :(得分:1)
感谢@Vince,他给了我一个有趣的想法。 ggplot2
基于图层,因此我决定使用as.layer()
在格子中尝试类似的内容。首先,我将我的数据分成三组,每个级别一个
fm1<-filter(fmeans, group=="Ave-int")
fm2<-filter(fmeans, group=="Mini-int")
fm3<-filter(fmeans, group=="High-int")
line1<-fm2[43,]
fm3<-rbind(fm3,line1)
然后我决定使用多边形作为错误带使用panel.polygon()
:
my.panel.pol <- function(x, y, subscripts, col,sd,...) {
plot.line <- trellis.par.get("plot.line")
xs <- if(is.factor(x)) {
factor(c(levels(x) , rev(levels(x))), levels=levels(x))
} else {
xx <- sort(unique(x))
c(xx, rev(xx))
}
low95 <- y-sd[subscripts]
up95 <- y+sd[subscripts]
panel.xyplot(x, y, col=col,...)
panel.polygon(xs, c(up95, rev(low95)), col=col, alpha=0.2, border=F)
}
然后表示所有数据并合并到一个图形
a<-
xyplot(Mean ~ slice*12 |treatment , fm1,
layout=c(2,2),col="red",
grid=T,
ylim = c(0,max(fmeans$Mean)),
group = stemcell, type = "l",
sd=fm1$sd,
panel.groups= "my.panel.pol",
panel="panel.superpose")
b<-xyplot(Mean ~ slice*12 |treatment , fm2,
layout=c(2,2),col="blue",
group = stemcell, type = "l",
sd=fm2$sd,
panel.groups= "my.panel.pol",
panel="panel.superpose")
c<-xyplot(Mean ~ slice*12 |treatment , fm3,
layout=c(2,2),col="green",
group = stemcell, type = "l",
sd=fm3$sd,
panel.groups= "my.panel.pol",
panel="panel.superpose")
a+as.layer(b)+as.layer(c)
所以结果如下: