问题
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 <...>
回答1:
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)))
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/6615161/how-do-i-write-a-plot-containing-a-symbol-to-pdf-in-r