R - 图例:为同一文本分配多种颜色

发布于 2024-10-19 19:41:40 字数 1265 浏览 5 评论 0原文

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

I have an R barplot that has six bars divided into two parts each, and six colours - but several of the colours mean the same thing. How can I assign one text to several colours in the legend? Thanks in advance for your help!!!

Here's the R code for the plot:

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")
        )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吝吻 2024-10-26 19:41:40

您可以精确控制图例:

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

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

条形图

You control the legend exactly:

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

You'll need to create some space for the legend or push it outside the plotting region, but you control which colours appear and what text they are associated with.

barplot figure

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文