Stacked Barplot in R

The Stacked Bar Chart in R Programming is very useful in comparing the data visually. Let us see how to Create a Stacked Barplot in R, Format its color, adding legends, adding names, creating clustered Barplot in R Programming language with an example.

Before we get into the R Programming Stacked Barplot example, let us see the data that we are going to use for this bar plot example. The employee is the date set that we used in our previous articles.

Stacked Barplot in R Programming 1

Create Stacked Barplot in R Programming

In this example, we show how to create a stacked barplot in R using the external data. For this example, we are importing data from the CSV file using the read.csv function. I suggest you refer R Read CSV article to understand the steps involved in CSV file import. And also refer to the Barplot in R Programming article.

# Stacked barPlot R Example 

# Read Data from CSV File
employee <- read.csv("Products.csv", TRUE, sep = ",", na.strings = TRUE)

count <- table(employee$EnglishCountryRegionName, employee$Color)
count

barplot(count)
Stacked Barplot in R Programming 2

The following R Programming statement creates a table with records of Countries, and color. Here, column values are unique colors, and row values are unique country names.

count <- table(employee$EnglishCountryRegionName, employee$Color)

Next, we are creating a bar chart using the above-specified table.

barplot(count)

Assigning names to Stacked Barplot in R Programming

In this example, we assign names to stacked barplot, X-Axis, and Y-Axis using main, xlab, and ylab

  • main: You can change, or provide the Title for your stacked Barplot.
  • xlab: Please specify the label for the X-Axis
  • ylab: Please specify the label for the Y-Axis
  • las: Used to change the Y-axis values direction
# Stacked R barPlot Example 

employee <- read.csv("Products.csv", TRUE, sep = ",", 
                     na.strings = TRUE)

count <- table(employee$EnglishCountryRegionName, 
               employee$Color)
count

barplot(count,
        main = "Products Boxplot",
        xlab = "Colors",
        ylab = "Numbers",
        las = 1)
Stacked Barplot in R Programming 3

In this example, we change the stacked Barplot colors using col argument

  • col: Please specify the color you want to use for your barplot. Here, colors differentiated by the country names. Type colors() in your console to get the list of colors available in R programming.
  • names: Please specify the names for the stacked. Here, we are changing the names.
# Stacked R barPlot Example 

employee <- read.csv("Products.csv", TRUE, sep = ",", 
                     na.strings = TRUE)

count <- table(employee$EnglishCountryRegionName, employee$Color)
count
cols = c("yellow2", "hotpink4" , "brown", 
         "rosybrown2", "seagreen", "royalblue")

barplot(count,
        col = cols,
        main = "Products Boxplot",
        xlab = "Colors",
        ylab = "Numbers",
        las = 1,
        names = c("Black", "Blue", "Multiple", "No Color", 
                  "Red", "Silver", "White", "Yellow"))
Stacked Barplot in R Programming 4

Add Legend to Stacked Barplot in R

In this example, we add the legend to the stacked barplot in R Programming using legend.text argument.

  • If you specify legend.text = TRUE, legend values are automatically assigned, or
  • You can use vector of characters as legend values. For example, legend.text = c(“black”, “blue”, “green”, ..)
# Stacked R barPlot Example 

employee <- read.csv("Products.csv", TRUE, sep = ",", 
                     na.strings = TRUE)

count <- table(employee$EnglishCountryRegionName, employee$Color)
count
cols = c("yellow2", "hotpink4" , "brown", 
         "rosybrown2", "seagreen", "royalblue")

barplot(count,
        col = cols,
        legend.text = TRUE,
        main = "Products Boxplot",
        xlab = "Colors",
        ylab = "Numbers",
        las = 1,
        names = c("Black", "Blue", "Multiple", "No Color", 
                  "Red", "Silver", "White", "Yellow"))
Stacked Barplot in R Programming 5

Horizontal Stacked Bar Chart in R Programming

In this example, we convert the default vertical Stacked Barplot in R into a horizontal bar chart using the horiz argument

# Stacked R barPlot Example 

employee <- read.csv("Products.csv", TRUE, sep = ",", 
                     na.strings = TRUE)

count <- table(employee$EnglishCountryRegionName, 
               employee$Color)
count
cols = c("yellow2", "hotpink4" , "brown", 
         "rosybrown2", "seagreen", "royalblue")

barplot(count,
        col = cols,
        legend.text = TRUE,
        main = "Products Boxplot",
        xlab = "Numbers",
        ylab = "Colors",
        las = 1,
        horiz = TRUE,
        names = c("Black", "Blue", "Multiple", "No Color", 
                  "Red", "Silver", "White", "Yellow"))
Stacked Barplot in R Programming 6

Create Clustered Barplot in R Programming

In this example, we create a clustered barplot in R programming language using beside argument

# Clustered R barPlot Example 

employee <- read.csv("Products.csv", TRUE, sep = ",", 
                     na.strings = TRUE)

count <- table(employee$Gender, employee$EnglishOccupation)
count
cols = c("hotpink4" , "royalblue")

barplot(count,
        col = cols,
        beside = TRUE,
        # legend.text = TRUE,
        legend.text = c("Female", "Male"),
        main = "Employees Boxplot",
        xlab = "Occupation",
        ylab = "Numbers",
        las = 1)
Stacked Barplot in R Programming 7