'Changing Plot Font with Cairo in R

I have found R's default plots to be poorly aliased. As a solution, I set Cairo as the graphics device, and now the plots look much better.

Unfortunately, using Cairo has created another issue, which is that for some reason, I am not able to apply the font that I was using when the graph was displayed in the plot window (in the left-hand diagram above, Cambria is used, but the right-hand diagram fails to apply this font).

Here is my code:

library(readxl)
library(scales)
library(ggplot2)
library(dplyr)
library('Cairo')

windowsFonts(Cam = windowsFont("Cambria"))

dataset <- read_excel('CW Data.xlsx')
colnames(dataset)[4] <- "Broadband Subs (%)"

options(scipen = 1000)

# Scatter plot FDI~GDP with regression line
CairoWin()
ggplot(dataset, aes(x=`2019 GDP ($bn)`, y=`2019 FDI ($m)`)) + 
  geom_point(size=3, shape=1) +
  geom_smooth(method='lm',formula=y~x, se=FALSE, color='black') +
  scale_x_continuous(label = comma) + scale_y_continuous(label=comma) +
  theme(panel.background = element_rect(fill="peachpuff"), 
        plot.background = element_rect(fill="peachpuff")) +
  theme(panel.grid.major = element_line(colour = "gray72"), 
        panel.grid.minor = element_line(colour = "gray72")) + 
  theme(text = element_text(family = "Cam")) 

ggsave("FDI~GDP.png", device="png", type = "cairo") 

And here is a OneDrive link for the Excel data that I am using

https://1drv.ms/x/s!AvGKDeEV3LOs4gNr714Ie0KbOjhO?e=bkdPvk



Solution 1:[1]

I suggest you have a look at the packages ragg and systemfonts. They make working with fonts extremly easy and the results are better than the output of the base options.

First, I suggest you query all available fonts using View(systemfonts::system_fonts()). You can select every font present here and use it for plotting or saving a plot.

I recreated your plot using a built in dataset as the onedrive link you shared was broken. I used the Cambria font like this.

plot <- ggplot(dataset, aes(x = mpg, y = hp)) +
  geom_point(size = 3, shape = 1) +
  geom_smooth(
    method = 'lm',
    formula = y ~ x,
    se = FALSE,
    color = 'black'
  ) +
  scale_x_continuous(label = comma) +
  scale_y_continuous(label = comma) +
  labs(x = "2019 GDP ($bn)", y = "2019 FDI ($m)") +
  theme(
    panel.background = element_rect(fill = "peachpuff"),
    plot.background = element_rect(fill = "peachpuff")
  ) +
  theme(
    panel.grid.major = element_line(colour = "gray72"),
    panel.grid.minor = element_line(colour = "gray72")
  ) +
  theme(text = element_text(family = "Cambria")) # relevant line

I prefer saving the plot in an object and passing it explicitly to the save function.

ggsave(
  "FDI~GDP.png", 
  plot = plot, 
  device = ragg::agg_png, # this is the relevant part
  width = 1920, 
  height = 1080,
  units = "px"
) 

Here is the result:

plot

I would say it worked flawlessly. You can also use ragg as your graphics device in RStudio to make this more consistent. Have a look here.

If you want to output the plot to a PDF, you can use showtext to register system fonts with all newly opening graphics devices. So what you need to do is:

library(showtext)
showtext_auto()
ggsave(
  "FDI~GDP.pdf", 
  plot = plot,
  width = 1920, 
  height = 1080,
  units = "px"
) 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1