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

最后都变了- 提交于 2019-12-10 13:26:20

问题


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!


回答1:


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()



回答2:


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.



来源:https://stackoverflow.com/questions/10438398/any-way-to-disable-the-minus-hack-in-pdf-poscript-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!