6

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

1 回答 1

6

您可以完全控制图例:

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

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

条形图

于 2011-03-02T15:06:16.420 回答