Plot Frequencies on Top of Stacked Bar Chart with ggplot2 in R (Example)
In this R programming tutorial you’ll learn how to show data values on top of each bar of a stacked ggplot2 bar chart.
The post contains one example for the plotting of data with ggplot2. To be more specific, the post consists of this content:
- Creating Exemplifying Data
- Example: Draw Stacked ggplot2 Bar Plot with Frequencies on Top
- Video & Further Resources
Sound good? Let’s jump right to the R code:
Creating Exemplifying Data
First, we need to create some example data that we can plot in a bar chart:
set.seed(85158) # Create example data data <- data.frame(x = rep(LETTERS[1:5], each = 5), y = round(runif(25, 10, 100)), group = rep(LETTERS[6:10], time = 5)) head(data) # Print example data # x y group # 1 A 25 F # 2 A 88 G # 3 A 88 H # 4 A 22 I # 5 A 18 J # 6 B 54 F |
set.seed(85158) # Create example data data <- data.frame(x = rep(LETTERS[1:5], each = 5), y = round(runif(25, 10, 100)), group = rep(LETTERS[6:10], time = 5)) head(data) # Print example data # x y group # 1 A 25 F # 2 A 88 G # 3 A 88 H # 4 A 22 I # 5 A 18 J # 6 B 54 F
As you can see based on the output of the RStudio console, our example data consists of three columns: x, y and a grouping column.
If we want to draw a plot of our data with the ggplot2 package, we also need to install and load the ggplot2 package:
install.packages("ggplot2") # Install and load ggplot2 library("ggplot2") |
install.packages("ggplot2") # Install and load ggplot2 library("ggplot2")
Now, we can draw a ggplot2 stacked bar graph as follows:
ggp <- ggplot(data, aes(x = x, y = y, fill = group, label = y)) + # Create stacked bar chart geom_bar(stat = "identity") ggp # Draw stacked bar chart |
ggp <- ggplot(data, aes(x = x, y = y, fill = group, label = y)) + # Create stacked bar chart geom_bar(stat = "identity") ggp # Draw stacked bar chart
Figure 1: Stacked Bar Chart Created with ggplot2 Package in R.
Figure 1 illustrates the output of the previous R code – A stacked bar chart with five groups and five stacked bars in each group.
Next, I’ll show how to add frequency values on top of each bar in this graph. So keep on reading!
Example: Draw Stacked ggplot2 Bar Plot with Frequencies on Top
If we want to put values on the top of each bar of our bar chart, we have to use the geom_text function and the position_stack argument of the ggplot2 package. Have a look at the following R programming syntax:
ggp + # Add values on top of bars geom_text(size = 5, position = position_stack(vjust = 0.5)) |
ggp + # Add values on top of bars geom_text(size = 5, position = position_stack(vjust = 0.5))
Figure 2: ggplot2 Stacked Bar Chart with Frequencies on Top of Bars.
As you can see in Figure 2, we added the frequency numbers of each bar on top. Looks great!
Video & Further Resources
Do you need more information on the R codes of this tutorial? Then you might watch the following video of my YouTube channel. I’m explaining the R programming codes of this tutorial in the video:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
In addition, you may want to read some of the other articles of this website. I have released several posts already:
- Barplot in Base R, plotly & ggplot2
- How to Order Bars of a Bar Chart
- R Graphics Gallery
- The R Programming Language
To summarize: This tutorial showed how to plot a frequencies, proportion values or a percentage on the top of each bar of a ggplot2 bar graphic in the R programming language. In case you have additional comments or questions, please let me know in the comments section.
Subscribe to my free statistics newsletter: