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.
In the example, we have used the text()
function to label the bars with the corresponding values. To do so, we constructed two vectors, x
and y
, with the X and Y coordinates of the labels. We first created the barplot
and saved it as an R object called x
. When the result of the barplot()
function call is assigned to an object, a vector that contains the X coordinates of the center of each of the bars is returned and saved in that object. You can verify this by typing in x
at the R prompt and hitting the Enter key.