Fix typography in axis labels

十年热恋 提交于 2019-11-30 01:21:46

问题


Preamble: I want to create publication-grade graphics from R without postprocessing. Other researchers at my institute always perform postprocessing in a graphics software (such as Adobe Illustrator). I am hoping to avoid this.

My gripe is that R doesn’t use the correct minus sign for negative numbers (especially in plot axes):

plot(-20:-1, rnorm(20) + 1 : 20)

(I’ve circled the offenders for your consideration.)

As somewhat of a typography nerd (it’s true! Check my Careers CV!) this is unacceptable. I need to use the correct Unicode character ᴍɪɴᴜꜱ ꜱɪɢɴ (U+2212, “−”) here. A friend of mine achieves this by replacing all minus signs in Adobe Illustrator prior to publication but I can’t help but think that there must be a better way – from within R – to accomplish this; and one that doesn’t force me to manually replace all axis labels.

(I’m not currently using ggplot2 but if there’s a solution which only works with ggplot2 I’ll gladly take it.)


回答1:


Perhaps draw the axis and labels manually, rather than accepting the defaults?

plot(-20:-1, rnorm(20) + 1 : 20, xaxt="n")
Axis(-20:-1, at=seq(-20,-5,5), side=1,
  labels=paste("\U2212",seq(20,5,-5),sep=""))




回答2:


Another way which is almost the same as the one provided by Joshua Ulrich, except that you can let R compute the axis ticks :

plot(-20:-1, rnorm(20) + 1 : 20, xaxt="n")
at <- axTicks(1, usr=par("usr")[1:2])
labs <- gsub("-", "\U2212", print.default(at))
axis(1, at=at, labels=labs)




回答3:


Here is how to do it with ggplot. The pdf device doesn't render the unicode symbols, so use cairo_pdf instead.

unicode_minus <- function(x) sub('^-', '\U2212', format(x))

# change the default scales
scale_x_continuous <- function(..., labels=unicode_minus)
                        ggplot2::scale_x_continuous(..., labels=labels)
scale_y_continuous <- function(..., labels=unicode_minus)
                        ggplot2::scale_y_continuous(..., labels=labels)

qplot(-20:-1, rnorm(20) + 1:20)



回答4:


Here's a hack that fixes the axis labels of lattice plots. The idea is to insert code in the low-level function panel.axis() which is called by most/all of the higher level plotting functions.

Here (with a couple of lines of context above and below) is the code you would insert "by hand" following a call to library(lattice); trace("panel.axis", edit=TRUE):

if (draw.labels && !is.null(labels)) {
    {
        labels <- gsub("-", "\U2212", labels)  ## <- My addition
        Encoding(labels) <- "UTF-8"            ## <- My addition
        just <- if (outside) 
            switch(side, bottom = if (rot[1] == 0) c("centre", 

A better option (once you've divined the correct value of the at= argument (and see here for several ways to do that)) is to run this:

library(lattice)

trace(what = "panel.axis",
      tracer = quote({labels <- gsub("-", "\U2212", labels)
                      Encoding(labels) <- "UTF-8"}),
      at = list(c(30,3,2,2)))

Following which proper minus signs in tick labels are printed on any of the many screen or file graphics devices that I've checked

To compare on your own screen device, run the code block directly above and then:

xyplot(1:10 ~ -1:-10)
untrace("panel.axis")
windows()
xyplot(1:10 ~ -1:-10)


来源:https://stackoverflow.com/questions/14780860/fix-typography-in-axis-labels

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