Draw Grouped Barplot in R (3 Examples)

 

This tutorial illustrates how to create a bargraph with groups in the R programming language.

The post will consist of this:

Let’s take a look at some R codes in action!

 

Creation of Example Data

The following data will be used as basement for this R programming tutorial:

data <- data.frame(values = c(4, 1, 3, 6, 7, 3),  # Create example data
                   group = rep(c("group 1",
                                 "group 2",
                                 "group 3"),
                               each = 2),
                   subgroup = LETTERS[1:2])
data                                              # Print example data
#   values   group subgroup
# 1      4 group 1        A
# 2      1 group 1        B
# 3      3 group 2        A
# 4      6 group 2        B
# 5      7 group 3        A
# 6      3 group 3        B

Have a look at the previous output of the RStudio console. It shows that our example data has six rows and three columns.

The variable values contains the height of our bars, the variable group defines three different groups, and the variable subgroup divides each of our three main groups into two subgroups A and B.

The following examples show three different alternatives on how to draw grouped barplots in R. So keep on reading!

 

Example 1: Drawing Grouped Barchart Using Base R

In this example, I’ll show how to use the basic installation of the R programming language to draw a barplot with groups.

For this, we first have to convert our data frame to a properly formatted matrix:

data_base <- reshape(data,                        # Modify data for Base R barplot
                     idvar = "subgroup",
                     timevar = "group",
                     direction = "wide")
row.names(data_base) <- data_base$subgroup
data_base <- data_base[ , 2:ncol(data_base)]
colnames(data_base) <- c("group 1", "group 2", "group 3")
data_base <- as.matrix(data_base)
data_base                                         # Print modified data
#   group 1 group 2 group 3
# A       4       3       7
# B       1       6       3

Have a look at the previous output of the RStudio console. It shows our matrix that we will use to draw a Base R barchart.

The grouping column was converted to be the variables of our matrix and the subgroups are now specified by the row names.

Now, we can use the barplot() function to draw our grouped barplot in Base R:

barplot(height = data_base,                       # Grouped barplot using Base R
        beside = TRUE)

 

r graph figure 1 draw grouped barplot

 

As shown in Figure 1, we drew a bargraph with groups with the previous syntax.

However, the previous R code was kind of complicated and often the layouts of other graphical packages are preferred over the Base R graphics layout.

In the following examples, I’ll explain how to draw grouped barcharts using R add-on packages.

 

Example 2: Drawing Grouped Barchart Using ggplot2 Package

In Example 2, I’ll illustrate how to create a grouped barplot using the ggplot2 add-on package.

In case we want to apply the functions of the ggplot2 package, we first need to install and load ggplot2:

install.packages("ggplot2")                       # Install & load ggplot2 package
library("ggplot2")

Now, we can use the ggplot2 and the geom_bars functions to draw a grouped ggplot2 barchart.

Note that we are using the subgroup column to define the filling color of the bars and we are specifying the position argument within the geom_bar function to be equal to “dodge”.

ggplot(data,                                      # Grouped barplot using ggplot2
       aes(x = group,
           y = values,
           fill = subgroup)) +
  geom_bar(stat = "identity",
           position = "dodge")

 

r graph figure 2 draw grouped barplot

 

As shown in Figure 2, the previous R syntax drew a ggplot2 barchart with groups.

Note that we did not have to convert our input data in the forefront. In other words, the data type that is used by the basic installation of the R programming language (see Example 1) is different compared to the data type used by the ggplot2 package.

 

Example 3: Drawing Grouped Barchart Using lattice Package

Another popular add-on package for graphics in R is the lattice package.

If we want to use the functions of the lattice add-on package, we first have to install and load lattice to RStudio.

install.packages("lattice")                       # Install lattice package
library("lattice")                                # Load lattice package

Now, we can use the barchart function provided by the lattice package and the groups argument to create a grouped barchart:

barchart(values ~ group,                          # Grouped barplot using lattice
         data = data,
         groups = subgroup)

 

r graph figure 3 draw grouped barplot

 

As shown in Figure 3, we created a barplot with groups using the functions of the lattice package with the previous R programming syntax.

 

Video & Further Resources

Have a look at the following video of my YouTube channel. In the video, I’m showing the R codes of this page:

 

The YouTube video will be added soon.

 

In addition, I can recommend to have a look at some of the related tutorials of my website:

 

In this R tutorial you learned how to construct grouped barplots. If you have further comments or questions, don’t hesitate to tell me about it in the comments section.

 

Subscribe to my free statistics newsletter

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


Leave a Reply

Your email address will not be published. Required fields are marked *

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

Menu
Top