R - legend: assign multiple colours to the same text
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,
开发者_如何学Python 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")
)
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.
精彩评论