Using the package

The package is designed so the user just has to run gghdx() once a session and mainly forget about it. This will automatically set your ggplot2 to use the HDX theme, palettes, fonts, and more by default. If you want more control or want to better understand how the package works, please see the details below!


A quick and simple example would be plotting the iris dataset included in base R.


p <- ggplot(
    x = Sepal.Length,
    y = Petal.Length,
    color = Species
) +
  geom_point() +
    title = "Iris species distributed by sepal and petal lengths",
    y = "Petal length",
    x = "Sepal length"


This output using the base ggplot style doesn’t look particularly bad, but we can use theme_hdx() to quickly adjust some of the styling to fit the style guide.


p + theme_hdx(base_family = "sans")

Now, axis lines have been cleaned up and the plot better resembles recommendations from the visual guide with just that single line of code.

Color palettes

However, the color palette for the points is still using the base R palette. We can use one of the many scale_...hdx() functions to use HDX colors. Let’s just use the primary discrete color scale that will align each species with one of the 3 non-gray colorramps (sapphire, mint, and tomato).

p + theme_hdx(base_family = "sans") + scale_color_hdx_discrete()

You can check the documentation of any of the scale_...hdx() functions to see all available scales, or directly access the colors using hdx_colors() or the raw list in hdx_color_list. The available palettes can be easily visualized using hdx_display_pal().

Adding fonts

We also would like to use the HDX font family. Since Source Sans 3 is a free Google font, it makes it relatively easy to access in R. gghdx uses the sysfonts package to load the Google font and then showtext to include them in our plot. You can also use the extrafont package as an alternative if you have the font installed locally. This requires ghostscript to be installed locally and can run into other issues, such as font names not being found.

Below, I use the showtext package because it’s simpler.

#> Loading required package: sysfonts
#> Loading required package: showtextdb

font_add_google("Source Sans 3")

p + theme_hdx(base_family = "Source Sans 3") + scale_color_hdx_discrete()

Streamlined plotting

As clear above, even though we have an HDX theme function, we still have to separately call the scale function to adjust our colors. And we have to call these every time we make a new plot. So, to make life simpler, gghdx() is provided as a convenience function that sets ggplot to:

You just have to run gghdx() once a session, and then our plots will already be where we would like!