• 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 add percentage label on bars in barplot with ggplot2

datavizpyr · December 31, 2021 ·

Adding percentage labels or count labels to bars in a barplot can help read the barplot quickly. In this tutorial, we will learn how to add labels with percentage symbol on bars in a barplot made with ggplot2 in R.

Loading Packages and Dataset

Let us get started by loading packages needed and a dataset to make the barplots with percentage values on bars.

library(tidyverse)

We will use student debt dataset showing racial disparity from tidytuesday project.

student_debt <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-02-09/student_debt.csv")

Our data looks like this.

student_debt %>% head()
## # A tibble: 6 × 4
##    year race     loan_debt loan_debt_pct
##   <dbl> <chr>        <dbl>         <dbl>
## 1  2016 White       11108.         0.337
## 2  2016 Black       14225.         0.418
## 3  2016 Hispanic     7494.         0.219
## 4  2013 White        8364.         0.285
## 5  2013 Black       10303.         0.412
## 6  2013 Hispanic     3177.         0.157

Simple Barplot with Percentage values as bar heights

Let us make a simple barplot showing the average loan debt percentage for each race in the dataset.

p1 <- student_debt %>%
  group_by(race) %>%
  summarize(average_loan_debt_pct=mean(loan_debt_pct)) %>%
  ggplot(aes(x = forcats::fct_reorder(race, average_loan_debt_pct),
             y = average_loan_debt_pct,
             fill=race))+
  geom_col(width=0.5)+
  theme(legend.position="none")+
  labs(x="",
       y="Mean Load Debt Pct")
print(p1)
ggsave("simple_barplot_with_ggplot2_with_percent_values_on_y_axis.png")

Simple barplot with percentage values on y-axis
Simple barplot with percentage values on y-axis

Adding Percentage Symbol on y-axis

Note that our y-axis values or bar heights are in percentage. A better way to make the barplot is to add the percentage symbol on the y-axis instead of the fraction we have now.

We can use scales package’ percent method to add percentage symbol to the y-axis using scale_y_continuous() function.

p1 +
  scale_y_continuous(labels = scales::percent,
                     breaks = scales::pretty_breaks(n = 8))
ggsave("barplot_with_percent_symbol_on_y_axis.png")

Now our y-axis text has percentage symbols in the barplot.

Add percent Symbols on x/y axis using scales package
Add percent Symbols on x/y axis using scales package

Add Percentage Labels on bars in barplot using label and geom_text()

We can improve the barplot further by labeling the percentage values directly on the bars with percent symbols. To do that, we will use label argument with scales’ percent function. And use geom_text() function to add the labels with percentage symbol on bars.

student_debt %>%
  group_by(race) %>%
  summarize(average_loan_debt_pct=mean(loan_debt_pct)) %>%
  ggplot(aes(x = forcats::fct_reorder(race, average_loan_debt_pct),
             y = average_loan_debt_pct,
             fill=race,
             label=scales::percent(average_loan_debt_pct)))+
  geom_col(width=0.5)+
  theme(legend.position="none")+
  labs(x="",
       y="Mean Load Debt")+
  scale_y_continuous(labels = scales::percent,
                     breaks = scales::pretty_breaks(n = 8))+
  geom_text(nudge_y= -.01,
            color="white",
            size = 5,
            fontface="bold")
gsave("add_label_with_percent_symbol_to_barplot_ggplot2.png")

How to add labels with percentage symbols on barplots in ggplot2
How to add labels with percentage symbols on barplots in ggplot2

Related posts:

Stacked Barplots Side By Side with ggplot2 in RHow to Make Horizontal Stacked Barplots with ggplot2 in R? Coloring Barplots by a Variable with ggplot2Coloring Barplots with ggplot2 in R How to Make Barplots with Error bars in R?How To Make Barplots with Error bars in ggplot2? Manually specified colors in barplot RHow To Manually Specify Colors for Barplot in ggplot2?

Filed Under: barplot ggplot2, R Tagged With: add percentage labels barplot

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