Displaying values on top of or next to the bars

Sometimes, it is useful to have the exact values displayed on a bar chart to enable quick and accurate reading. There is no built-in function in R to do this. In this recipe, we will learn how to do this by writing some custom code.

Getting ready

Once again, we will use the citysales.csv dataset and build upon the graph from the first recipe in this chapter.

How to do it...

Let's make the graph with vertical bars and display the sales values just on top of the bars:

x<-barplot(as.matrix(citysales[,2:4]), beside=TRUE, legend.text=citysales$City, args.legend=list(bty="n",horiz=TRUE), col=brewer.pal(5,"Set1"),border="white", ylim=c(0,100),ylab="Sales Revenue (1,000's of USD)", main="Sales Figures") y<-as.matrix(citysales[,2:4]) ...

Get R: Data Analysis and Visualization now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.