rstudio / thematic

Theme ggplot2, lattice, and base graphics based on a few simple settings.
https://rstudio.github.io/thematic/
Other
244 stars 10 forks source link

font families disappear after one plot in Quarto by just calling `okabe_ito()` #142

Closed AlbertRapp closed 1 year ago

AlbertRapp commented 1 year ago

Description

I like to use thematic::okabe_ito() to generate a color palette for my ggplots. I have used this many times inside and outside of Quarto docs without any issue. However, now I encounter a font rendering issue with this. For example, take a look at the following two code chunks:

library(tidyverse)
my_colors <- thematic::okabe_ito(3) # Remove this line to fix rendering
plt <- palmerpenguins::penguins |> 
  filter(!is.na(sex)) |> 
  ggplot(aes(x = body_mass_g, fill = species, y = flipper_length_mm)) +
  geom_point(shape = 21, size = 4) +
  theme_minimal(base_size = 16, base_family = 'Merriweather')
plt
plt

If I have both chunks in my Quarto doc, then the first renders just fine and the second one cannot render the "Merriweather" font anymore. Here's an image of that:

image

The key problem here is that I used thematic::okabe_ito(3) in my first code chunk. If I remove this, then both chunks render fine (I'm not using the colors in the plot). If I were to put the line my_colors <- thematic::okabe_ito(3) into a new code chunk before the first one, then none of the plots would render correctly.

I get that {thematic} renders the fonts differently when I want to use the package to adjust the plots but here I am only grabing three hex codes that are saved inside of {thematic}. This feels like an undesired consequence that I didn't run into before (and I have used thematic::okabe_ito() extensively).

Apart from that, I have used Merriweather here. And since it is a Google font, I gather from #107 and the link from that issue there that rendering should work even if {thematic} takes over.

Also, let me note that I am not quite sure whether this is a Quarto issue or {thematic} issue. Let me know if I should move this issue to Quarto.

Session Info


─ Session info ───────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.3.1 (2023-06-16)
 os       Ubuntu 22.04.3 LTS
 system   x86_64, linux-gnu
 ui       X11
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       Europe/Berlin
 date     2023-08-15
 pandoc   3.1.1 @ /usr/lib/rstudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)

─ Packages ───────────────────────────────────────────────────────────────────
 package        * version    date (UTC) lib source
 cachem           1.0.8      2023-05-01 [1] CRAN (R 4.3.0)
 callr            3.7.3      2022-11-02 [1] CRAN (R 4.3.0)
 cli              3.6.1      2023-03-23 [1] CRAN (R 4.3.0)
 colorspace       2.1-0      2023-01-23 [1] CRAN (R 4.3.0)
 crayon           1.5.2      2022-09-29 [1] CRAN (R 4.3.0)
 devtools         2.4.5      2022-10-11 [1] CRAN (R 4.3.1)
 digest           0.6.33     2023-07-07 [1] CRAN (R 4.3.1)
 dplyr          * 1.1.2      2023-04-20 [1] CRAN (R 4.3.0)
 ellipsis         0.3.2      2021-04-29 [1] CRAN (R 4.3.0)
 evaluate         0.21       2023-05-05 [1] CRAN (R 4.3.0)
 fansi            1.0.4      2023-01-22 [1] CRAN (R 4.3.0)
 farver           2.1.1      2022-07-06 [1] CRAN (R 4.3.0)
 fastmap          1.1.1      2023-02-24 [1] CRAN (R 4.3.0)
 forcats        * 1.0.0      2023-01-29 [1] CRAN (R 4.3.0)
 fs               1.6.2      2023-04-25 [1] CRAN (R 4.3.0)
 generics         0.1.3      2022-07-05 [1] CRAN (R 4.3.0)
 ggplot2        * 3.4.3      2023-08-14 [1] CRAN (R 4.3.1)
 glue             1.6.2      2022-02-24 [1] CRAN (R 4.3.0)
 gtable           0.3.3      2023-03-21 [1] CRAN (R 4.3.0)
 hms              1.1.3      2023-03-21 [1] CRAN (R 4.3.0)
 htmltools        0.5.5      2023-03-23 [1] CRAN (R 4.3.0)
 htmlwidgets      1.6.2      2023-03-17 [1] CRAN (R 4.3.1)
 httpuv           1.6.11     2023-05-11 [1] CRAN (R 4.3.1)
 jsonlite         1.8.7      2023-06-29 [1] CRAN (R 4.3.1)
 knitr            1.43       2023-05-25 [1] CRAN (R 4.3.0)
 labeling         0.4.2      2020-10-20 [1] CRAN (R 4.3.0)
 later            1.3.1      2023-05-02 [1] CRAN (R 4.3.1)
 lifecycle        1.0.3      2022-10-07 [1] CRAN (R 4.3.0)
 lubridate      * 1.9.2      2023-02-10 [1] CRAN (R 4.3.0)
 magrittr         2.0.3      2022-03-30 [1] CRAN (R 4.3.0)
 memoise          2.0.1      2021-11-26 [1] CRAN (R 4.3.0)
 mime             0.12       2021-09-28 [1] CRAN (R 4.3.0)
 miniUI           0.1.1.1    2018-05-18 [1] CRAN (R 4.3.1)
 munsell          0.5.0      2018-06-12 [1] CRAN (R 4.3.0)
 palmerpenguins   0.1.1      2022-08-15 [1] CRAN (R 4.3.1)
 pillar           1.9.0      2023-03-22 [1] CRAN (R 4.3.0)
 pkgbuild         1.4.2      2023-06-26 [1] CRAN (R 4.3.1)
 pkgconfig        2.0.3      2019-09-22 [1] CRAN (R 4.3.0)
 pkgload          1.3.2.1    2023-07-08 [1] CRAN (R 4.3.1)
 prettyunits      1.1.1      2020-01-24 [1] CRAN (R 4.3.0)
 processx         3.8.2      2023-06-30 [1] CRAN (R 4.3.1)
 profvis          0.3.8      2023-05-02 [1] CRAN (R 4.3.1)
 promises         1.2.0.1    2021-02-11 [1] CRAN (R 4.3.1)
 ps               1.7.5      2023-04-18 [1] CRAN (R 4.3.0)
 purrr          * 1.0.1      2023-01-10 [1] CRAN (R 4.3.0)
 R6               2.5.1      2021-08-19 [1] CRAN (R 4.3.0)
 Rcpp             1.0.11     2023-07-06 [1] CRAN (R 4.3.1)
 readr          * 2.1.4      2023-02-10 [1] CRAN (R 4.3.0)
 remotes          2.4.2.1    2023-07-18 [1] CRAN (R 4.3.1)
 rlang            1.1.1      2023-04-28 [1] CRAN (R 4.3.0)
 rmarkdown        2.22       2023-06-01 [1] CRAN (R 4.3.0)
 rstudioapi       0.15.0     2023-07-07 [1] CRAN (R 4.3.1)
 scales           1.2.1      2022-08-20 [1] CRAN (R 4.3.0)
 sessioninfo      1.2.2      2021-12-06 [1] CRAN (R 4.3.1)
 shiny            1.7.4.1    2023-07-06 [1] CRAN (R 4.3.1)
 showtext         0.9-6      2023-05-03 [1] CRAN (R 4.3.1)
 showtextdb       3.0        2020-06-04 [1] CRAN (R 4.3.1)
 stringi          1.7.12     2023-01-11 [1] CRAN (R 4.3.0)
 stringr        * 1.5.0      2022-12-02 [1] CRAN (R 4.3.0)
 sysfonts         0.8.8      2022-03-13 [1] CRAN (R 4.3.1)
 thematic         0.1.3.9000 2023-08-15 [1] Github (rstudio/thematic@ea2937b)
 tibble         * 3.2.1      2023-03-20 [1] CRAN (R 4.3.0)
 tidyr          * 1.3.0      2023-01-24 [1] CRAN (R 4.3.0)
 tidyselect       1.2.0      2022-10-10 [1] CRAN (R 4.3.0)
 tidyverse      * 2.0.0      2023-02-22 [1] CRAN (R 4.3.0)
 timechange       0.2.0      2023-01-11 [1] CRAN (R 4.3.0)
 tzdb             0.4.0      2023-05-12 [1] CRAN (R 4.3.0)
 urlchecker       1.0.1      2021-11-30 [1] CRAN (R 4.3.1)
 usethis          2.2.2      2023-07-06 [1] CRAN (R 4.3.1)
 utf8             1.2.3      2023-01-31 [1] CRAN (R 4.3.0)
 vctrs            0.6.3      2023-06-14 [1] CRAN (R 4.3.0)
 withr            2.5.0      2022-03-03 [1] CRAN (R 4.3.0)
 xfun             0.40       2023-08-09 [1] CRAN (R 4.3.1)
 xtable           1.8-4      2019-04-21 [1] CRAN (R 4.3.1)
 yaml             2.3.7      2023-01-23 [1] CRAN (R 4.3.0)

 [1] /home/albert/R/x86_64-pc-linux-gnu-library/4.3
 [2] /usr/local/lib/R/site-library
 [3] /usr/lib/R/site-library
 [4] /usr/lib/R/library

──────────────────────────────────────────────────────────────────────────────
gadenbuie commented 1 year ago

Hi @AlbertRapp and thanks for the issue!

Getting fonts to just work for plots inside R Markdown documents (or really anywhere that knitr is used) is tricky business. The reason that calling thematic::okabe_it(3) changes how your plots render (i.e. the warnings about Merriweather not being found) is because thematic does a few things when the package is attached to set everything up so that it can work correctly.

It's an unfortunate consequence that this means that you can't use the package without it trying to work with knitr, but the advantage is that it's much easier to use thematic in all of the most common use cases.

If you're only interested in using the Okabe-Ito palette in your plot, I'd recommend using ggokabeito instead of thematic. Another option is to call thematic::okabe_ito(3) in the R console and then copy the actual values into your script (in other words, don't attach thematic unless you need it 😄).

On the other hand, if you want to stick with thematic, I'd recommend adding a chunk like the following to your document, or incorporating this call into your setup chunk.

```{r}
#| message: false
thematic::thematic_rmd(font = "Merriweather")


If you're using a theme for your quarto document, you'll get best results if you add the `fg`, `bg`, and `accent` colors that match your document's theme.
AlbertRapp commented 1 year ago

HI @gadenbuie, thanks for the quick reply. I figured that this is probably intended behavior. I was just confused with the fact that I have used okabe_ito() a lot and never ran into any trouble before yesterday. Turns out, whenever I used this function in a Quarto doc, I either didn't change the default fonts or used custom fonts that I imported anyway 😆

I think for now I'll just use okabe_ito() to get the hex codes and put them into the code chunks manually. With the color highlighting in RStudio, that's also a nice touch 😆