I'm trying to print a ggplot2 plot that contains a unicode character (specifically Japanese, but that shouldn't matter) to PDF. System locale is UTF8 on macOS, so I can use Unicode characters everywhere else. Unfortunately, ggsave only prints dots, not the characters. It's not really feasible to replace every individual character with its respective unicode code entry because it would just be too many. I've tried using Cairo as suggested below and in many other places, but that doesn't seem to work on macOS and R4.0.3, or maybe it used to work with earlier R versions but doesn't work anymore.
Save unicode characters to .pdf in R
library(ggplot2)
data("cars")
mpg$manufacturer[mpg$manufacturer == "nissan"] <- "日産"
carshwyplot <- ggplot(data=mpg, aes(x=manufacturer, y=hwy)) +
geom_bar(stat="identity")
ggsave(filename = "carshwyplot.pdf", plot = carshwyplot,
width = 12, height = 6, dpi = 300)
R version 4.0.3 (2020-10-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 10.16
locale:
[1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8
This does not work for me, it just replaces the dots with square blocs.
library(Cairo)
ggsave(filename = "carshwyplot.pdf", plot = carshwyplot,
width = 12, height = 6, dpi = 300, device = cairo_pdf)
Any suggestions?