2012-12-31 35 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