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")
)
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以精确控制图例:
您需要为图例创建一些空间或将其推到绘图区域之外,但您可以控制显示的颜色以及与它们关联的文本。
You control the legend exactly:
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.