关于r:修复轴标签中的字体

Fix typography in axis labels

序言:我想从R创建发布级图形,而无需进行后处理。我所的其??他研究人员总是在图形软件(例如Adobe Illustrator)中执行后处理。我希望避免这种情况。

我的抱怨是R对负数没有使用正确的减号(尤其是在绘图轴上):

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

plot with negative numbers

(我已圈出违法者供您考虑。)

作为某种印刷风格的书呆子(确实如此!请检查我的职业简历!),这是不可接受的。我需要使用正确的Unicode字符????? ???? (U 2212,"?")在这里。我的一个朋友通过在发布之前替换Adobe Illustrator中的所有减号来实现此目的,但我不禁认为,必须有一种更好的方法-从R内实现此目标;并且不会强迫我手动替换所有轴标签。

(我目前未使用ggplot2,但如果有一种仅适用于ggplot2的解决方案,我会很乐意使用。)


也许是手动绘制轴和标签,而不是接受默认设置?

1
2
3
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=""))

enter image description here


另一种方法与Joshua Ulrich提供的方法几乎相同,不同之处在于您可以让R计算轴刻度:

1
2
3
4
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)

enter image description here


这是使用ggplot的方法。 pdf设备不会呈现unicode符号,因此请使用cairo_pdf

1
2
3
4
5
6
7
8
9
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)

这是一种修复格子图的轴标签的技巧。想法是将代码插入低级功能panel.axis()中,该功能由大多数/所有高级绘图功能调用。

此处(上下两行上下文)是您在调用library(lattice); trace("panel.axis", edit=TRUE)之后插入"手工"的代码:

1
2
3
4
5
6
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",

一个更好的选择(一旦您确定了at=参数的正确值(并在此处查看执行此操作的几种方法)),运行以下命令:

1
2
3
4
5
6
library(lattice)

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

在我检查过的许多屏幕或文件图形设备中的任何一个上,打勾标签上会显示哪个正确的减号

要在自己的屏幕设备上进行比较,请直接在上方运行代码块,然后:

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

新程序包按照OP的要求进行签名。这是网站上的示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
library(dplyr)
library(ggplot2)
library(ggrepel)

theme_set(theme_gray())
theme_update(panel.grid.minor = element_blank())

p <-
  ggplot(sleep) +
  aes(group, extra) +
  geom_point() +
  xlab("Drug") +
  ylab("Extra Sleep (hours)")

label_hours <- function(mapping) {
  geom_text_repel(
    mapping,
    nudge_x       = -.1,
    direction     ="y",
    segment.size  = .4,
    segment.color ="grey75",
    hjust         ="right"
  )
}

p +
  label_hours(
    mapping = aes(
      label = case_when(
        group == 1 ~ signs(extra, accuracy = .1), # Unicode minuses
        group == 2 ~ number(extra, accuracy = .1) # ASCII minuses
      )
    )
  ) +
  scale_y_continuous(
    limits = c(-4, 6),
    breaks = seq(-4, 6),
    labels = signs_format(accuracy = .1) # Unicode, analogous to number_format()
  )

Here is the plot on the website.