2015-08-18 54 views
0

我想繪製一些情節和每個分段取決於分數我想繪製顏色作爲從藍色( - 值) - 白色(0值) - 紅色(+值)。 現在使用我的代碼的Segments部分我可以做到這一點,如果我給出一個特定的顏色,如col="red" or col="blue",它很好。所以,由於我的數據的龐大規模,我想用那麼自動化它:R調色板不打印指定的顏色

rbPal <- colorRampPalette(c('red','white','blue')) 
jcolor<-rbPal(nrow(datasetsize)) 

的問題是,當我到達繪圖數據,它只是繪製顏色隨機,而不是我一直在使用的顏色分配的顏色即使檢查html顏色代碼,調色板也是正確的顏色。任何幫助將不勝感激。 (請參見下面的代碼)

測試數據:

Sample loc Start End p sm 
S1 9 1000000 10000000 2 -7.5751 
S2 9 11000000 18000000 6 -1.5906 
S3 9 20000000 40000000 3 0 
S4 9 50000000 70000000 2 0.8 
S5 9 80000000 100000000 2 1.25 
S6 9 110000000 140000000 6 7.789 

代碼:

### Read in Data 
data<-read.table("stack_eg.txt",header=TRUE,sep="\t") 

### Order Setdata Object 
data<-data[order(data$sm,decreasing=T),] 

### Setup Plot Space 
plot(1, type="n", axes=T, xlab="X", ylab="Y",ylim=c(1,6),xlim=c(0,142000000)) 

### Color setup 
rbPal <- colorRampPalette(c('red','white','blue')) 
jcolor<-rbPal(6) 
jonzo<-cbind(data,jcolor) 

### Plot data 
for(l in 1:nrow(jonzo)){ 
    startpos<-jonzo$Start[l] 
    endpos<-jonzo$End[l] 
    segments(startpos,2,endpos,2,col=jonzo$jcolor[l]) 
} 

我得到的圖像像這樣: enter image description here

而是這樣一個形象: enter image description here

+0

感謝小費。我注意到,從jonzo讀取顏色代碼似乎是一個問題。如果我刪除它,只需從jcolor中拉出顏色,效果很好。 – Jcrow06

回答

0

好了,所以它看起來像這個問題是在下面的章節:

jonzo<-cbind(data,jcolor) 

這從讀取顏色代碼#FF0000列綁定的文件。 如果單純從該jcolor對象,然後它的工作原理和下面的圖像產生閱讀的顏色代碼: enter image description here

下面是對於那些有興趣的調整代碼:

### Read in Data 
data<-read.table("stack_eg.txt",header=TRUE,sep="\t") 

### Order Setdata Object 
data<-data[order(data$sm,decreasing=T),] 

### Setup Plot Space 
plot(1, type="n", axes=T, xlab="X", ylab="Y",ylim=c(1,6),xlim=c(0,142000000)) 

### Color setup 
rbPal <- colorRampPalette(c('red','white','blue')) 
jcolor<-rbPal(6) 

### Plot data 
for(l in 1:nrow(jonzo)){ 
    startpos<-jonzo$Start[l] 
    endpos<-jonzo$End[l] 
    segments(startpos,2,endpos,2,col=jcolor[l]) 
}