wilkelab / ggtext

Improved text rendering support for ggplot2
https://wilkelab.org/ggtext/
GNU General Public License v2.0
655 stars 37 forks source link

Icon Font support #3

Closed minimaxir closed 4 years ago

minimaxir commented 5 years ago

From the demos which support HTML, it seems like you could support icon fonts like Font Awesome, which would be interesting for annotations. (although I'm not 100% sure if this package would allow font mixing).

clauswilke commented 5 years ago

This works just fine, as long as you install the font locally and use a graphics device that can access it. On OS X, the quartz device has access to all local fonts, and the following works.

library(ggplot2)
library(ggtext)

ggplot(mtcars) +
  aes(mpg, disp) +
  geom_point() +
  labs(title = "This font is awesome:
       <span style='font-family: \"Font Awesome 5 Free Solid\"'>&#62766; &#62650; &#62577;</span>") +
  theme(plot.title = element_markdown())

Created on 2019-08-09 by the reprex package (v0.3.0)

minimaxir commented 5 years ago

Ah, using a CSS style attribute is a clever hack!

GegznaV commented 4 years ago

I think this example by @clauswilke should be included in the documentation (vignettes) of the package.

ercbk commented 4 years ago

I'm having trouble getting an icon to display on a github action runner.
This is the ouptut the from the ubuntu 18-04 runner. pos-policy-line-2020-07-19

And this is the output locally on my windows 10 machine pos-policy-line-2020-07-19

fonts directory Installation step from the workflow yaml

- name: install fonts
        run: Rscript -e 'library(extrafont); font_import(paths = glue::glue("{rprojroot::find_rstudio_root_file()}/font"), prompt=FALSE); loadfonts()'

Github Action log

Registering fonts with R
Scanning ttf files in /home/runner/work/Indiana-COVID-19-Tracker/Indiana-COVID-19-Tracker/font ...
Extracting .afm files from .ttf files...
/home/runner/work/Indiana-COVID-19-Tracker/Indiana-COVID-19-Tracker/font/Roboto-Black.ttf : Roboto-Black already registered in fonts database. Skipping.
/home/runner/work/Indiana-COVID-19-Tracker/Indiana-COVID-19-Tracker/font/Roboto-BlackItalic.ttf : Roboto-BlackItalic already registered in fonts database. Skipping.
/home/runner/work/Indiana-COVID-19-Tracker/Indiana-COVID-19-Tracker/font/Roboto-Bold.ttf : Roboto-Bold already registered in fonts database. Skipping.
/home/runner/work/Indiana-COVID-19-Tracker/Indiana-COVID-19-Tracker/font/Roboto-BoldItalic.ttf : Roboto-BoldItalic already registered in fonts database. Skipping.
/home/runner/work/Indiana-COVID-19-Tracker/Indiana-COVID-19-Tracker/font/Roboto-Italic.ttf : Roboto-Italic already registered in fonts database. Skipping.
/home/runner/work/Indiana-COVID-19-Tracker/Indiana-COVID-19-Tracker/font/Roboto-Light.ttf : Roboto-Light already registered in fonts database. Skipping.
/home/runner/work/Indiana-COVID-19-Tracker/Indiana-COVID-19-Tracker/font/Roboto-LightItalic.ttf : Roboto-LightItalic already registered in fonts database. Skipping.
/home/runner/work/Indiana-COVID-19-Tracker/Indiana-COVID-19-Tracker/font/Roboto-Medium.ttf : Roboto-Medium already registered in fonts database. Skipping.
/home/runner/work/Indiana-COVID-19-Tracker/Indiana-COVID-19-Tracker/font/Roboto-MediumItalic.ttf : Roboto-MediumItalic already registered in fonts database. Skipping.
/home/runner/work/Indiana-COVID-19-Tracker/Indiana-COVID-19-Tracker/font/Roboto-Regular.ttf : Roboto-Regular already registered in fonts database. Skipping.
/home/runner/work/Indiana-COVID-19-Tracker/Indiana-COVID-19-Tracker/font/Roboto-Thin.ttf : Roboto-Thin already registered in fonts database. Skipping.
/home/runner/work/Indiana-COVID-19-Tracker/Indiana-COVID-19-Tracker/font/Roboto-ThinItalic.ttf : Roboto-ThinItalic already registered in fonts database. Skipping.
Found FontName for 0 fonts.
Scanning afm files in /home/runner/.local/share/renv/cache/v5/R-3.6/x86_64-pc-linux-gnu/extrafontdb/1.0/a861555ddec7451c653b40e713166c6f/extrafontdb/metrics
Font Awesome 5 Free Solid already registered with pdfFonts().
Roboto Black already registered with pdfFonts().
Roboto already registered with pdfFonts().
Roboto Light already registered with pdfFonts().
Roboto Medium already registered with pdfFonts().
Roboto Thin already registered with pdfFonts().

Code chunk from the script

# text styled depending on number of consecutive days and increasing or decreasing trend
neg_one <- glue("<b style='color: #33a532'>{consec_days$num_days[[1]]}</b> day of {consec_days$trend[[1]]} new cases")
pos_one <- glue("<b style='color: #cf142b'>{consec_days$num_days[[1]]}</b> day of {consec_days$trend[[1]]} new cases")
zero_days <- glue("{consec_days$trend[[1]]} new cases")
under_five <- glue("<b style='color: #cf142b'>{consec_days$num_days[[1]]}</b> consecutive days of {consec_days$trend[[1]]} new cases")
five_over <- glue("<b style='color: #cf142b'>{consec_days$num_days[[1]]}</b> consecutive days of {consec_days$trend[[1]]} new cases <span style='font-family: \"Font Awesome 5 Free Solid\"; color: #cf142b'>&#xf071;</span>")
under_neg_one <- glue("<b style='color: #33a532'>{consec_days$num_days[[1]]}</b> consecutive days of {consec_days$trend[[1]]} new cases")

# choose the subtitle text based number of consecutive days and trend
subtitle_dat <- consec_days %>%
   mutate(text = case_when(num_days == 1 & trend == "increased" ~
                              pos_one,
                           num_days == 1 & trend == "decreased" ~
                              neg_one,
                           between(num_days, 2, 4) & trend == "increased" ~
                              under_five,
                           num_days >= 5 & trend == "increased" ~
                              five_over,
                           num_days > 1 & trend == "decreased" ~
                              under_neg_one,
                           TRUE ~ zero_days))
pos_policy_line <- ggplot(cases_dat %>%
                             as_tibble(), aes(x = cumulative_cases, y = daily_cases)) +
   geom_point(color = "#B28330") +
   geom_line(color = "#B28330") +
   scale_y_continuous(limits = c(0, 1200), labels = scales::label_comma()) +
   scale_x_continuous(limits = c(10000, xmax), labels = scales::label_comma()) +
   geom_text(aes(x = 10000, y = 1200, label="Daily Cases"),
             family="Roboto",
             size=4.5, hjust=0.5, color="white") +
   # policy labels, hjust and vjust values depends on label
   geom_label(data=label_dat, aes(x = cumulative_cases,
                                  y = daily_cases,
                                  label= labels,
                                  hjust = hjust, vjust = vjust),
              family="Roboto", lineheight=0.95,
              size=4.5, label.size=0,
              color = "white", fill = "black") +
   # segments connecting policy labels to points
   # stage 2
   geom_curve(
      data = data.frame(), aes(x = 17500, xend = 19000,
                               y = 449, yend = 585),
      color = deep_light[[7]], arrow = arw,
      curvature = -0.20
   ) +
   # stage 3
   geom_curve(
      data = data.frame(), aes(x = 29000, xend = 30000,
                               y = 320, yend = 440),
      color = deep_light[[7]], arrow = arw,
      curvature = -0.20
   ) +
   # stage 4
   geom_segment(
      data = data.frame(), aes(x = 39000, xend = 39750,
                               y = 588, yend = 470),
      color = deep_light[[7]], arrow = arw
   ) +
   # stage 4.5
   geom_curve(
      data = data.frame(), aes(x = 46660, xend = 47280,
                               y = 690, yend = 605),
      color = deep_light[[7]], arrow = arw,
      curvature = 0.20
   ) +
   labs(x = "Cumulative Cases", y = NULL,
        title = "Daily <b style='color:#B28330'>Positive Test Results</b> vs. Cumulative <b style='color:#B28330'>Positive Test Results</b>",
        subtitle = subtitle_dat$text[[1]],
        caption = caption_text) +
   theme(plot.title = element_textbox_simple(size = 16,
                                             color = "white",
                                             family = "Roboto"),
         plot.subtitle = element_textbox_simple(size = 14,
                                                color = "white"),
         plot.caption = element_text(color = "white",
                                     size = 12),
         text = element_text(family = "Roboto"),
         legend.position = "none",
         axis.text.x = element_text(color = "white",
                                    size = 12),
         axis.text.y = element_text(color = "white",
                                    size = 12),
         axis.title.x = element_textbox_simple(color = "white",
                                               size = 13),
         panel.background = element_rect(fill = "black",
                                         color = NA),
         plot.background = element_rect(fill = "black",
                                        color = NA),
         panel.border = element_blank(),
         panel.grid.minor = element_blank(),
         panel.grid.major = element_line(color = deep_rooted[[7]]))

plot_path <- glue("{rprojroot::find_rstudio_root_file()}/plots/pos-policy-line-{data_date}.png")

ggsave(plot_path, plot = pos_policy_line, dpi = "screen", width = 33, height = 20, units = "cm")

Love the package. Use it all the time. Would appreciate any help you can offer. Thank you.

current session info ```r - Session info ----------------------------------------------------------------------------------------------------------------------------------- setting value version R version 3.6.2 (2019-12-12) os Windows 10 x64 system x86_64, mingw32 ui RStudio language (EN) collate English_United States.1252 ctype English_United States.1252 tz America/New_York date 2020-07-20 - Packages --------------------------------------------------------------------------------------------------------------------------------------- package * version date lib source anytime 0.3.7 2020-01-20 [1] CRAN (R 3.6.2) assertthat 0.2.1 2019-03-21 [1] CRAN (R 3.6.2) backports 1.1.5 2019-10-02 [1] CRAN (R 3.6.1) cli 2.0.2 2020-02-28 [1] CRAN (R 3.6.3) colorspace 1.4-1 2019-03-18 [1] CRAN (R 3.6.1) crayon 1.3.4 2017-09-16 [1] CRAN (R 3.6.2) curl 4.3 2019-12-02 [1] CRAN (R 3.6.2) digest 0.6.25 2020-02-23 [1] CRAN (R 3.6.2) dplyr * 0.8.5 2020-03-07 [1] CRAN (R 3.6.3) ellipsis 0.3.0 2019-09-20 [1] CRAN (R 3.6.2) extrafont * 0.17 2014-12-08 [1] CRAN (R 3.6.0) extrafontdb 1.0 2012-06-11 [1] CRAN (R 3.6.0) fansi 0.4.1 2020-01-08 [1] CRAN (R 3.6.2) farver 2.0.3 2020-01-16 [1] CRAN (R 3.6.2) ggplot2 * 3.3.0.9000 2020-04-04 [1] Github (tidyverse/ggplot2@bca6105) ggtext * 0.1.0 2020-03-31 [1] Github (wilkelab/ggtext@24e9cd0) glue * 1.4.0 2020-04-03 [1] CRAN (R 3.6.2) gridtext 0.1.1 2020-02-24 [1] CRAN (R 3.6.3) gtable 0.3.0 2019-03-25 [1] CRAN (R 3.6.1) hms 0.5.3 2020-01-08 [1] CRAN (R 3.6.2) httr 1.4.1 2019-08-05 [1] CRAN (R 3.6.1) labeling 0.3 2014-08-23 [1] CRAN (R 3.6.0) lifecycle 0.2.0 2020-03-06 [1] CRAN (R 3.6.3) lubridate 1.7.4 2018-04-11 [1] CRAN (R 3.6.1) magrittr 1.5 2014-11-22 [1] CRAN (R 3.6.2) markdown 1.1 2019-08-07 [1] CRAN (R 3.6.1) munsell 0.5.0 2018-06-12 [1] CRAN (R 3.6.1) pack 0.1-1 2019-05-28 [1] local pacman 0.5.1 2019-03-11 [1] CRAN (R 3.6.2) pillar 1.4.3 2019-12-20 [1] CRAN (R 3.6.2) pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 3.6.2) prismatic 0.2.0 2019-12-01 [1] CRAN (R 3.6.3) purrr 0.3.4 2020-04-17 [1] CRAN (R 3.6.3) R6 2.4.1 2019-11-12 [1] CRAN (R 3.6.2) Rcpp 1.0.4.6 2020-04-09 [1] CRAN (R 3.6.3) readr 1.3.1 2018-12-21 [1] CRAN (R 3.6.2) renv 0.9.3-30 2020-07-07 [1] Github (rstudio/renv@916923a) rlang 0.4.6 2020-05-02 [1] CRAN (R 3.6.2) rprojroot 1.3-2 2018-01-03 [1] CRAN (R 3.6.1) rstudioapi 0.11 2020-02-07 [1] CRAN (R 3.6.2) Rttf2pt1 1.3.8 2020-01-10 [1] CRAN (R 3.6.2) scales 1.1.0 2019-11-18 [1] CRAN (R 3.6.2) sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.6.2) stringi 1.4.6 2020-02-17 [1] CRAN (R 3.6.2) stringr 1.4.0 2019-02-10 [1] CRAN (R 3.6.2) swatches * 0.5.0 2017-12-21 [1] CRAN (R 3.6.1) tibble 3.0.1 2020-04-20 [1] CRAN (R 3.6.3) tidyr 1.0.2 2020-01-24 [1] CRAN (R 3.6.2) tidyselect 1.0.0 2020-01-27 [1] CRAN (R 3.6.2) tsibble * 0.8.6 2020-01-31 [1] CRAN (R 3.6.2) vctrs 0.2.4 2020-03-10 [1] CRAN (R 3.6.3) withr 2.1.2 2018-03-15 [1] CRAN (R 3.6.1) xml2 1.2.5 2020-03-11 [1] CRAN (R 3.6.3) [1] C:/Users/tbats/Documents/R/Projects/Indiana-COVID-19-Tracker/renv/library/R-3.6/x86_64-w64-mingw32 [2] C:/Users/tbats/AppData/Local/Temp/RtmpUFK2ik/renv-system-library ```


clauswilke commented 4 years ago

Sorry, fonts are handled by the R graphics device. ggtext can display whatever fonts your graphics device can display. In particular, since it works on one graphics device, we know for sure that ggtext is not the problem here.

clauswilke commented 4 years ago

However, one thing you can try is use the agg_png() device from the ragg package. It should be able to use all fonts that are available on your system.

ercbk commented 4 years ago

That didn't work either.

ercbk commented 4 years ago

I had problems with it before, but I may try showtext again. FA installation docs describe the installation using the otf files instead of the ttf and I think showtext can handle those. I don't know... maybe I need to load some linux library I'm not aware of. Its a shame.

ercbk commented 4 years ago

Finally came back to this. Problem was a linux dependency evidently. Just need to install font awesome through apt.

      - name: Install font awesome with deps 
        run: sudo apt-get install -y fonts-font-awesome