• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Python Viz
  • Seaborn
  • Altair
  • R Viz
  • ggplot2
  • About
    • Privacy Policy
  • Show Search
Hide Search

How to Make Grouped Barplots with Same Bar Width

datavizpyr · April 29, 2021 ·

Grouped barplot allows to show third categorical variable in the barplot. Sometimes, when we make grouped barplot using ggplot2, some of the bars would have much broader width sizes. We typically get bars with uneven widths in a grouped barplot when some of the groups don’t have data for all of its levels.

In this tutorial, we will see how to make grouped barplots with same bar widths. We will first make grouped barplot with un-even bar width due to some data missing corresponding to the third categorical variable that we are using for grouping. And then we will use a trick or function available in ggplot2 version 3.3+ to make the bar widths the same in the grouped barplots.

Load Data and Packages for making Grouped Barplot

Let us load tidyverse. We will use gapminder dataset to make the grouped barplot.

library(tidyverse)
library(gapminder)
theme_set(theme_bw(base_size=16))

To make grouped barplot with uneven bar widths, we will create summary data from gapminder with average lifeExp for two years. We will also make sure it has missing data for the third group.

df <- gapminder %>%
  filter(year %in% c(1952, 2007)) %>%
  filter(continent %in% c("Africa", "Europe", "Americas") | year != 1952)%>%
  mutate(year=as.factor(year)) %>%
  group_by(continent, year) %>%
  summarize(ave_lifeExp=mean(lifeExp))

We can quickly spot that Asia does not data for the year 1952.

df %>% head()
## # A tibble: 6 x 3
## # Groups:   continent [4]
##   continent year  ave_lifeExp
##   <fct>     <fct>       <dbl>
## 1 Africa    1952         39.1
## 2 Africa    2007         54.8
## 3 Americas  1952         53.3
## 4 Americas  2007         73.6
## 5 Asia      2007         70.7
## 6 Europe    1952         64.4

Grouped barplot with bars placed side by side: with uneven bar width

Let us make a grouped barplot with bars side by side using geom_col(). Here we use fill argument for the variable year. And we also use position=”dodge” to make the bars are placed side by side.

df  %>%
  ggplot(aes(continent,ave_lifeExp, fill=year))+
  geom_col(position="dodge") +
  labs(title="Grouped Barplot with unequal bar widths",
        x="Continent", y= "Mean LifeExp")
ggsave("barplots_with_uneven_barwidth.png")

Looking at the grouped barplot we can see that our bars double the size when we have missing data corresponding to one of the groups. For example, content Asia and Oceania are missing life Expectancy data for the year 1952.

Grouped barplot with  un-even bar widths
Grouped barplot with un-even bar widths

Grouped Barplot with equal bar widths using position_dodge2(preserve = “single”)

We can make the bar widths in a grouped barplots the same size using the position argument to geom_col() function. We specify position_dodge2(preserve = “single”) to make the widths the same. And this option is available from ggplot2 version 3.3 and above.

df  %>%
  ggplot(aes(continent,ave_lifeExp, fill=year))+
  geom_col(position=position_dodge2(preserve = "single")) +
  labs(title="Grouped Barplot with equal bar widths",
        x="Continent", y= "Mean LifeExp")
gsave("barplots_with_same_barwidth.png")

Voila!. now our bars in the grouped barplots have the same width as we aimed to have.

Grouped barplot with same bar widths
Grouped barplot with same bar widths

Related posts:

Customizing Mean mark to boxplot with ggplot2How To Show Mean Value in Boxplots with ggplot2? Scatterplot with marginal multi-histogram with ggExtraHow To Make Scatterplot with Marginal Histograms in R? Default ThumbnailHow to Move Facet strip label to the bottom Default ThumbnailHow to change axis tick label size in ggplot2

Filed Under: ggplot2, R Tagged With: ggplot2 grouped barplot, ggplot2 position_dodge2

Primary Sidebar

Tags

Altair barplot Boxplot boxplot python boxplot with jiitered text labels Bubble Plot Color Palette Countplot Density Plot Facet Plot gganimate ggplot2 ggplot2 Boxplot ggplot2 error ggplot boxplot ggridges ggtext element_markdown() Grouped Barplot R heatmap heatmaps Histogram Histograms Horizontal boxplot Python lollipop plot Maps Matplotlib Pandas patchwork pheatmap Pyhon Python R RColorBrewer reorder boxplot ggplot Ridgeline plot Scatter Plot Scatter Plot Altair Seaborn Seaborn Boxplot Stock Price Over Time Stripplot UpSetR Violinplot Violin Plot World Map ggplot2

Buy Me a Coffee

Copyright © 2025 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version