在 R 中的绘图中分组
Sub-grouping in a plot in R
我想在 xyplot 中表示来自不同单个单元格的数据,然后在不同类别的基础上为它们赋予颜色。但是,当我能够用点表示时:
xyplot( signal ~ time | as.factor(treatment), data=data,groups=cell,
fill.color = as.character(data$color),
panel = function(x, y,fill.color,...,subscripts){
fill = fill.color [subscripts]
panel.xyplot(x, y,pch=19, col=fill, type ="p")}
)
但这种方式无法直观地追踪细胞。因此,我想对线和多边形作为错误区域做同样的事情,但每次我尝试时,格子都会覆盖应该分配颜色的第二组(或者我无法正确地将它告诉格子。这是我的方法:
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)))
谢谢;
三体
感谢@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)
结果如下:
我想在 xyplot 中表示来自不同单个单元格的数据,然后在不同类别的基础上为它们赋予颜色。但是,当我能够用点表示时:
xyplot( signal ~ time | as.factor(treatment), data=data,groups=cell,
fill.color = as.character(data$color),
panel = function(x, y,fill.color,...,subscripts){
fill = fill.color [subscripts]
panel.xyplot(x, y,pch=19, col=fill, type ="p")}
)
但这种方式无法直观地追踪细胞。因此,我想对线和多边形作为错误区域做同样的事情,但每次我尝试时,格子都会覆盖应该分配颜色的第二组(或者我无法正确地将它告诉格子。这是我的方法:
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)))
谢谢; 三体
感谢@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)
结果如下: