gpt4 book ai didi

R - 图例 : assign multiple colours to the same text

转载 作者:行者123 更新时间:2023-12-04 10:36:29 25 4
gpt4 key购买 nike

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

最佳答案

您可以准确地控制图例:

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

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

barplot figure

关于R - 图例 : assign multiple colours to the same text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5169265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com