• 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
  • Seaborn
  • Matplotlib
  • ggplot2
  • Altair
  • About
    • Privacy Policy
  • Visualizing Activation Functions in Neural Networks
  • Confusion Matrix Calculator
  • Visualizing Dropout Rate in Neural Network
  • Visualizing Loss Functions in Neural Networks
  • Show Search
Hide Search

How to make stacked barplot with percent on x/y axis

datavizpyr · August 19, 2022 ·

In this post we will see how to make a stacked barplot showing percentage on its axis instead of count or proportion. We can make stacked barplot with count or proportion directly using geom_bar() function in ggplot2. Then we will use scales R package to add percentage on the axis instead of proportion.

Let us get started by loading packages needed for making the stacked barplot with percentage on it axis.

library(tidyverse)
library(palmerpenguins)
theme_set(theme_bw(16))

Stacked barplot with geom_bar() in ggplot2

We can make stacked plot by providing the second variable for fill as shown below. Note with geom_bar() function, we let ggplot2 to get the count for each species under the hood. In contrast, we have to provide the count for geom_col() function to make barplots.

penguins %>%
  ggplot(aes(x=species, fill=island))+
  geom_bar()
ggsave("stacked_bar_plot_with_geom_bar.png")

Stacked barplot with geom_bar()
Stacked barplot with geom_bar()

With position=”fill” argument to geom_bar() function we can convert the (stacked) barplot with count on y-axis to a barplot with proportion on y-axis.

penguins %>%
  ggplot(aes(x=species, fill=island))+
  geom_bar(position = "fill")
ggsave("stacked_bar_plot_with_proportion_using_geom_bar.png")
Stacked barplot with proportion on y-axis
Stacked barplot with proportion on y-axis
penguins %>%
  ggplot(aes(x=species, fill=island))+
  geom_bar(position = "fill")+
  scale_y_continuous(name = "percentage", 
                     labels = scales::label_percent())
ggsave("stacked_bar_plot_with_percentage_using_geom_bar.png")
Stacked bar plot with percentage on y-axis
Stacked bar plot with percentage on y-axis

Explore the Complete ggplot2 Guide

35+ tutorials with code: scatterplots, boxplots, themes, annotations, facets, and more—tested and beginner-friendly.

Visit the ggplot2 Hub → No fluff—just code and visuals.

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? ggforce geom_circle(): Annotate with a circleHow To Annotate a Plot with Circle in R Default ThumbnailHow to Make Axis Text Bold in ggplot2

Filed Under: ggplot2, R Tagged With: add percentage with scales, stacked barplot with percentage

Primary Sidebar

Python & R Viz Hubs

  • Seaborn Guide & Cookbook
  • ggplot2 Guide & Cookbook
  • Matplotlib Guide & Cookbook
  • Confusion Matrix Calculator
  • Visualizing Activation Functions
  • Visualizing Dropout
  • Visualizing Loss Functions

Buy Me a Coffee

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

Go to mobile version