You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#' This function returns a text that can be used as alt-text in webpages etc.
108
+
#' Currently it will use the `alt` label, added with `+ labs(alt = <...>)`, or
109
+
#' a return an empty string, but in the future it might try to generate an alt
110
+
#' text from the information stored in the plot.
111
+
#'
112
+
#' @param p a ggplot object
113
+
#' @param ... Currently ignored
114
+
#'
115
+
#' @return A text string
116
+
#'
117
+
#' @export
118
+
#' @aliases alt_text
119
+
#'
120
+
#' @examples
121
+
#' p <- ggplot(mpg, aes(displ, hwy)) +
122
+
#' geom_point()
123
+
#'
124
+
#' # Returns an empty string
125
+
#' get_alt_text(p)
126
+
#'
127
+
#' # A user provided alt text
128
+
#' p <- p + labs(
129
+
#' alt = paste("A scatterplot showing the negative correlation between engine",
130
+
#' "displacement as a function of highway miles per gallon")
131
+
#' )
132
+
#'
133
+
#' get_alt_text(p)
134
+
#'
135
+
get_alt_text<-function(p, ...) {
136
+
UseMethod("get_alt_text")
137
+
}
138
+
#' @export
139
+
get_alt_text.ggplot<-function(p, ...) {
140
+
p$labels[["alt"]] %||% ""
141
+
}
142
+
#' @export
143
+
get_alt_text.ggplot_built<-function(p, ...) {
144
+
p$plot$labels[["alt"]] %||% ""
145
+
}
146
+
#' @export
147
+
get_alt_text.gtable<-function(p, ...) {
148
+
attr(p, "alt-label") %||% ""
149
+
}
150
+
151
+
#' Generate an alt text from a plot
152
+
#'
153
+
#' This function returns a text that can be used as alt-text in webpages etc.
154
+
#' It will synthesize one from the information in the plot itself, but you can
155
+
#' add a conclusion to the synthesized text using `+ labs(alt_insight = <...>)`.
156
+
#'
157
+
#' There is no way an automatically generated description can compete with one
158
+
#' written by a human with knowledge of what the plot shows and in which
159
+
#' context. We urge users to write their own alt text if at all possible.
160
+
#' Guidance to how an effective alt-text is written can be found in
161
+
#' [Writing Alt Text for Data Visualization](https://medium.com/nightingale/writing-alt-text-for-data-visualization-2a218ef43f81)
162
+
#' and [Effective Practices for Description of Science Content within Digital Talking Books](https://www.wgbh.org/foundation/ncam/guidelines/effective-practices-for-description-of-science-content-within-digital-talking-books)
163
+
#'
164
+
#' @param p a ggplot object
165
+
#'
166
+
#' @return A text string
167
+
#'
168
+
#' @noRd
169
+
#'
170
+
#' @examples
171
+
#' p <- ggplot(mpg, aes(displ, hwy)) +
172
+
#' geom_point()
173
+
#'
174
+
#' get_alt_text(p)
175
+
#'
176
+
#' p <- p + ggtitle("The relationship between displacement and yield in cars")
177
+
#' get_alt_text(p)
178
+
#'
179
+
#' # It will use scale information if available
180
+
#' p <- p + scale_x_continuous("highway miles per gallon")
181
+
#' get_alt_text(p)
182
+
#'
183
+
#' # Add a short description of the main conclusion of the plot
184
+
#' p <- p + labs(alt_insight = "The higher the yield, the lower the displacement")
185
+
#' get_alt_text(p)
186
+
#'
187
+
#' # A user provided alt text takes precedence
188
+
#' p <- p + labs(
189
+
#' alt = paste("A scatterplot showing the negative correlation between engine",
190
+
#' "displacement as a function of highway miles per gallon")
0 commit comments