在 coplot {graphics} 中添加一条线,经典方法不起作用
Add a line to coplot {graphics}, classic approaches don't work
我发现 coplot {graphics}
对我的情节非常有用。但是,我想不仅包括一行,还想在那里添加另一行。对于基本图形,我只需要 add = TRUE
添加另一行,或者你使用 plot(..)
和 lines(..).
对于 {lattice}
我可以将我的绘图保存为对象
a<-xyplot(..)
b<-xyplot(..)
并通过a + as.layer(b)
简单显示。这些方法都不适用于 coplot()
,显然是因为将对象创建为 a<-coplot()
不会生成 trellis
图形,而是生成 NULL 对象。
请帮忙 如何在 coplot() 中添加数据线? 我真的很喜欢它的图形,所以我想保留它。谢谢!!
我的示例数据在这里:http://ulozto.cz/xPfS1uRH/repr-exemple-csv
我的代码:
sub.tab<-read.csv("repr_exemple.csv", , header = T, sep = "")
attach(sub.tab)
cells.f<-factor(cells, levels=c(2, 25, 100, 250, 500), # unique(cells.in.cluster)???
labels=c("size2", "size25", "size100", "size250", "size500"))
perc.f<-factor(perc, levels=c(5, 10), # unique(cells.in.cluster)???
labels=c("perc5", "perc10"))
# how to put these plots together?
a<- coplot(max_dist ~ time |cells.f + perc.f, data = sub.tab,
xlab = "ticks", type = "l", col = "black", lwd = 1)
b<- coplot(mean_dist ~ time |cells.f * perc.f, data = sub.tab,
xlab = "ticks", type = "l", col = "grey", lwd = 1)
a + as.layer(b) # this doesn't work
请问如何合并这两个图(灰线和黑线)?我想不通...谢谢!
链接到样本数据并没有多大帮助。这是一个随机创建的示例数据集
set.seed(15)
dd <- do.call("rbind",
do.call("Map", c(list(function(a,b) {
cbind.data.frame(a,b, x=1:5,
y1=cumsum(rpois(5,7)),
y2=cumsum(rpois(5,9)))
}),
expand.grid(a=letters[1:5], b=letters[20:22])))
)
head(dd)
# a b x y1 y2
# 1 a t 1 8 16
# 2 a t 2 13 28
# 3 a t 3 25 35
# 4 a t 4 33 45
# 5 a t 5 39 57
# 6 b t 1 4 12
我会注意到 coplot
是基本图形函数,不是 Lattice。但它确实有一个 panel=
参数。您可以让 coplot()
为您处理数据子集(好吧,至少计算索引)。但是,与其他基本图形函数一样,绘制不同的组也并非易事。在这种情况下,您可以使用
coplot(y~x|a+b,
# make a fake y col to cover range of all y1 and y2 values
cbind(dd, y=seq(min(dd$y1, dd$y2), max(dd$y1, dd$y2), length.out=nrow(dd))),
#request subscripts to be sent to panel function
subscripts=TRUE,
panel=function(x,y,subscripts, ...) {
# draw group 1
lines(x, dd$y1[subscripts])
# draw group 2
lines(x, dd$y2[subscripts], col="red")
})
这给出了
我发现 coplot {graphics}
对我的情节非常有用。但是,我想不仅包括一行,还想在那里添加另一行。对于基本图形,我只需要 add = TRUE
添加另一行,或者你使用 plot(..)
和 lines(..).
对于 {lattice}
我可以将我的绘图保存为对象
a<-xyplot(..)
b<-xyplot(..)
并通过a + as.layer(b)
简单显示。这些方法都不适用于 coplot()
,显然是因为将对象创建为 a<-coplot()
不会生成 trellis
图形,而是生成 NULL 对象。
请帮忙 如何在 coplot() 中添加数据线? 我真的很喜欢它的图形,所以我想保留它。谢谢!!
我的示例数据在这里:http://ulozto.cz/xPfS1uRH/repr-exemple-csv
我的代码:
sub.tab<-read.csv("repr_exemple.csv", , header = T, sep = "")
attach(sub.tab)
cells.f<-factor(cells, levels=c(2, 25, 100, 250, 500), # unique(cells.in.cluster)???
labels=c("size2", "size25", "size100", "size250", "size500"))
perc.f<-factor(perc, levels=c(5, 10), # unique(cells.in.cluster)???
labels=c("perc5", "perc10"))
# how to put these plots together?
a<- coplot(max_dist ~ time |cells.f + perc.f, data = sub.tab,
xlab = "ticks", type = "l", col = "black", lwd = 1)
b<- coplot(mean_dist ~ time |cells.f * perc.f, data = sub.tab,
xlab = "ticks", type = "l", col = "grey", lwd = 1)
a + as.layer(b) # this doesn't work
请问如何合并这两个图(灰线和黑线)?我想不通...谢谢!
链接到样本数据并没有多大帮助。这是一个随机创建的示例数据集
set.seed(15)
dd <- do.call("rbind",
do.call("Map", c(list(function(a,b) {
cbind.data.frame(a,b, x=1:5,
y1=cumsum(rpois(5,7)),
y2=cumsum(rpois(5,9)))
}),
expand.grid(a=letters[1:5], b=letters[20:22])))
)
head(dd)
# a b x y1 y2
# 1 a t 1 8 16
# 2 a t 2 13 28
# 3 a t 3 25 35
# 4 a t 4 33 45
# 5 a t 5 39 57
# 6 b t 1 4 12
我会注意到 coplot
是基本图形函数,不是 Lattice。但它确实有一个 panel=
参数。您可以让 coplot()
为您处理数据子集(好吧,至少计算索引)。但是,与其他基本图形函数一样,绘制不同的组也并非易事。在这种情况下,您可以使用
coplot(y~x|a+b,
# make a fake y col to cover range of all y1 and y2 values
cbind(dd, y=seq(min(dd$y1, dd$y2), max(dd$y1, dd$y2), length.out=nrow(dd))),
#request subscripts to be sent to panel function
subscripts=TRUE,
panel=function(x,y,subscripts, ...) {
# draw group 1
lines(x, dd$y1[subscripts])
# draw group 2
lines(x, dd$y2[subscripts], col="red")
})
这给出了