• 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 Labels Over Each Bar in Barplot in R?

datavizpyr · May 22, 2020 ·

Adding text annotations over a barplot can be useful in quickly conveying the core message of a plot. In this post, we will see example of how to add text labels or text annotations over each bar in barplot. Barplot is great way to visualize numerical values corresponding to categorical variables. The height of the barplot represent the numerical value. With text label we can add the numerical value on top of the barplot for the ease of quick understanding.

Let us load tidyverse suit of packages.

library(tidyverse)

Let us use gapminder data to make barplots. We will download the data directly from github page.

link2data <- "https://raw.githubusercontent.com/cmdlinetips/data/master/gapminder-FiveYearData.csv"
gapminder <- read_csv(link2data)
## Parsed with column specification:
## cols(
##   country = col_character(),
##   year = col_double(),
##   pop = col_double(),
##   continent = col_character(),
##   lifeExp = col_double(),
##   gdpPercap = col_double()
## )

Let us compute mean life expectancy per continent using dplyr’s group_by() and summarize() functions.

life_df <- gapminder %>%
  group_by(continent) %>%
  summarize(ave_lifeExp=mean(lifeExp)) 

Our data looks like this.

life_df
## # A tibble: 5 x 2
##   continent ave_lifeExp
## * <chr>           <dbl>
## 1 Africa           48.9
## 2 Americas         64.7
## 3 Asia             60.1
## 4 Europe           71.9
## 5 Oceania          74.3

Now we are all set to make some barplots. To begin with, we will make a simple barplot in R using ggplot2. We will use geom_col() function in ggplot2 to make barplot. Here, the height of the bars represent numerical values in the data.

life_df %>% 
  ggplot(aes(continent,ave_lifeExp))+
  geom_col() +
  labs(title="Barplot with geom_col()")
ggsave("simple_barplot_with_R_ggplot2.png")
Barplot with geom_col()
Barplot with geom_col()

We can labels to bars in barplot using ggplot2’s function geom_text(). We need to provide how we want to annotate the bars using label argument. In our example, label values are average life expectancy values.

options(digits=2)
life_df %>% 
  ggplot(aes(continent,ave_lifeExp))+
  geom_col() +
  labs(title="Barplot with labels on bars")+
  geom_text(aes(label = signif(ave_lifeExp, digits = 3)), nudge_y = 4)

We get a nice barplot with average lifeexpectancy values on top of the bar.

Barplot with labels on each bar with R
Barplot with labels on each bar with R

We can easily customize the text labels on the barplot. For example, we can move the labels on y-axis to contain inside the bars using nudge_y argument. We can also specify the color of the labels on barplot with color argument.

life_df %>% 
  ggplot(aes(continent,ave_lifeExp))+
  geom_col() +
  coord_flip()+
  labs(title="Barplot with labels on bars",
        x="Continent", y= "Mean LifeExp")+
  geom_text(aes(label = round(ave_lifeExp, 1)), nudge_y= -3, color="white")

In this example, we have also flipped x and y-axis to make horizontal barplots using ggplot2 3.3.0’s feature.

Customizing labels on bars in barplot with R
Customizing labels on bars in barplot with R

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? Customizing Labels on Bars in Side by side Stacked BarplotHow To Add Labels to Grouped Barplot with Bars Side-By-Side in R?

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

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