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

datavizpyr · September 10, 2024 ·

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

Related posts:

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? Scatterplot with marginal multi-histogram with ggExtraHow To Make Scatterplot with Marginal Histograms in R?

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

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