R - 图例:为同一文本指定多种颜色

时间:2011-03-02 14:56:49

标签: r plot legend bar-chart

我有一个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")
        )

1 个答案:

答案 0 :(得分:6)

您完全控制了图例:

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

您需要为图例创建一些空间或将其推到绘图区域之外,但您可以控制显示的颜色以及与之关联的文本。

barplot figure