Skip to contents

The hdatools package provides a set of functions and tools for data analysis and visualization.

Installation

# install.packages("devtools")
devtools::install_github("hdadvisors/hdatools")

hdatools bundles the Lato, Roboto Slab, Open Sans, Poppins, Noto Sans, and Montserrat faces used by its themes and registers them with systemfonts offline the first time the package loads — no network request, no per-session Google Fonts download. To skip registration (for example, to supply your own font setup), set options(hdatools.fonts = FALSE) or the environment variable HDATOOLS_NO_FONTS before loading the package.

Rendering plots with these fonts requires a systemfonts-aware graphics device. In a Quarto document, add the following to _quarto.yml (the default Cairo device does not consult the systemfonts registry, so without this the bundled fonts won’t appear in rendered output):

knitr:
  opts_chunk:
    dev: "ragg_png"

Features

Themes

Under ggplot2 >= 4.0, override a theme’s strip.text (e.g. for faceted plots) with ggtext::element_markdown(), never a raw ggplot2::element_text() — the themes’ own strip element is a ggtext markdown element, and ggplot2 4.0 only merges theme elements of the same class.

Color Scales

Continuous ramp scales

Sequential and diverging colorspace HCL ramps, one pair per brand. The HDA/HFV/PHA ramps were tuned and CVD-checked in the Ramp Lab review; VHA’s pair was generated by the same method when VHA was added. HDA’s and VHA’s diverging ramps are provisional (see NEWS.md).

Each takes palette = c("sequential", "diverging") to choose the ramp.

Utility Functions

Usage

Basic example:

library(hdatools)
library(tidyverse)

# Create a sample dataset
data <- data.frame(
  x = as.character(c(1:8)),
  y = runif(8, 0, 100),
  group = rep(c("A", "B"), each = 4)
)

# Create a plot with HDA theme and colors
ggplot(data, aes(x, y, fill = group)) +
  geom_col(position = "dodge") +
  scale_fill_hda() +
  add_zero_line() +
  theme_hda() +
  labs(title = "Sample Plot with HDA Theme",
       subtitle = "Using *hdatools* package",
       caption = "**Source:** Data source.")

# Add reliability labels to a dataset, naming the CV column (percent scale)
data_with_reliability <- data |> 
  mutate(cv = runif(8, 0, 50)) |> 
  add_reliability(cv_col = cv)

# Legacy path: auto-detects a single column ending in "_cv" (0-1 proportion)
data_with_legacy_cv <- data |> 
  mutate(value_cv = runif(8, 0, 0.5)) |> 
  add_reliability()

# Create a factor with custom ordering
data_with_factor <- data |> 
  mutate(factor_col = fct_case_when(
    x < 3 ~ "Low",
    x < 7 ~ "Medium",
    TRUE ~ "High"
  ))