Add missing alt text

This commit is contained in:
mine-cetinkaya-rundel 2023-05-26 11:23:14 -04:00
parent 9ca4cb9a95
commit 587cb0d51d
6 changed files with 67 additions and 2 deletions

View File

@ -744,6 +744,15 @@ For example, if we extract two classes of cars and plot them separately, it's di
```{r}
#| layout-ncol: 2
#| fig-width: 4
#| fig-alt: |
#| On the left, a scatterplot of highway mileage vs. displacement of SUVs.
#| On the right, a scatterplot of the same variables for compact cars.
#| Points are colored by drive type for both plots. Among SUVs more of
#| the cars are 4-wheel drive and the others are rear-wheel drive, while
#| among compact cars more of the cars are front-wheel drive and the others
#| are 4-wheel drive. SUV plot shows a clear negative relationship
#| between higway mileage and displacement while in the compact cars plot
#| the relationship is much flatter.
suv <- mpg |> filter(class == "suv")
compact <- mpg |> filter(class == "compact")
@ -762,6 +771,16 @@ One way to overcome this problem is to share scales across multiple plots, train
```{r}
#| layout-ncol: 2
#| fig-width: 4
#| fig-alt: |
#| On the left, a scatterplot of highway mileage vs. displacement of SUVs.
#| On the right, a scatterplot of the same variables for compact cars.
#| Points are colored by drive type for both plots. Both plots are plotted
#| on the same scale for highway mileage, displacement, and drive type,
#| resulting in the legend showing all three types (front, rear, and 4-wheel
#| drive) for both plots even though there are no front-wheel drive SUVs and
#| no rear-wheel drive compact cars. Since the x and y scales are the same,
#| and go well beyond minimum or maximum highway mileage and displacement,
#| the points do not take up the entire plotting area.
x_scale <- scale_x_continuous(limits = range(mpg$displ))
y_scale <- scale_y_continuous(limits = range(mpg$hwy))
@ -829,6 +848,9 @@ Finally, you can customize the non-data elements of your plot with a theme:
```{r}
#| message: false
#| fig-alt: |
#| Scatterplot of highway mileage vs. displacement of cars, colored by class
#| of car. The plot background is white, with gray grid lines.
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point(aes(color = class)) +

View File

@ -647,6 +647,10 @@ Wouldn't it be nice if you could wrap this up into a histogram function?
This is easy as pie once you know that `aes()` is a data-masking function and you need to embrace:
```{r}
#| fig-alt: |
#| A histogram of carats of diamonds, ranging from 0 to 5, showing a unimodal,
#| right-skewed distribution with a peak between 0 to 1 carats.
histogram <- function(df, var, binwidth = NULL) {
df |>
ggplot(aes(x = {{ var }})) +
@ -672,6 +676,11 @@ It's straightforward to add more variables to the mix.
For example, maybe you want an easy way to eyeball whether or not a dataset is linear by overlaying a smooth line and a straight line:
```{r}
#| fig-alt: |
#| Scatterplot of height vs. mass of StarWars characters showing a positive
#| relationship. A smooth curve of the relationship is plotted in red, and
#| the best fit line is ploted in blue.
# https://twitter.com/tyler_js_smith/status/1574377116988104704
linearity_check <- function(df, x, y) {
df |>
@ -689,6 +698,10 @@ starwars |>
Or maybe you want an alternative to colored scatterplots for very large datasets where overplotting is a problem:
```{r}
#| fig-alt: |
#| Hex plot of price vs. carat of diamonds showing a positive relationship.
#| There are more diamonds that are less than 2 carats than more than 2 carats.
# https://twitter.com/ppaxisa/status/1574398423175921665
hex_plot <- function(df, x, y, z, bins = 20, fun = "mean") {
df |>
@ -764,6 +777,10 @@ This works similarly to `str_glue()`, so any value wrapped in `{ }` will be inse
But it also understands `{{ }}`, which automatically inserts the appropriate variable name:
```{r}
#| fig-alt: |
#| Histogram of carats of diamonds, ranging from 0 to 5. The distribution is
#| unimodal and right skewed with a peak between 0 to 1 carats.
histogram <- function(df, var, binwidth) {
label <- rlang::englue("A histogram of {{var}} with binwidth {binwidth}")

View File

@ -948,6 +948,11 @@ We can take the same basic approach to create many plots.
Let's first make a function that draws the plot we want:
```{r}
#| fig-alt: |
#| Histogram of carats of diamonds from the by_clarity dataset, ranging from
#| 0 to 5 carats. The distribution is unimodal and right skewed with a peak
#| around 1 carat.
carat_histogram <- function(df) {
ggplot(df, aes(x = carat)) + geom_histogram(binwidth = 0.1)
}

View File

@ -754,10 +754,15 @@ Inequality joins use `<`, `<=`, `>=`, or `>` to restrict the set of possible mat
#| label: fig-join-lt
#| echo: false
#| out-width: ~
#| fig-cap: >
#| fig-cap: |
#| An inequality join where `x` is joined to `y` on rows where the key
#| of `x` is less than the key of `y`. This makes a triangular
#| shape in the top-left corner.
#| fig-alt: |
#| A diagram depicting an inequality join where a data frame x is joined by
#| a data frame y where the key of x is less than the key of y, resulting
#| in a triangular shape in the top-left corner.
knitr::include_graphics("diagrams/join/lt.png", dpi = 270)
```

View File

@ -469,6 +469,10 @@ plot <- ggplot(mpg, aes(x = displ, y = hwy)) + geom_point()
#| echo: false
#| fig-width: 4
#| out-width: "50%"
#| fig-alt: |
#| Scatterplot of highway mileage vs. displacement of cars, where the points
#| are normally sized and the axis text and labels are in similar font size
#| to the surrounding text.
plot
```
@ -477,6 +481,10 @@ plot
#| echo: false
#| fig-width: 6
#| out-width: "50%"
#| fig-alt: |
#| Scatterplot of highway mileage vs. displacement of cars, where the points
#| are smaller than in the previous plot and the axis text and labels are
#| smallter than the surrounding text.
plot
```
@ -485,6 +493,10 @@ plot
#| echo: false
#| fig-width: 8
#| out-width: "50%"
#| fig-alt: |
#| Scatterplot of highway mileage vs. displacement of cars, where the points
#| are even smaller than in the previous plot and the axis text and labels are
#| even smallter than the surrounding text.
plot
```

View File

@ -38,7 +38,11 @@ This is particularly useful if you use RStudio because typing `str_` will trigge
```{r}
#| echo: false
#| fig-alt: "`str_c` typed into the RStudio console with the autocomplete tooltip shown on top, which lists functions beginning with `str_c`. The funtion signature and beginning of the man page for the highlighted function from the autocomplete list are shown in a panel to its right."
#| fig-alt: |
#| str_c typed into the RStudio console with the autocomplete tooltip shown
#| on top, which lists functions beginning with str_c. The funtion signature
#| and beginning of the man page for the highlighted function from the
#| autocomplete list are shown in a panel to its right.
knitr::include_graphics("screenshots/stringr-autocomplete.png")
```