问题描述
我发现coplot {graphics}
对我的情节非常有用.但是,我不仅要在其中添加一行,还要在其中添加一行.对于基本图形,我只需要add = TRUE
添加另一条线,或者使用plot(..)
和lines(..).
对于{lattice}
我可以将绘图另存为对象
I found coplot {graphics}
very useful for my plots. However, I would like to include there not only one line, but add there one another. For basic graphic I just need to add = TRUE
to add another line, or tu use plot(..)
and lines(..).
For {lattice}
I can save my plots as objects
a<-xyplot(..)
b<-xyplot(..)
并通过a + as.layer(b)
进行简单显示.这些方法中的任何一种都不适用于coplot()
,这显然是因为将对象创建为a<-coplot()
不会产生trellis
图形而是产生NULL对象.
and display it simply by a + as.layer(b)
. No one of these approaches works for coplot()
, apparently because creating objects as a<-coplot()
doesn't produce trellis
graphic but NULL object.
请帮忙如何在coplot()中添加数据行?我真的很喜欢它的图形,所以我希望保留它.谢谢!!
Please, any help how to add data line in coplot()? I really like its graphic so I wish to keep it. Thank you !!
我的示例数据在这里: http://ulozto.cz/xPfS1uRH/repr-exemple -csv
my exemle data are here: 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
请,如何合并这两个图(灰线和黑线)?我想不通...谢谢!
Please, how to merge these two plots (grey and black lines)? I couldn't figure it out... Thank you !
推荐答案
链接到示例数据并没有真正帮助.这是随机创建的样本数据集
Linking to sample data isn't really as helpful. Here's a randomly created sample data set
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()
为您设置数据子集(好吧,至少要计算索引).但是,像其他基本图形功能一样,绘制不同的组也不是一件容易的事.在这种情况下,您可以使用
I will note the coplot
is a base graphics function, not Lattice. But it does have a panel=
parameter. And you can have the coplot()
take care of subsetting your data for you (well, calculating the indexes at least). But, like other base graphics functions, plotting different groups isn't exactly trivial. You can do it in this case with
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}上添加一行,经典方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!