Reputation: 1475
I am struggling to apply fonts in ggplot on macOS and need some help. Hopefully this will help others too as I've searched a lot and tried all sorts to no avail. Fonts that appear to be there are not recognised and then strange spacing happens (which is probably related). In this example, I am using BoB Rudis's FT Theme which uses the Robot Condensed Google font.
There are some reported issues with using that font on a Mac, but these seem to be related to using hrbrthemes::import_roboto_condensed()
function (which only seems to partially download the font) and not via sysfonts::font_add_google
, which is what I am doing.
When I run the attached code, the first 46 warnings are
In grid.Call.graphics(C_text, as.graphicsAnnot(x$label), ... :
font family 'Roboto Condensed Light' not found, will use 'sans' instead
Even though, if I run fonts(), then it returns both 'Roboto Condensed' and 'Roboto Condensed Light'. The fonts are fully installed on my machine and available in other applications. Interestingly, having looked through the entire FT theme definition, I can't see any direct reference to 'Roboto Condensed Light'.
If I change bodyFont to 'Times', I get a similar warning even though that is installed too. Even more strangely, some of the text starts appearing in what is clearly Times.
I did try showtext::showtext_auto()
as this seems to make the scaling more predictable (see here) but with this, no fonts seem to be recognised at all, as if it has a separate font database.
So my questions are:
showtext
use a different font db?I am on an Intel MacBook Pro running Mojave.
install.packages("extrafontdb") # reset extrafont fonttable
devtools::install_github("hrbrmstr/hrbrthemes", force = TRUE)
# Run if you get Segmentation faults. This can happen if you install extrafont from source (1.0.3) which seems to corrupt Rttf2pt1
# library(remotes)
# remotes::install_version("Rttf2pt1", version = "1.3.8")
# Load libraries
pacman::p_load(tidyverse, sysfonts, extrafont, ggforce, ggtext, showtext, cowplot)
library(hrbrthemes)
# Install roboto via font_add_google instead as this import function only installs 3 font files and seems to cause issues as per
# https://github.com/hrbrmstr/hrbrthemes/issues/18#issuecomment-633253069
# hrbrthemes::import_roboto_condensed()
# Download Google Fonts into 'sysfonts'
headingFont <- "Roboto Condensed"
bodyFont <- "Open Sans"
sysfonts::font_add_google(headingFont)
sysfonts::font_add_google(bodyFont)
bodyFont <- "Times" # Try Non Google Font
# Not fully sure what showtext does and whether it uses the same font definition, but seems to be useful in
# setting the scaling correctly as per https://github.com/yixuan/showtext
showtext::showtext_opts(dpi = 300)
# Currently disabled as not even Times works with this
showtext::showtext_auto(enable = FALSE)
# Load fonts into extrafontdb font library - only needed when you've installed new fonts on the device (i.e. outside R))
#extrafont::font_import()
# Documentation suggests you need this to register the fonts with the (PDF) output device.
# There is an option for device = "win". Not clear what is needed for png output etc
extrafont::loadfonts()
# Create Data
x <- runif(n = 20, min = 0, max = 9)
y <- runif(n = 20, min = 0, max = 9)
xend <- runif(n = 20, min = 0, max = 9)
yend <- runif(n = 20, min = 0, max = 9)
data = data.frame(x, xend, y, yend)
# Create Plot
p <- ggplot(data) +
ggforce::geom_link(aes(x = x, xend = xend, y = y, yend = yend, colour = "orange")) +
hrbrthemes::theme_ft_rc(base_family = bodyFont) +
# Allow styling of headings
theme(
plot.title=ggtext::element_markdown(size = 32, margin = margin(0, 0, 0.5, 0, "cm"), family = headingFont),
plot.subtitle=ggtext::element_markdown(size = 14, margin = margin(0, 0, 0.4 , 0, "cm")),
plot.caption=ggtext::element_markdown(size = 8)
) +
# Headings
labs(title = "Main Plot Title which should be in more stylised font",
subtitle = "Subtitle which should be in simpler font along with everything else on the plot",
caption = "7.10.2021 | Visualisation by @ChrisWoodsSays | Random Data")
cowplot::ggdraw(p) +
cowplot::draw_label(label = "First Label", x=0.168, y=0.426, color="white", size=10, fontfamily = bodyFont) +
cowplot::draw_label(label = "Label after first", x=0.465, y=0.560, color="white", size=10, fontfamily = bodyFont)
ggsave("SO.png", height = 18, width = 32, units = "cm")
Upvotes: 3
Views: 830