Closed minimaxir closed 4 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\"'>  </span>") +
theme(plot.title = element_markdown())
Created on 2019-08-09 by the reprex package (v0.3.0)
Ah, using a CSS style
attribute is a clever hack!
I think this example by @clauswilke should be included in the documentation (vignettes) of the package.
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.
And this is the output locally on my windows 10 machine
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'></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.
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.
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.
That didn't work either.
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.
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
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).