Violin125
Violin125

Reputation: 133

Why am I unable to bold or italicize a google font in ggplot?

I am having difficulty stylizing google fonts that were downloaded and saved to my Mac's font book. Including a simple example below with a blank plot area.

ggplot() +
  labs(title = "Title in Bold",
       subtitle = "Subtitle in Italics") +
  xlab("Y Label") +
  ylab("X Label") +
  theme(text = element_text(family = "Raleway"),
        plot.title = element_text(face = 'bold', size = 18),
        plot.subtitle = element_text(face = 'italic')
        )

This yields the below where the correct font is rendered but the bold and italics are not applied to the title and subtitle, respectively. I only get the font in its regular/plain form:

enter image description here

I then try font_import() from extrafont which does return (amongst a lengthier message):

"/Users/username/Library/Fonts/Raleway-Italic-VariableFont_wght.ttf : Raleway-ThinItalic already registered in fonts database. Skipping. /Users/username/Library/Fonts/Raleway-VariableFont_wght.ttf : Raleway-Thin already registered in fonts database. Skipping."

If I change the backend graphics to something like AGG then I get warnings of "In grid.Call.graphics(C_text, as.graphicsAnnot(x$label), ... : Unable to load font: Raleway". Likewise, if I use showtext::showtext_auto() I get the same response.

I am able to utilize Raleway italics/bold in other applications such as Microsoft Word. How can I get these font styles to properly load/register in R?

Session info below in case that's relevant:

R version 4.3.3 (2024-02-29) Platform: aarch64-apple-darwin20 (64-bit) Running under: macOS Sonoma 14.4.1

Matrix products: default BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.11.0

locale: 1 en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/Los_Angeles tzcode source: internal

attached base packages: 1 stats graphics grDevices utils datasets methods base

other attached packages: 1 extrafont_0.19 lubridate_1.9.3 forcats_1.0.0 stringr_1.5.1 dplyr_1.1.4 purrr_1.0.2 readr_2.1.5
[8] tidyr_1.3.1 tibble_3.2.1 ggplot2_3.5.0 tidyverse_2.0.0 RPostgres_1.4.6

loaded via a namespace (and not attached): 1 cfbfastR_1.9.0 DBI_1.2.2 remotes_2.5.0 writexl_1.5.0 rlang_1.1.3 magrittr_2.0.3
[7] snakecase_0.11.1 furrr_0.3.1 compiler_4.3.3 mgcv_1.9-1 png_0.1-8 systemfonts_1.0.6 [13] vctrs_0.6.5 reshape2_1.4.4 sysfonts_0.8.9 rvest_1.0.4 profvis_0.3.8 crayon_1.5.2
[19] pkgconfig_2.0.3 fastmap_1.1.1 backports_1.4.1 ellipsis_0.3.2 labeling_0.4.3 utf8_1.2.4
[25] promises_1.2.1 rmarkdown_2.26 sessioninfo_1.2.2 tzdb_0.4.0 ps_1.7.6 bit_4.0.5
[31] xfun_0.43 showtext_0.9-7 cachem_1.0.8 jsonlite_1.8.8 blob_1.2.4 later_1.3.2
[37] emoji_15.0 jpeg_0.1-10 broom_1.0.5 parallel_4.3.3 R6_2.5.1 stringi_1.8.3
[43] espnscrapeR_0.8.0 extrafontdb_1.0 parallelly_1.37.1 pkgload_1.3.4 assertthat_0.2.1 Rcpp_1.0.12
[49] knitr_1.45 usethis_2.2.3 pacman_0.5.1 nnet_7.3-19 Matrix_1.6-5 httpuv_1.6.15
[55] splines_4.3.3 timechange_0.3.0 tidyselect_1.2.1 rstudioapi_0.16.0 yaml_2.3.8 codetools_0.2-19
[61] miniUI_0.1.1.1 websocket_1.4.1 curl_5.2.1 processx_3.8.4 listenv_0.9.1 pkgbuild_1.4.4
[67] lattice_0.22-6 plyr_1.8.9 withr_3.0.0 shiny_1.8.1 evaluate_0.23 future_1.33.1
[73] RcppParallel_5.1.7 urlchecker_1.0.1 xml2_1.3.6 pillar_1.9.0 generics_0.1.3 chromote_0.2.0
[79] hms_1.1.3 munsell_0.5.0 scales_1.3.0 globals_0.16.3 xtable_1.8-4 emo_0.0.0.9000
[85] glue_1.7.0 janitor_2.2.0 mlbplotR_1.1.0 tools_4.3.3 data.table_1.15.2 fs_1.6.3
[91] grid_4.3.3 Rttf2pt1_1.3.12 devtools_2.4.5 colorspace_2.1-0 nlme_3.1-164 patchwork_1.2.0
[97] showtextdb_3.0 proto_1.0.0 baseballr_1.6.0 cli_3.6.2 fansi_1.0.6 gt_0.10.1
[103] V8_4.4.2 emojifont_0.5.5 gtable_0.3.4 digest_0.6.35 farver_2.1.1 htmlwidgets_1.6.4 [109] memoise_2.0.1 htmltools_0.5.8 lifecycle_1.0.4 httr_1.4.7 mime_0.12 bit64_4.0.5

Upvotes: 0

Views: 100

Answers (2)

Violin125
Violin125

Reputation: 133

It turns out I was not properly loading the font (second line of code below) before attempting showtext::showtext_auto(). Previously, I had included only one of the two arguments in font_add_google.

library(showtext)
font_add_google(name = "Raleway", family = "Raleway") 
showtext_auto()

Upvotes: 0

Yann
Yann

Reputation: 85

I cant open the img since my work blocks it, but when i tried your code it seemed to work.

Might consider this code and see if it works? making use of the tidyverse for mtcars. enter image description here

mtcars %>% 
  ggplot(aes(x = mpg, y = disp))+geom_point()+
  labs(title = "Is it italicized?",
       subtitle = "Is it italicized?",
       caption = "Is it italicized?",
       x = "Miles per gallon",
       y = "Displacement")+
  theme(text =element_text(family = "raleway",
                           face = "italic"))

I installed locally the font. Another option is to try and use the ggtext package:

require(ggtext)
mtcars %>% 
  ggplot(aes(x = mpg, y = disp))+geom_point()+
  labs(title = "Is it italicized?",
       subtitle = "*Is it italicized? It is thanks to ggtext::*",
       caption = "Is it italicized?",
       x = "Miles per gallon",
       y = "Displacement")+
  theme(text =element_text(family = "raleway"),
        plot.subtitle = ggtext::element_markdown()
  )

which generated this: enter image description here

*I use also thematic::thematic_on() and set_theme for convivence, that's why the appearance is a bit different

RVersion: 4.3.2 ggplot2 version": 3.5.0

Upvotes: 1

Related Questions