Using Unicode 'Dingbat-Like' Glyphs in R Graphics, Across Devices & Platforms, Especially Pdf

using Unicode 'dingbat-like' glyphs in R graphics, across devices & platforms, especially PDF

I think you are out of luck Ben, as, according to some notes by Paul Murrell, pdf() can only handle single-byte encodings. Multi-byte encodings need to be converted to a the single-byte equivalent, and therein lies the rub; by definition, single-byte encodings cannot contain all the glyphs that can be represented in a multi-byte encoding like UTF-8, say.

Paul's notes can be found here wherein he suggests a couple of solutions using Cairo-based PDF devices, using cairo_pdf() on suitably-endowed Linux and Mac OS systems, or via the Cairo package under MS Windows.

Encoding KNIME R-Snippet in pdf export (Special Characters)

It was working well for me (also Windows, in my case Windows 10, 64 bit, English locale, KNIME 3.1.2, R 3.0.3).

You might give a try with the following change:

df <- data.frame("crit \u00B0C", 1)

(\u00B0 stands for the unicode degree sign.)

Unicode Characters in ggplot2 PDF Output

As Ben suggested, cairo_pdf() is your friend. It also allows you to embed non-postscript fonts (i.e. TTF/OTF) in the PDF via the family argument (crucial if you don't happen to have any postscript fonts that contain the glyphs you want to use). For example:

library(ggplot2)
cairo_pdf("example.pdf", family="DejaVu Sans")
qplot(Sepal.Length, Petal.Length, data=iris, main="Aʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘǫʀsᴛᴜᴠᴡxʏᴢ")
dev.off()

...gives a PDF that looks like this:
ggplot2 graph with custom font family and non-ASCII characters in the title

See also this question; though it doesn't look directly relevant from the title, there is a lot in there about getting fonts to do what you want in R.

EDIT per request in comments, here is the windows-specific code:

library(ggplot2)
windowsFonts(myCustomWindowsFontName=windowsFont("DejaVu Sans"))
cairo_pdf("example.pdf", family="myCustomWindowsFontName")
qplot(Sepal.Length, Petal.Length, data=iris, main="Aʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘǫʀsᴛᴜᴠᴡxʏᴢ")
dev.off()

To use the base graphics command cairo_pdf() it should suffice to just define your font family with the windowsFonts() command first, as shown above. Of course, make sure you use a font that you actually have on your system, and that actually has all the glyphs that you need.

TThe instructions about DLL files in the comments below are what I had to do to get the Cairo() and CairoPDF() commands in library(Cairo) to work on Windows. Then:

library(ggplot2)
library(Cairo)
windowsFonts(myCustomWindowsFontName=windowsFont("DejaVu Sans"))
CairoPDF("example.pdf")
par(family="myCustomWindowsFontName")
qplot(Sepal.Length, Petal.Length, data=iris, main="Aʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘǫʀsᴛᴜᴠᴡxʏᴢ")
dev.off()

How do I write a plot containing a symbol to PDF in R?

You can either use Unicode as in this example: using Unicode 'dingbat-like' glyphs in R graphics, across devices & platforms, especially PDF (infinity is Unicode 8734): in particular, it's possible that simply using a cairoPDF device will make this work. Probably a better idea is to use ?plotmath capabilities (as referenced in ?boxplot under the names argument:

 boxplot(matrix(1:10,ncol=2),names=c("a",expression(infinity)))

Plotting symbols fails in PDF

The problem is that your default font does not have "‰" (which I would speak as "per mil") as the glyph that is produced with \u0028. You need to change to a font that does have that glyph:

?pdfFonts

This is what I get with my setup where there is no problem (at least as I understand ti.)

> str(pdfFonts("sans"))
List of 1
$ sans:List of 3
..$ family : chr "Helvetica"
..$ metrics : chr [1:5] "Helvetica.afm" "Helvetica-Bold.afm" "Helvetica-Oblique.afm" "Helvetica-BoldOblique.afm" ...
..$ encoding: chr "default"
..- attr(*, "class")= chr "Type1Font"

R and unicode PDF fonts on linux - letters got stuck together

Ah, it was easy.

I had to use cairo_pdf() instead of the default PDF device. That did the trick.



Related Topics



Leave a reply



Submit