| Title: | Data-Driven Alternative Text and Data Tables for 'ggplot2' Charts |
|---|---|
| Description: | Generates alternative text that states what a 'ggplot2' chart shows (highest and lowest values, rankings, trends, gaps between groups, overlap of intervals), a long description and the data table, computed from the data of the chart itself. Output for 'R Markdown', 'Quarto', 'Word', saved images and 'Shiny', in English and Italian. |
| Authors: | Federico Ninivaggi [aut, cre] |
| Maintainer: | Federico Ninivaggi <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-10-14 18:59:25 UTC |
| Source: | https://github.com/cran/ggaltdata |
Fills labs(alt = ), so that ggplot2::get_alt_text(), Shiny's renderPlot() and tools that
read it use the generated text. A text already written by hand is kept unless overwrite = TRUE.
add_alt(p, lang = "en", max_chars = 250, overwrite = FALSE, area_var = NULL)add_alt(p, lang = "en", max_chars = 250, overwrite = FALSE, area_var = NULL)
p |
A ggplot object. |
lang |
Language of the text: |
max_chars |
Maximum length of the text in characters (default 250). |
overwrite |
Replace an existing alternative text. |
area_var |
For maps ( |
The plot, with the alternative text set.
library(ggplot2) df <- data.frame(item = c("Apples", "Pears"), score = c(3.6, 2.7)) p <- add_alt(ggplot(df, aes(item, score)) + geom_col()) ggplot2::get_alt_text(p)library(ggplot2) df <- data.frame(item = c("Apples", "Pears"), score = c(3.6, 2.7)) p <- add_alt(ggplot(df, aes(item, score)) + geom_col()) ggplot2::get_alt_text(p)
The data table of a chart
alt_data(p, area_var = NULL)alt_data(p, area_var = NULL)
p |
A ggplot object. |
area_var |
For maps ( |
A data frame with the plotted values under readable column names.
library(ggplot2) df <- data.frame(item = c("Apples", "Pears"), score = c(3.6, 2.7)) alt_data(ggplot(df, aes(item, score)) + geom_col())library(ggplot2) df <- data.frame(item = c("Apples", "Pears"), score = c(3.6, 2.7)) alt_data(ggplot(df, aes(item, score)) + geom_col())
Builds a plain-text description of the chart (title, structure, every finding, ranking) and its data table. Printed in the console, it reads line by line in a screen reader.
alt_describe(p, lang = "en", n_extremes = 3, area_var = NULL)alt_describe(p, lang = "en", n_extremes = 3, area_var = NULL)
p |
A ggplot object. |
lang |
Language of the text: |
n_extremes |
How many categories to name at the top and bottom when there are many. |
area_var |
For maps ( |
An object of class ggaltdata with elements short_sentences, long, table and
facts; its print method writes the long description and the data table.
library(ggplot2) df <- data.frame(item = c("Apples", "Pears", "Plums"), score = c(3.6, 2.7, 3.2)) alt_describe(ggplot(df, aes(item, score)) + geom_col())library(ggplot2) df <- data.frame(item = c("Apples", "Pears", "Plums"), score = c(3.6, 2.7, 3.2)) alt_describe(ggplot(df, aes(item, score)) + geom_col())
Reads the data drawn by the chart and writes one or two sentences with its main finding: highest and lowest values, gaps between two groups, trends of series, intervals that overlap or not. Every number comes from the data of the chart.
alt_text(p, lang = "en", max_chars = 250, n_extremes = 3, area_var = NULL)alt_text(p, lang = "en", max_chars = 250, n_extremes = 3, area_var = NULL)
p |
A ggplot object. |
lang |
Language of the text: |
max_chars |
Maximum length of the text in characters (default 250). |
n_extremes |
How many categories to name at the top and bottom when there are many. |
area_var |
For maps ( |
A character string.
library(ggplot2) df <- data.frame(item = c("Apples", "Pears", "Plums"), score = c(3.6, 2.7, 3.2)) p <- ggplot(df, aes(item, score)) + geom_col() + labs(y = "average score") alt_text(p) alt_text(p, lang = "it")library(ggplot2) df <- data.frame(item = c("Apples", "Pears", "Plums"), score = c(3.6, 2.7, 3.2)) p <- ggplot(df, aes(item, score)) + geom_col() + labs(y = "average score") alt_text(p) alt_text(p, lang = "it")
Requires the officer package.
as_docx_table(x, doc, caption = NULL, lang = "en")as_docx_table(x, doc, caption = NULL, lang = "en")
x |
A ggplot object or the result of |
doc |
An |
caption |
Optional caption written before the table. |
lang |
Language of the generated texts, if |
The rdocx object with the table added.
if (requireNamespace("officer", quietly = TRUE)) { library(ggplot2) df <- data.frame(item = c("Apples", "Pears"), score = c(3.6, 2.7)) p <- ggplot(df, aes(item, score)) + geom_col() doc <- as_docx_table(p, officer::read_docx(), caption = "Data of Figure 1") print(doc, target = file.path(tempdir(), "report.docx")) }if (requireNamespace("officer", quietly = TRUE)) { library(ggplot2) df <- data.frame(item = c("Apples", "Pears"), score = c(3.6, 2.7)) p <- ggplot(df, aes(item, score)) + geom_col() doc <- as_docx_table(p, officer::read_docx(), caption = "Data of Figure 1") print(doc, target = file.path(tempdir(), "report.docx")) }
The data table as a Markdown table
as_markdown(x, caption = NULL, digits = NULL, lang = "en")as_markdown(x, caption = NULL, digits = NULL, lang = "en")
x |
A ggplot object or the result of |
caption |
Optional caption written above the table. |
digits |
Digits for numbers; by default chosen from the data. |
lang |
Language used for numbers (decimal mark). |
A character string with a pipe table.
library(ggplot2) df <- data.frame(item = c("Apples", "Pears"), score = c(3.6, 2.7)) cat(as_markdown(ggplot(df, aes(item, score)) + geom_col()))library(ggplot2) df <- data.frame(item = c("Apples", "Pears"), score = c(3.6, 2.7)) cat(as_markdown(ggplot(df, aes(item, score)) + geom_col()))
Writes two files beside the image saved with ggplot2::ggsave(): for file = "figure1.png",
figure1.alt.txt (short and long text) and figure1.data.csv (the data table).
save_alt(p, file, lang = "en", max_chars = 250, area_var = NULL)save_alt(p, file, lang = "en", max_chars = 250, area_var = NULL)
p |
A ggplot object. |
file |
Path of the image (for example |
lang |
Language of the text: |
max_chars |
Maximum length of the text in characters (default 250). |
area_var |
For maps ( |
Invisibly, the paths of the two files written.
library(ggplot2) df <- data.frame(item = c("Apples", "Pears"), score = c(3.6, 2.7)) p <- ggplot(df, aes(item, score)) + geom_col() f <- file.path(tempdir(), "figure1.png") ggsave(f, p, width = 4, height = 3) save_alt(p, f)library(ggplot2) df <- data.frame(item = c("Apples", "Pears"), score = c(3.6, 2.7)) p <- ggplot(df, aes(item, score)) + geom_col() f <- file.path(tempdir(), "figure1.png") ggsave(f, p, width = 4, height = 3) save_alt(p, f)