## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 6, fig.height = 3.5)
library(ggaltdata)
library(ggplot2)

## ----bars, fig.alt = alt_text(p)----------------------------------------------
scores <- data.frame(
  item = rep(c("Apples", "Pears", "Plums", "Figs"), 2),
  area = rep(c("North", "South"), each = 4),
  score = c(2.80, 3.05, 3.35, 2.95, 3.35, 3.30, 3.50, 3.25)
)
p <- ggplot(scores, aes(item, score, fill = area)) +
  geom_col(position = "dodge") +
  labs(title = "Ratings in two markets", x = "products", y = "average score")
p

## -----------------------------------------------------------------------------
alt_text(p)

## -----------------------------------------------------------------------------
alt_text(p, lang = "it")

## -----------------------------------------------------------------------------
alt_describe(p)

## -----------------------------------------------------------------------------
p2 <- add_alt(p)
ggplot2::get_alt_text(p2)

## ----eval = FALSE-------------------------------------------------------------
# output$plot <- renderPlot(p, alt = alt_text(p))

## ----eval = FALSE-------------------------------------------------------------
# ggsave("figure1.png", p)
# save_alt(p, "figure1.png")   # figure1.alt.txt and figure1.data.csv

## ----eval = FALSE-------------------------------------------------------------
# doc <- officer::read_docx()
# doc <- as_docx_table(p, doc, caption = "Data of Figure 1")
# print(doc, target = "report.docx")

## ----results = "asis"---------------------------------------------------------
cat(as_markdown(p, caption = "Data of the chart"))

## ----intervals, fig.alt = alt_text(q)-----------------------------------------
towns <- data.frame(
  town = c("Alder", "Birch", "Cedar", "Maple"),
  est = c(4.05, 3.90, 3.31, 3.82), lo = c(3.93, 3.77, 3.15, 3.70), hi = c(4.17, 4.03, 3.47, 3.94)
)
q <- ggplot(towns, aes(est, town)) +
  geom_pointrange(aes(xmin = lo, xmax = hi)) +
  geom_vline(xintercept = 3.85, linetype = 2) +
  labs(x = "satisfaction index")
q
alt_text(q, max_chars = 400)

