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 data frame draw stacked bars within grouped barplot r


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

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)


r graph figure 1 draw stacked bars within grouped barplot r


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:


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.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.


Furthermore, you might want to have a look at the related R tutorials on this homepage.


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.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

7 Comments. Leave new

  • Alexey Soloviev
    September 30, 2021 8:18 am

    Hi, Joachim Schork.Thank you for your work, very interesting site.
    Help me put the “stack” values ​​on the chart in reverse order, i.e. “C (blue)> B (green)> A (red)”. Thank you

    • Hey Alexey,

      Thank you for the kind words!

      Regarding your question, you may change the ordering of the stacking by executing the following R code before you create the plot:

      data$stack <- factor(data$stack, levels = letters[3:1])

      Note that you could also change the colors using the scale_fill_manual function.

      I hope that helps!


      • Hello Joachim

        Thank you for this tutorial, it helped me a lot!
        I have a follow-up question. I try to change the colors with the scale_fill_manual function by simply adding this part of code after yours:
        scale_colour_manual(values=c(‘aliceblue’,’green’,’red’),aesthetics = “colour”)
        But somehow this does not change the colors. Could you give me a hint why not or where I can find more information for this problem?

        Thanks a lot in advance!

  • Dear Joachim,

    this was grouped/stacked barchart is what I was looking for! so thanks. Would it be possible to have the facet labels on the bottom (as in below the group labels)? and to remove the grid in the background?
    Thanks it advance!


Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.