2012-12-31 34 views
4

这可能是一个简单的问题,但我无法找到解决方案。而不是点的情节线R

我有以下情节(我使用情节CI因为我不能用情节()填充点)。

leg<-c("1","2","3","4","5","6","7","8") 
Col.rar1<-c(rgb(1,0,0,0.7), rgb(0,0,1,0.7), rgb(0,1,1,0.7),rgb(0.6,0,0.8,0.7),rgb(1,0.8,0,0.7),rgb(0.4,0.5,0.6,0.7),rgb(0.2,0.3,0.2,0.7),rgb(1,0.3,0,0.7)) 
library(plotrix) 
plotCI(test$size,test$Mean, 
pch=c(21), pt.bg=Col.rar1,xlab="",ylab="", ui=test$Mean,li= test$Mean) 
legend(4200,400,legend=leg,pch=c(21),pt.bg=Col.rar1, bty="n", cex=1) 

enter image description here

我要创造同样的效果,但用线,而不是点(继续行)

什么建议吗?

+0

与情节我会尝试:一个< - seq(1:10); b <-a^2; plot(a,b,type ='l')我不知道type ='l'是否适用于您正在使用的软件包。 –

+1

你的例子中的'test'是什么? –

+0

测试是原始数据表(太大而无法重现) – Francisco

回答

5

当您想要根据某些分组变量生成连线时,您希望摆脱base-R图并检查出latticeggplot2。 Base-R图在xy图中没有“组”的简单概念。

一个简单的lattice例如:

library(lattice) 
dat <- data.frame(x=rep(1:5, times=4), y=rnorm(20), gp=rep(1:4,each=5)) 
xyplot(y ~ x, dat, group=gp, type='b') 

你应该可以,如果你在test类似于您定义的颜色矢量具有可变使用这样的事情。

17

你有2个解决方案:

  1. 使用的lines()函数绘制(X,Y)的位置之间的线路。
  2. 使用plottype = "l"像线

很难表现出来,而不重复的例子,但你可以例如做:

Col.rar1<-c(rgb(1,0,0,0.7), rgb(0,0,1,0.7), rgb(0,1,1,0.7),rgb(0.6,0,0.8,0.7),rgb(1,0.8,0,0.7),rgb(0.4,0.5,0.6,0.7),rgb(0.2,0.3,0.2,0.7),rgb(1,0.3,0,0.7)) 

x <- seq(0, 5000, length.out=10) 
y <- matrix(sort(rnorm(10*length(Col.rar1))), ncol=length(Col.rar1)) 
plot(x, y[,1], ylim=range(y), ann=FALSE, axes=T,type="l", col=Col.rar1[1]) 

lapply(seq_along(Col.rar1),function(i){ 
    lines(x, y[,i], col=Col.rar1[i]) 
    points(x, y[,i]) # this is optional 
}) 

enter image description here

+0

OP提到实心圆圈;可能值得一提的是'pch = 21','col'和'bg'参数指向'points'。 – drammock

+0

@drammock也许..但为什么? OP要求线条而不是点(因此这里的点是可选的)并且OP接受了一个格点的答案。 – agstudy