In R, when saving a plot to a PDF or Postscript file, hyphens in axis labels get turned into minus signs. This, apparently, is by design. According to the documentation for the "postscript" device:
There is an exception [to the normal encoding rules]. Character 45 (‘"-"’) is always set as minus (its value in Adobe ISOLatin1) even though it is hyphen in the other encodings. Hyphen is available as character 173 (octal 0255) in all the Latin encodings, Cyrillic and Greek. (This can be entered as ‘"\uad"’ in a UTF-8 locale.)
Is there any way to turn this feature off?
The problem I'm having is that I often save plots in various formats and, if I follow the suggested "\uad" workaround, I get the expected hyphens in Postscript/PDF output but nothing when rendering my plots to other graphics devices like PNG. I'd rather not have to create two versions of each plot, one for PDF and one for PNG.
If I could disable the "minus hack", the rendering behavior across graphics devices would be consistent, and I could simply "print" a plot to multiple devices to get it in different formats. For example, I'd like to be able to do the following, and have the hyphens render consistently in both PDF and PNG versions of the plot:
p <- qplot(arrival_rate, mean_service_time, data = response_times, ...)
ggsave(p, file = "/tmp/service-scaling.pdf", useDingbats = F)
ggsave(p, file = "/tmp/service-scaling.png")
Thanks for your help!
If your machine supports it (and you can type capabilities()
to learn whether it does), you could instead use cairo_pdf()
. It seems to handle "-"
more like the other plotting devices:
Here, because I might as well include it, is the code I used for the two pdfs above:
cairo_pdf("cairo_pdf.pdf", width=6, height=3.5)
par(mar=c(10,4,4,1))
plot(1:10, type = "n", axes = FALSE,
main = "Plotted using cairo_pdf()",
ylab = "", xlab = "x-y", cex.lab = 10)
dev.off()
pdf("pdf.pdf", width=6, height=3.5)
par(mar=c(10,4,4,1))
plot(1:10, type = "n", axes = FALSE,
main = "Plotted using pdf()",
ylab = "", xlab = "x-y", cex.lab = 10)
dev.off()
There is a workaround for pdf() described here:
replace the "-" hyphen with the unicode character "\255"
or in UTF8 "\uad"
.
This might not print nicely in the R-console, but will in the pdf.
It can easily be substituted using gsub("-", "\uad", "x-y")
:
pdf("pdf.pdf", width=5, height=4)
par(mar=c(6,2,2,2), mfrow=c(2,1))
plot(1:10, type = "n", axes = FALSE,
main = "Default",
ylab = "", xlab = "x-y", cex.lab = 8)
plot(1:10, type = "n", axes = FALSE,
main = "with '\\uad'",
ylab = "", xlab = gsub("-", "\uad", "x-y"), cex.lab = 8)
dev.off()
I ended using this solution because I want to export a pdf in CMYK colormode, which is not possible in cairo_pdf (and the alternative of later conversion to CMYK makes the filesize increase by 10-fold for small files). I hope somebody else can use it.
[Edit]: added a missing " (and this to avoid the 6 character limit)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With