0

I am trying to export some unicode characters in the U+1xxxx format from R to pdf using ggplot2 (this is one example: https://unicode-table.com/en/1F321/). In R, the output is great when I have it in the ggtitle as ggtitle("Temperature range \U1F321"):

enter image description here

However, when I export the plot to pdf it appears as these blocks below:

enter image description here

I have tried many things that other people suggested (such as using cairo pdf and the showtext package) but none of it worked. I am using Windows.

Thank you very much!

EDIT: I'm saving as pdf using pdf("example.pdf") or cairo_pdf("example.pdf")

Reproducible code:

ggplot(mtcars, aes(x=cyl, y=mpg)) + geom_point() + ggtitle("Temperature range \U1F321") 

cairo_pdf("plot_cairo.pdf")
ggplot(mtcars, aes(x=cyl, y=mpg)) + geom_point() + ggtitle("Temperature range \U1F321") 
dev.off()

pdf("plot_normal.pdf")
ggplot(mtcars, aes(x=cyl, y=mpg)) + geom_point() + ggtitle("Temperature range \U1F321") 
dev.off()

Also, just want to state my intentions more clearly. I'm just aiming to have a high-resolution ggplot exported from R to Word. When I export it as an image, the characters show well but the resolution is of poor quality. Following suggestions from other users, I'm therefore first exporting it as pdf and then importing it to Word as an object from the "Insert" tab:

enter image description here

Using this method, the plot is of much better resolution in Word, but the unicode characters don't show up well. So if maybe someone else has another suggestion on how to get a high quality ggplot exported from R to Word while still preserving the unicode characters, that would also work! Thank you!!

Darren LSH
  • 35
  • 7
  • How exactly are you saving to PDF? It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and code that can be used to test and verify possible solutions. – MrFlick Sep 03 '21 at 21:28
  • Sorry all. I've just added more details. Could there be a workaround to what I'm trying to achieve? I really need to use a thermometer icon and a fish icon (https://unicode-table.com/en/1F420/). – Darren LSH Sep 03 '21 at 23:26
  • Did you try using a surrogate pair for a character above BMP? In your case, try `"Temperature range \ud83c\udf21"`. – JosefZ Sep 04 '21 at 09:42
  • @JosefZ Just tried it, but it still doesn't work :( Thanks for the suggestion though! – Darren LSH Sep 04 '21 at 14:53
  • 1
    Does the font you are using contain the according character, and is that font embeddable? – Max Wyss Sep 04 '21 at 14:54
  • @MaxWyss Right I don't think it does. I need to use Sans Serif. – Darren LSH Sep 04 '21 at 23:14
  • @KJ Haha no worries thanks! Might have to use this if I don't find another solution. – Darren LSH Sep 04 '21 at 23:23
  • Hi all, just an update. Last thing I've been trying is to have 2 different fonts in the ggtitle (if you use this link you can see which font works for the thermometer icon: https://www.fileformat.info/info/unicode/char/1f321/fontsupport.htm). Using showtext, I can download the required font and it works when all of the title is of the same font, however I need the base font to be Sans Serif (with just the icon being Symbola - for eg.). Been searching the web how to mix fonts but so far the only thing I've seen is how to have fonts of different sizes/bold and unbold/italic and normal, etc. – Darren LSH Sep 06 '21 at 22:25
  • @DarrenLSH This might be an instance of the R-for-Windows character encoding bug. See https://stackoverflow.com/a/17717284/8436923 and https://stackoverflow.com/a/46179516/8436923 but this is (reportedly) fixed in R 4.2.0; see https://stackoverflow.com/a/72009911/8436923 – Montgomery Clift May 03 '22 at 02:27

0 Answers0