• 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

Four geoms in ggplot2 to visualize uncertainty

datavizpyr · September 20, 2023 ·

In this post, we will learn about four geoms in ggplot2 that are useful for revealing uncertainty in numerical variables with multiple categories. The four geoms, geom_errorbar(), geom_linerange(), geom_crossbar(), and geom_pointrange() are useful when we have categeorical x values and we are interested in the “distribution of y conditional on x and use the aesthetics ymin and ymax to determine the range of the y values”. The third edition of ggplot2 book has a great chapter describing these geoms.

Let us get started by loading tidyverse and palmer penguin pacakges.

library(tidyverse)
library(palmerpenguins)
theme_set(theme_bw(16))
df <- penguins %>%
  drop_na() %>%
  group_by(species) %>%
  summarize(n= n(),
            mean_body_mass= mean(body_mass_g),
            sd = sd(body_mass_g))
df

# A tibble: 3 × 4
  species       n mean_body_mass    sd
  <fct>     <int>          <dbl> <dbl>
1 Adelie      146          3706.  459.
2 Chinstrap    68          3733.  384.
3 Gentoo      119          5092.  501.

Visualizing Uncertainty with geom_crossbar()

df %>%
  ggplot(aes(species, mean_body_mass,
                  ymin = mean_body_mass - sd,
                  ymax = mean_body_mass + sd)) +
  geom_crossbar()
ggsave("visualizing_uncertainty_with_geom_crossbar.png")
Visualizing uncertainty with geom_crossbar()
Visualizing uncertainty with geom_crossbar()

Visualizing Uncertainty with geom_errorbar()

df %>%
  ggplot(aes(species, mean_body_mass,
                  ymin = mean_body_mass - sd,
                  ymax = mean_body_mass + sd)) +
  geom_errorbar(linewidth = 1)
ggsave("visualizing_uncertainty_with_geom_errorbar.png")

Visualizing uncertainty with geom_errorbar()
Visualizing uncertainty with geom_errorbar()

Visualizing Uncertainty with geom_pointrange()

df %>%
  ggplot(aes(species, mean_body_mass,
                  ymin = mean_body_mass - sd,
                  ymax = mean_body_mass + sd)) +
  geom_pointrange()
ggsave("visualizing_uncertainty_with_geom_pointrange.png")
Visualizing uncertainty with geom_pointange()
Visualizing uncertainty with geom_pointange()

Visualizing Uncertainty with geom_linerange()

df %>%
  ggplot(aes(species, mean_body_mass,
                  ymin = mean_body_mass - sd,
                  ymax = mean_body_mass + sd)) +
  geom_linerange(color="blue", linewidth=2)
ggsave("visualizing_uncertainty_with_geom_linerange.png")
Visualizing uncertainty with geom_linerange()
Visualizing uncertainty with geom_linerange()

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:

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: geom_errorbar(), geom_pointrange(), ggplot2, R Tagged With: geoms for visualizing uncertainty

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