• 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 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

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

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