Draw Stacked Bars within Grouped Barplot in R (Example)
In this R tutorial you’ll learn how to create stacked bars within a grouped ggplot2 barchart.
Table of contents:
Let’s just jump right in.
Creation of Example Data
The following data will be used as basement for this R programming tutorial:
set.seed(687532) # Create example data frame data <- data.frame(facet = rep(LETTERS[1:5], each = 6), group = c("x", "y"), stack = letters[1:3], value = round(abs(rnorm(30)), 2)) data # Print example data frame
Table 1 shows that our example data consists of 30 rows and four columns called “facet”, “group”, “stack”, and “value”. The variables facet, group, and stack are characters and the variable value has the numeric class.
It is important to structure the data frame similar to the example data in this tutorial (i.e. long format). In case you need to reshape your data from wide to long format, you may have a look here.
Anyway, let’s move on to the drawing of our data!
Example: Draw Stacked Bars within Grouped Barchart Using ggplot2 Package
This example demonstrates how to create a grouped barplot with stacked bars in R.
For the following R code, we first need to install and load the ggplot2 package, in order to use the corresponding functions:
install.packages("ggplot2") # Install & load ggplot2 package library("ggplot2")
In the next step, we can use the ggplot, geom_bar, and facet_grid functions to draw a bargraph with stacked and grouped bars.
Let’s have a look at the output of the following R code, and then I will explain each component of the code:
ggplot(data, # Draw barplot with grouping & stacking aes(x = group, y = value, fill = stack)) + geom_bar(stat = "identity", position = "stack") + facet_grid(~ facet)
After executing the previous R syntax the ggplot2 barchart with stacked and grouped bars shown in Figure 1 has been created.
So how did we do that? Let’s have a closer look:
- Within the facet_grid function, we have specified the column facet. This column contains the main groups in our data and differentiates our data into five different plot panels.
- Within the ggplot & aes functions, we have specified the other groupings. The x argument was set to the group variable, i.e. the grouped bars within each facet; And the fill argument was set to the stack variable; i.e. the different colors for the stacked bars.
- Within the ggplot & aes functions, we also had to specify the numerical values corresponding to the bars, i.e. y = value.
You may replace those variable names by the column names in your data set to draw such a graphic based on other data.
Video & Further Resources
Have a look at the following video on my YouTube channel. In the video, I show the content of this post in a live programming session:
Furthermore, you might want to have a look at the related R tutorials on this homepage.
- Draw Grouped Barplot in R
- Draw Stacked Barplot in R
- Scale Bars of Stacked Barplot to a Sum of 100 Percent
- Graphics in R
- The R Programming Language
To summarize: In this tutorial you have learned how to combine stack and doge to draw stacked bars within a grouped ggplot2 barchart in the R programming language. Please let me know in the comments section, in case you have further questions.