Unicode character not shown in pdf file after embeding fonts with extrafonts package

639 views Asked by At

I am trying to embed the ≤ sign into the fonts of my pdf plot. The ≤ sign is shown in the plot when viewed within RStudio (p1). However, when I save the plot and embed the fonts the ≤ sign is converted to an = sign.

Using the extrafont package I want to save my plot with the CM Roman font. I have tried alternative methods with the device set as cairo_pdf in ggsave(). This embeds the ≤ sign into the pdf, but the font is no longer CM Roman.

What needs to be done so that the ≤ sign remains in the plot and the font is CM Roman?

library(ggplot2)
library(extrafont)

font_import()
font_install("fontcm")
loadfonts()

df <- data.frame(foo = c(2, 4, 8 , 16),
                 bar = factor(c(1:3, "4\u2264")))


p1 <- ggplot(df, aes(x = bar, y = foo)) + 
  geom_bar(stat="identity") +
  labs(title = "p1: Equality sign shows in RStudio plot") +
  theme(text = element_text(family = "CM Roman", size = 25))

p2 <- ggplot(df, aes(x = bar, y = foo)) + 
  geom_bar(stat="identity") +
  labs(title = "p2: Equality sign not shown in .pdf file") +
  theme(text = element_text(family = "CM Roman", size = 25)) 

print(p1)
[![p1 plot][1]][1] # there should be an image of p1 here...

# Sys.setenv(R_GSCMD = "C:/Program Files/gs/gs9.53.3/bin/gswin64c.exe") #~ run once at start of each R session  
ggsave("p2.pdf", p2, width = 15, height = 10, units = "in")
embed_fonts("p2.pdf", outfile="p2.pdf")
[![p2 plot][1]][1] # there should be an image of p2 here...
1

There are 1 answers

0
BanffBoss122 On

ggsave losing unicode characters from ggplot+gridExtra

There are several solutions in this post. I think it has to do something with encoding, but I am not 100% sure.