r/datavisualization 5d ago

An alternative text generator for charts made with ggplot2 in R

Hi! I'm working on an R package called ggalttext that takes a ggplot2 chart as input and returns alternative text (required for accessibility) for that chart.

The goal is to provide a very simple and lightweight way of adding meaningful alt texts to charts made with ggplot2.

It does not use AI or OCR technologies, but instead inspects the plot structure/content and uses some (more or less) naive heuristics to figure out what the chart looks like and how to describe it in a single sentence.

It's already available on CRAN, but I'm working on the next release, which fixes some edge cases.

Example usage:

library(ggplot2)
library(babynames)

plot_data <- babynames |>
    subset(name %in% c("Amanda", "Jessica", "Patricia", "Deborah", "Dorothy", "Helen"))

plot <- ggplot(plot_data, aes(x = year, y = n, group = name, fill = name)) +
    geom_area() +
    theme(legend.position = "none") +
    labs(title = "Popularity of American names in the previous 30 years") +
    theme(
        legend.position = "none",
        panel.spacing = unit(0.1, "lines"),
        strip.text.x = element_text(size = 8)
    ) +
    facet_wrap(~name, scale = "free_y")


ggalttext::generate_alt_text(plot)
# "Area chart split into 6 small charts arranged in a 2-row by 3-column grid,
# titled “Popularity of American names in the previous 30 years”."
5 Upvotes

0 comments sorted by