Learn R Programming

gghdx (version 0.2.0)

theme_hdx: ggplot color theme based on HDX visual design guide

Description

A theme that approximates the style of the Humanitarian Data Exchange (HDX).

Usage

theme_hdx(
  base_size = 10,
  base_family = NULL,
  horizontal = TRUE,
  title_family = NULL,
  design = c("2025", "legacy")
)

Value

A theme() to stylize a ggplot2::ggplot()

plot.

Arguments

base_size

base font size, given in pts.

base_family

Base font family for body text, such as axis text and legend text. Defaults to "Roboto" when design = "2025", or "Source Sans 3" when design = "legacy".

horizontal

logical Horizontal axis lines?

title_family

Font family for titles, subtitles, and strip text when design = "2025". Defaults to "Merriweather", unless base_family is also supplied, in which case it defaults to base_family. Ignored when design = "legacy", which always uses a single font family.

design

Either "2025" (default), the current HDX visual design, or "legacy", the original pre-2025 theme kept for backwards compatibility.

Details

theme_hdx() implements a chart that follows the general visual guide of the HDX platform, as defined in the dataviz-guide.

Use scale_color_hdx_discrete() with this theme.

As of the 2025 HDX website redesign, theme_hdx() defaults to the new typography: the free Google font Merriweather for titles and other display text (title_family), and the free Google font Roboto for body text (base_family). Use the sysfonts package to add the Google fonts easily, or use load_hdx_fonts() to load both at once. Pass design = "legacy" to instead get the pre-2025 theme (single Source Sans 3 font, original gray palette, no axis ticks) for reports that aren't ready to move to the new look.

If you supply base_family but not title_family, title_family is set to match base_family rather than defaulting to "Merriweather". This means code written for the pre-2025 single-font theme, such as theme_hdx(base_family = "Source Sans 3"), continues to work without requiring Merriweather to also be loaded.

References

See Also

gghdx() for automatically applying the theme to all plots in this current R session, along with other styling.

Examples

Run this code
library(ggplot2)

p <- ggplot(mtcars) +
  geom_point(
    aes(
      x = mpg,
      y = hp
    )
  ) +
  labs(
    x = "Miles per gallon",
    y = "Horsepower",
    title = "Horsepower relative to miles per gallon"
  )

# the default fonts are Roboto (body) and Merriweather (titles)
# an error will occur if not loaded before using theme_hdx()
try(p + theme_hdx())

# you can change the base and title families
p + theme_hdx(base_family = "sans", title_family = "sans")

# supplying only base_family carries it over to title_family too
try(load_source_sans_3())
try(p + theme_hdx(base_family = "Source Sans 3"))

# or load Roboto and Merriweather using gghdx() or load_hdx_fonts()
try(load_hdx_fonts())
try(p + theme_hdx())

# we can change the axis line direction depending on the plot
p + theme_hdx(horizontal = FALSE)

# use the pre-2025 theme instead
p + theme_hdx(design = "legacy")

Run the code above in your browser using DataLab