I am plotting a table with the values below:
| No | Sector | Amount USD (million) |
|---|---|---|
| 1 | Air Pollution | 134 |
| 2 | Water Resources | 3,158 |
| 3 | Land Use and Land Use Change | 91 |
| 4 | Biodiversity | 74 |
| 5 | Agriculture | 2,386 |
| 6 | Industry and Commerce | 528 |
| 7 | Mining | 312 |
| 8 | Tourism | 252 |
| 9 | Energy | 262 |
| 10 | Transport | 1,071 |
| 11 | Disaster Risk Management and Human Settlements | 519 |
| 12 | Waste Management | 231 |
| 13 | Health | 52 |
| 14 | Gender; People Living With HIV and AIDS and other Vulnerable groups | 25 |
| 15 | Children and Youth | 32 |
| 16 | Capacity Building | 45 |
| 17 | Role of Meteorological Services | 23 |
| 18 | Technology Transfer | 574 |
| 19 | Climate Change Education | 15 |
| 20 | Public Awareness | 15 |
| 21 | Climate Change Governance | 88 |
Once done, I put in the following code in R studio
#reading the table into R studio
zim5 <- read_excel ('zimbabwe-climate-finance-excel4.xlsx', col_names = TRUE)
zim5
#creating the objects for easier typing
sector1 <- zim5$Sector
sector1
millions1 <- zim5$'Amount USD (million)'
millions1
#finally plotting the barplot with a legend
mycol <- colors()[444:500]
barplot(millions1~sector1, width = 4, space = 0.2, main = 'Zimbabwe budget by sector', xlab = 'Economic sectors', ylab = 'Amount in US $ millions',
col = mycol, cex.names = 0.6, horiz = F, las = 2)
legend('top', sector1, fill = mycol, cex = 0.7, text.font = 3)
My aim was to get a barplot with a legend. However, although I managed to create the barplot (including a different one with horizontal bars (horiz = T and las = 1), my legend always seems to display the text labels in a different order than in the barplot in both. i.e., Air pollution corresponds with the fill of Agriculture in the graphic and so on. 
Though I noticed R studio seems to balance out the position of the bars according to their heights or something, I want the legend labels to correspond with their appropriate fill colours like in the x axis of the plot, whether or not R studio orders the bars differently from the table.
CodePudding user response:
Here it is - turning the label into a factor solves the problem.
z <- structure(list(no = 1:21, sector = c("Air Pollution", "Water Resources",
"Land Use and Land Use Change", "Biodiversity", "Agriculture",
"Industry and Commerce", "Mining", "Tourism", "Energy", "Transport",
"Disaster Risk Management and Human Settlements", "Waste Management",
"Health", "Gender; People Living With HIV and AIDS and other Vulnerable groups",
"Children and Youth", "Capacity Building", "Role of Meteorological Services",
"Technology Transfer", "Climate Change Education", "Public Awareness",
"Climate Change Governance"), amt = c(134, 3158, 91, 74, 2386,
528, 312, 252, 262, 1071, 519, 231, 52, 25, 32, 45, 23, 574,
15, 15, 88)), row.names = c(NA, 21L), class = "data.frame")
z$lab <- factor(z$no, labels = z$sector)
mycol <- colors()[444:500]
barplot(amt~lab, data = z, width = 4, space = 0.2, main = 'Zimbabwe budget by sector', xlab = 'Economic sectors', ylab = 'Amount in US $ millions',
col = mycol, cex.names = 0.6, horiz = F, las = 2)
legend('topright', legend=z$lab, fill = mycol, cex = 0.35, text.font = 3)
CodePudding user response:
barplot has builtin arguments for the legend, why not using them? Next, for the data using the height= argument is probably better, so the bars appear in the same order as in the table, use beside instead of horiz then; you can use names.arg= for the labels then, but they are actually superfluous because of the legend.
You definitely need more distinguishable colors, I use rainbow here.
Next, for the legend, just define legend.text=, and in args.legend= just put all the arguments you are used to from legend() in a list.
For better consistency I use the png device and not the one from Rstudio, which I think is more of a preview window.
Et voilá!
png('Rplot001.png', 800, 600)
barplot(z$am, beside=TRUE, col=rainbow(nrow(z), s=.6, v=.8),
names.arg=z$sector, ## just for demo, should be deleted
legend.text=z$sector, args.legend=list(x='topright', cex=.9),
width=4, space=0.2, main='Zimbabwe budget by sector',
xlab='Economic sectors', ylab='Amount in US $ millions',
cex.names=0.6, las=2)
dev.off()
Note: If you really want nice labels, check out my other answer.


