Missing font warnings generated by `gganimate` when using `showtext`
caldwellst opened this issue · 1 comments
When using showtext
to access and use Google fonts in ggplot2
, I am getting missing font warnings when using gganimate::transition_time()
.
Plotting a static plot works as expected.
library(ggplot2)
library(gganimate)
library(showtext)
#> Loading required package: sysfonts
#> Loading required package: showtextdb
font_add_google("Source Sans Pro")
showtext_auto()
df <- data.frame(
year = c(2010, 2010, 2010, 2013, 2013,2013, 2014,2014,2014),
category = c("A", "B", "C", "A", "B", "C", "A", "B", "C"),
pct_val = c(0.05, 0.12, 0.21, 0.37, 0.44, 0.53, 0.69, 0.76, 0.82)
)
p <- ggplot(
data = df,
mapping = aes(
x = category,
y = pct_val
)
) +
geom_bar(stat = "identity") +
coord_flip() +
theme(
text = element_text(family = "Source Sans Pro")
)
p
However, warnings are generated once transition_time()
is called. The fonts still appear, but raising an issue in case this is indicative of a deeper problem.
p + transition_time(year)
#> Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : no
#> font could be found for family "Source Sans Pro"
#> Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : no
#> font could be found for family "Source Sans Pro"
#> Warning: Cannot get dimensions of plot table. Plot region might not be fixed
However, this problem goes away if I install Source Sans Pro on my machine directly. Thus it appears to be something related to how showtext
and gganimate
work on the backend.
Session info
sessionInfo()
#> R version 4.2.3 (2023-03-15)
#> Platform: x86_64-apple-darwin17.0 (64-bit)
#> Running under: macOS Big Sur ... 10.16
#>
#> Matrix products: default
#> BLAS: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib
#>
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] showtext_0.9-5 showtextdb_3.0 sysfonts_0.8.8 gganimate_1.0.8
#> [5] ggplot2_3.4.2
#>
#> loaded via a namespace (and not attached):
#> [1] pillar_1.9.0 compiler_4.2.3 prettyunits_1.1.1 tools_4.2.3
#> [5] progress_1.2.2 digest_0.6.31 jsonlite_1.8.4 evaluate_0.20
#> [9] lifecycle_1.0.3 tibble_3.2.1 gtable_0.3.3 pkgconfig_2.0.3
#> [13] rlang_1.1.1 reprex_2.0.2 cli_3.6.1 rstudioapi_0.14
#> [17] curl_5.0.0 yaml_2.3.7 xfun_0.39 fastmap_1.1.1
#> [21] withr_2.5.0 dplyr_1.1.2 knitr_1.42 generics_0.1.3
#> [25] fs_1.6.1 vctrs_0.6.2 hms_1.1.3 grid_4.2.3
#> [29] tidyselect_1.2.0 glue_1.6.2 R6_2.5.1 gifski_1.12.0
#> [33] fansi_1.0.4 rmarkdown_2.21 farver_2.1.1 tweenr_2.0.2
#> [37] magrittr_2.0.3 scales_1.2.1 htmltools_0.5.5 colorspace_2.1-0
#> [41] labeling_0.4.2 utf8_1.2.3 stringi_1.7.12 munsell_0.5.0
#> [45] crayon_1.5.2
showtext hacks into the current graphic device and it is very likely that the multiple calls gganimate has to do somehow messes up showtext. I'm not going to do something special to accommodate the showtext hack as there are now much better and supported ways to get modern font support in R (e.g. using the ragg devices)