Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to disable the "minus hack" in PDF/Poscript output?

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!

like image 805
Tom Moertel Avatar asked May 03 '12 19:05

Tom Moertel


2 Answers

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:

enter image description hereenter image description here

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()
like image 174
Josh O'Brien Avatar answered Nov 14 '22 23:11

Josh O'Brien


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"):

enter image description here

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)

like image 35
RaphiS Avatar answered Nov 14 '22 22:11

RaphiS