2011-03-02 28 views
6

我有一個R條形圖,有六個條形,每個條形分爲兩個部分,還有六種顏色 - 但其中幾種顏色表示相同的東西。我如何將一個文本分配給圖例中的多種顏色?在此先感謝您的幫助!!!R - 傳奇:給同一個文字分配多種顏色

這裏有一個情節將R代碼:

height<-matrix(c(64.39173921,73.08486662, 
       64.25261321,90.70965993, 
       64.91170611,38.21450776, 
       35.60826079,26.91513338, 
       35.74738679,9.290340073, 
       35.08829389,61.78549224), 
       nrow=2, ncol=6, byrow=TRUE, 
       dimnames=list(c("Bilateral", "Multilateral"), 
          c("GER \ntotal", "GER \nto LA", "ESP \ntotal", 
           "ESP \nto LA", "UK \ntotal", "UK \nto LA"))) 

tmp <- height 
height <- matrix(0,nrow=12,ncol=6) 
height[cbind(1:12,rep(1:6,each=2))] <- tmp 

colnames(height) <- colnames(tmp) 
rownames(height) <- rep(rownames(tmp),6) 


barplot(height, beside=FALSE, 
     main="Bilateral vs. Multilateral Aid 2004-8 average", 
     ylab="Percentage of aid", ylim=c(0,100), 
     col=c("deepskyblue4","deepskyblue", 
       "deepskyblue4","deepskyblue", 
       "darkolivegreen4","darkolivegreen1", 
       "darkolivegreen4","darkolivegreen1", 
       "firebrick4", "firebrick1", 
       "firebrick4", "firebrick1") 
     ) 

回答

6

您可精確控制的傳說:

legend("bottom", 
     legend = c("Bilateral Aid","","","Other","",""), 
     fill = c("deepskyblue4","darkolivegreen4","firebrick4","deepskyblue", 
       "darkolivegreen1","firebrick1"), 
     bg = "white", ncol = 2) 

你需要創建一些空間圖例或推它的繪圖區域外,但是您可以控制顯示哪些顏色以及與哪些文本相關聯。

barplot figure

+0

這將工作,如果每種顏色意味着不同的東西。問題是,較深的顏色(deepskyblue4,darkolivegreen4,firebrick4)意味着同樣的事情,即「雙邊援助份額」,較輕的意味着「援助的多邊份額」 - 現在我想將三種顏色分別綁定到一個文字...不知道這是否合理,並解釋我想要做什麼? – Bettina 2011-03-02 15:15:23

+0

@Bettina是的,你的Q沒有這樣讀過。我會考慮編輯。 – 2011-03-02 15:24:04

+0

@Bettina,現在編輯好的版本有用嗎? – 2011-03-02 15:27:38