Skip to content

Commit

Permalink
caption-colors
Browse files Browse the repository at this point in the history
  • Loading branch information
friendly committed Jul 15, 2024
1 parent f4ed8db commit dafcf3f
Show file tree
Hide file tree
Showing 25 changed files with 325 additions and 449 deletions.
9 changes: 6 additions & 3 deletions 06-linear_models-plots.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -863,13 +863,16 @@ in the fitted values for the term being graphed.
In practical terms, a scoring matrix $\mathbf{X}^\bullet$ is defined by
the focal variables varied over their ranges and the other variables held
fixed. The fitted values for a model term are then calculated as
$\widehat{\mathbf{y}}^\bullet = \mathbf{X}^\bullet \hat{\mathbf{b}}$
$\widehat{\mathbf{y}}^\bullet = \mathbf{X}^\bullet \; \widehat{\mathbf{b}}$
using the equivalent of:
```{r}
#| eval: false
predict(model, newdata = X, se.fit = TRUE)
```
which also calculates the standard errors.
which also calculates the standard errors
as the square roots of
$\diag (\mathbf{X}^\bullet \, \widehat{\mathbf{\Var}} (\mathbf{b}) \, \mathbf{X}^{\bullet\mathsf{T}} )$
where $\widehat{\mathbf{\Var}} (\mathbf{b})$ is the estimated covariance matrix of the coefficients.
Consequently, predictor effect values can be obtained for _any_ modelling function that has
`predict()` and `vcov()` methods. To date, effect displays are available for
over 100 different model types in various packages.
Expand Down Expand Up @@ -1058,7 +1061,7 @@ predictorEffects(prestige.mod3, ~ income,
This provides a clear interpretation of the interaction, represented by the coefficients shown above for
`log10(income):typewc` and `log10(income):typeprof` in the model. For all three occupation types,
prestige increases linearly with log income, meaning that increasing income by 10% (say)
gives and increase of $40.33 / 10 = 4.033$ in prestige. The slope for professional workers
gives an increase of $40.33 / 10 = 4.033$ in prestige. The slope for professional workers
is less steep, suggesting that for these workers, prestige increases less with multiples of income.


Expand Down
295 changes: 99 additions & 196 deletions test/minimal-example.html → test/caption-colors.html

Large diffs are not rendered by default.

128 changes: 128 additions & 0 deletions test/caption-colors.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
---
title: "How to use colored text in Quarto figure captions"
format:
html:
toc: true
pdf:
toc: true
---

In figure captions, it is often useful to say refer to colored objects by printing
the words in their colors.
How can I do this with output in HTML or PDF?

Define a function to colorize text, that can be used in HTML or PDF output

```{r colorize}
# colorize text: use inline as `r colorize(text, color)` to print `text` in a given `color`
# can also be used to color a color name, as in r colorize("red")`
colorize <- function(text, color) {
if (missing(color)) color <- text
if (knitr::is_latex_output()) {
sprintf("\\textcolor{%s}{%s}", color, text)
} else if (knitr::is_html_output()) {
sprintf("<span style='color: %s;'>%s</span>", color, text)
} else text
}
```

In normal text, I can use `colorize()` to
print text in `r colorize("red")`, `r colorize("blue")`, `r colorize("green")`, ...

But not in a figure caption.

This works but the text is not colored in the caption of @fig-test

````qmd
#| label: fig-test
#| out-width: "70%"
#| fig-cap: Some points should be red, some blue, some green
cols <- c("red", "blue", "green")
plot(1:10, pch=16, cex = 3, col = cols)
````

```{r}
#| label: fig-test
#| out-width: "70%"
#| fig-cap: Some points should be red, some blue, some green
cols <- c("red", "blue", "green")
plot(1:10, pch=16, cex = 3, col = cols)
```

## Inline expression in a caption
`colorize()` doesn't work in a caption. The expression is not evaluated, and just prints in the caption
as a literal, in code font.

````qmd
#| label: fig-test2
#| out-width: "70%"
#| fig-cap: Some points are `r colorize("red")`, some are `r colorize("blue")`, some are `r colorize("green")`
cols <- c("red", "blue", "green")
plot(1:10, pch=16, cex = 3, col = cols)
````

```{r}
#| label: fig-test2
#| out-width: "70%"
#| fig-cap: Some points are `r colorize("red")`, some are `r colorize("blue")`, some are `r colorize("green")`
cols <- c("red", "blue", "green")
plot(1:10, pch=16, cex = 3, col = cols)
```

## Try !expr
One solution: use the YAML `!expr` "tag" literal in `fig-cap`:

````qmd
#| label: fig-test3
#| out-width: "70%"
#| fig-cap: !expr paste("Some points are", colorize('red'), "some are", colorize('blue'), "some are", colorize("green"))
cols <- c("red", "blue", "green")
plot(1:10, pch=16, cex = 3, col = cols)
````


```{r}
#| label: fig-test3
#| out-width: "70%"
#| fig-cap: !expr paste("Some points are", colorize('red'), "some are", colorize('blue'), "some are", colorize("green"))
cols <- c("red", "blue", "green")
plot(1:10, pch=16, cex = 3, col = cols)
```

## Can we simplify this?

Define color names as global variables and use `glue::glue()`
```{r}
red <- colorize('red')
blue <- colorize('blue')
green <- colorize("green")
library(glue)
```

````qmd
#| label: fig-test4
#| out-width: "70%"
#| fig-cap: !expr glue("Some points are ", {red}, " some are ", {blue}, "some are ", {green})
cols <- c("red", "blue", "green")
plot(1:10, pch=16, cex = 3, col = cols)
````
Voila! This is much easier to type in captions, but need to add spaces

```{r}
#| label: fig-test4
#| out-width: "70%"
#| fig-cap: !expr glue("Some points are ", {red}, " some are ", {blue}, " some are ", {green})
plot(1:10, pch=16, cex = 3, col = cols)
```

## Final solution

Use the color name variables, but just use `paste()`

````qmd
#| label: fig-test4
#| out-width: "70%"
#| fig-cap: !expr paste("Some points are", red, "some are", blue, "some are", green)
plot(1:10, pch=16, cex = 3, col = cols)
````

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit dafcf3f

Please sign in to comment.