Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to use the infinity sign on the x-axis of a box plot in R, which I want to write to a PDF file.

I can set the infinity symbol by doing

names(data)[9] <- "∞"

but that gets me encoding errors when trying to write:

conversion failure on '∞' in 'mbcsToSbcs': dot substituted for <...>
like image 486
htorque Avatar asked Jul 07 '11 18:07

htorque


3 Answers

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)))
like image 85
Ben Bolker Avatar answered Nov 14 '22 22:11

Ben Bolker


I'm not entirely sure how you're trying to place the label, but the following code works for me:

x <- 1:10
y <- 1:10

pdf("infty.pdf")
plot(x,y,xlab=expression(infinity))
dev.off()

in that it produces a PDF with the x axis labelled with an infinity symbol. For mathematical symbols, I would recommend not trying to store them as characters and expecting R to treat them like it does other characters. See ?plotmath for more information.

like image 34
joran Avatar answered Nov 14 '22 23:11

joran


I had a similar problem on MacOS with the symbols for male (mars unicode \u2642) and female(venus unicode \u2640). pdf() would not plot them, replacing them by dots.

I then installed Cairo and at first that did not work either (instead it replaced the symbols with rectangles) until I typed

cairo_pdf(pdf.file,family="Arial Unicode MS")

which works. The problem is to find a font with the symbol you want defined, so there is no guarantee that it will work for other symbols.

like image 37
Richard Mott Avatar answered Nov 14 '22 22:11

Richard Mott