Position geom_text Labels in Grouped ggplot2 Barplot in R (Example)
In this article, I’ll demonstrate how to properly add text labels to a dodged ggplot2 barchart in R.
The article consists of these contents:
Let’s take a look at some R codes in action…
Example Data, Packages & Basic Graphic
We’ll use the following data as basement for this R tutorial.
data <- data.frame(height = 1:6, # Create example data group = rep(LETTERS[1:3], each = 2), subgroup = letters[1:2]) data # Print example data
Table 1 illustrates the output of the RStudio console returned by the previous syntax and shows that our example data is composed of six rows and three variables.
The variable height defines the heights of our bars and the variables group and subgroup define our different bars.
For the example of this tutorial, we’ll also need to install and load the ggplot2 package:
install.packages("ggplot2") # Install ggplot2 package library("ggplot2") # Load ggplot2
Next, we can draw the data in a grouped ggplot2 barplot:
ggp <- ggplot(data, aes(x = group, # Create ggplot2 plot without labels y = height, fill = subgroup)) + geom_bar(stat = "identity", position = "dodge") ggp # Draw ggplot2 plot without labels
In Figure 1 it is shown that we have plotted a grouped ggplot2 barchart with dodged positions by executing the previous syntax.
Next, we may add text labels on top of the bars using the geom_text function:
ggp + # Add text labels at wrong positions geom_text(aes(group, label = height))
After running the previous R programming syntax the bargraph illustrated in Figure 2 has been plotted. As you can see, the text labels on top of the bars are not aligned properly.
So how can we change the positioning of our text labels? That’s what I’ll explain in the following example!
Example: Specify Correct Text Label Positions of Dodged Barplot
This example illustrates how to add geom_text labels at the top of each bar of our grouped barplot.
For this, we have to specify the position argument within the geom_text function to be equal to position_dodge(width = 1).
Have a look at the following R code:
ggp + # Add text labels at correct positions geom_text(aes(group, label = height), position = position_dodge(width = 1))
In Figure 3 you can see that we have plotted a grouped barplot with properly located text counts on the of the bars using the previous code.
Video & Further Resources
Have a look at the following video of my YouTube channel. In the video, I demonstrate the R code of this article:
Furthermore, you may read some of the other tutorials on my website. I have published numerous tutorials already.
- Change Labels of ggplot2 Facet Plot
- Move Position of Barplot Legend
- ggplot2 Barplot with Axis Break & Zoom in R
- Plot Mean in ggplot2 Barplot
- Graphics Overview in R
- All R Programming Tutorials
In summary: In this article, I have demonstrated how to use the geom_text function to draw text labels on top of the bars of a grouped barplot in the R programming language. Don’t hesitate to tell me about it in the comments section, if you have additional questions.