Grouped Bar Plots

Grouped Bar Plots or Clustered Bar Plots are used to extend the functionalities of a single variate or single category bar plot to a multi variate bar plot. In these plots, the bars are grouped according to their categories and the colors are the differentiating factor to represent the other categorical variable. The bars are positioned catering to one group or the primary group and the colors represent the secondary category. For grouped bar plots, the value of position parameter is specified as “dodge”.

Approach:

  • Import module
  • Create dataframe
  • Plot graph with required functions
  • Set position parameter to dodge in geom_bar( ) function
  • Display plot

Syntax :

geom_bar(position = “dodge” , ….)

Example:

R




# importing the ggplot2 library
library(ggplot2)
  
# creating data frame
cities <- c(rep("Delhi", 3), rep("Mumbai", 3),
            rep("Chennai", 3), rep("Bengaluru", 3))
   
humidity <- rep(c("High", "Medium", "Low"), 4)
  
temperature <- abs(rnorm(12, 25, 10))
  
dataframe <- data.frame(cities, humidity,
                        temperature)
  
# calling the dataframe
dataframe
  
# plotting the graph
ggplot(dataframe, aes(fill = humidity,
                      y = temperature, x = cities))+
geom_bar(position = "dodge", stat = "identity")+
ggtitle("Weather Data of 4 Cities !")+
theme(plot.title = element_text(hjust = 0.5))


Output:

Grouped Bar Plot for the Weather Data Set 

Grouped, stacked and percent stacked barplot in ggplot2

The ggplot is a library used for generating graphs in R language. We provide the data and specify the aesthetics as to how the specified data should be mapped. It is a very powerful library and widely used to generate comprehensive graphs and plots. It is used for creating graphics based on the “Grammar of Graphics”.

A Bar Plot or Bar Chart is a Data Visualization tool that is widely used to represent the relationship between a numeric and a categorical variable. The numeric variable is generally plotted on the Y-axis and the categorical variable on the horizontal X-axis. The height of the bars represents the corresponding numeric value of the categorical value. The above-mentioned come in handy when we have more than one categorical variable and a numeric variable.

In this article, we will be seeing how we can plot 3 different types of Bar Plots. These 3 different types of Bar Plots are :

  • Grouped Bar Plot
  • Stacked Bar Plot
  • Percent Stacked Bar Plot

The only difference in the codes of the 3 plots is the value of the “position” parameter in the geom_bar() function of the ggplot library. Given below is implementation of the same.

Example :

R




# importing the ggplot2 library
library(ggplot2)
  
# creating the cities column
# c() is used to combine vectors
# rep() is used for replication of values
cities <- c(rep("Delhi", 3), rep("Mumbai", 3),
            rep("Chennai", 3), rep("Bengaluru", 3))
  
# creating the humidity column
# contains 3 classes 
humidity <- rep(c("High", "Medium", "Low"), 4)
  
# creating the temperature column
# abs() is used for getting the absolute value
# rnorm() is used for generating random variates
# in a normal distribution
# rnorm(number of samples, mean, SD)
temperature <- abs(rnorm(12, 25, 10))
  
# dataframe consisting of the three columns
dataframe <- data.frame(cities, humidity,
                        temperature)
  
# calling the dataframe
dataframe


Similar Reads

Grouped Bar Plots

...

Stacked Bar Plots

Grouped Bar Plots or Clustered Bar Plots are used to extend the functionalities of a single variate or single category bar plot to a multi variate bar plot. In these plots, the bars are grouped according to their categories and the colors are the differentiating factor to represent the other categorical variable. The bars are positioned catering to one group or the primary group and the colors represent the secondary category. For grouped bar plots, the value of position parameter is specified as “dodge”....

Percent Stacked Bar Plots

...