• 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 Annotate Positive and Negative Values in a barplot

datavizpyr · September 10, 2024 ·

Last updated on August 14, 2025

In this post, we will learn how to properly annotate a bars barplot, where there are both positive and negative values/bars in ggplot2. In ggplot2, we can use gem_text() with label option to annotate text or bar heights on a barplot easily. However, when we have a barplot that has both positive and negative bars, We would live to annotate text/values such that it perfectly aligns with the bar heights. The simple attempt to annotate using geom_text() will not result in correctly aligned values.

library(tidyverse)
library(palmerpenguin)
library(broom)
theme_set(theme_bw(16)
set.seed(1234)
df <- tibble(grp = LETTERS[1:10],
             value = rnorm(n=10, mean=20, sd=40))|>
  mutate(direction=ifelse(value>0, "up", "down"))
df

# A tibble: 10 × 3
   grp    value direction
   <chr>  <dbl> <chr>    
 1 A     -28.3  down     
 2 B      31.1  up       
 3 C      63.4  up       
 4 D     -73.8  down     
 5 E      37.2  up       
 6 F      40.2  up       
 7 G      -2.99 down     
 8 H      -1.87 down     
 9 I      -2.58 down     
10 J     -15.6  down   

How to annotate barplots with both positive and negative bars

df |> 
  ggplot(aes(x=grp, y=value, fill=direction))+
  geom_col()+
  theme(legend.position="none")
ggsave("barplot_with_both_negative_n_positive_bars.png")
How to annotate barplots with both positive and negative bars?
How to annotate barplots with both positive and negative bars?

Annotating barplots with both positive and negative bars using geom_text()

df |> 
  ggplot(aes(x=grp, y=value, fill=direction))+
  geom_col()+
  theme(legend.position="none")+
  geom_text(aes(label=round(value)))
ggsave("annotate_barplot_with_geom_text.png")
Annotating barplots with geom_text()
Annotating barplots with geom_text()

Adjusting annotation location on barplot using geom_text()’s nudge_x or nudge_y is hard

df |> 
  ggplot(aes(x=grp, y=value, fill=direction))+
  geom_col()+
  theme(legend.position="none")+
  geom_text(aes(label=round(value)), nudge_y=5)
ggsave("annotate_barplot_with_geom_text_with_nudge_to_align.png")
Adjusting annotation location on barplot using geom_text()'s nudge_x or nudge_y
Adjusting annotation location on barplot using geom_text()’s nudge_x or nudge_y

Correctly align annotation on barplot with both negative and positive values on bars

df |> 
  ggplot(aes(x=grp, y=value, fill=direction))+
  geom_col()+
  theme(legend.position="none")+
  geom_text(aes(y= value + 5*sign(value) ,
            label=round(value)))
ggsave("annotate_barplot_with_both_negative_n_positive_values_on_bars_aligned.png")
Correctly align annotation on barplot with both negative and positive values on bars
Correctly align annotation on barplot with both negative and positive values on bars

Correctly align annotation on a horizontal barplot with both negative and positive values

df |> 
  ggplot(aes(y = grp, x=value, fill=direction))+
  geom_col()+
  theme(legend.position="none")+
  geom_text(aes(x = value + 5*sign(value),
                label=round(value)))
ggsave("annotate_horizontal_barplot_with_both_negative_n_positive_values_on_bars_aligned.png")
Correctly align annotation on a horizontal barplot with both negative and positive values
Correctly align annotation on a horizontal barplot with both negative and positive values

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:

Wrapping or Folding long axis tick labels in a ggplot into multiple linesHow to wrap long axis tick labels into multiple lines in ggplot2 Default Thumbnail3 Different ways to make bar plots with ggplot2 Stacked Barplots Side By Side with ggplot2 in RHow to Make Horizontal Stacked Barplots with ggplot2 in R? Customizing Mean mark to boxplot with ggplot2How To Show Mean Value in Boxplots with ggplot2?

Filed Under: barplot ggplot2, ggplot2, R Tagged With: annotate barplot ggplot2

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