通过 "top"、"bottom"、"left"、"right"、"center" 等关键字指定 geom_text 的位置

Specify position of geom_text by keywords like "top", "bottom", "left", "right", "center"

我希望在 ggplot 中定位文本而不指定 xy 位置,而是使用关键字,例如在 graphics::legend (也可以通过将 x 设置为列表“bottomright”、“bottom”、“bottomleft”、“left”、“topleft”、“顶部”、“右上”、“右”和“中心”).

假设我正在制作这样的图表。

sp <- ggplot(mpg, aes(hwy, cty, label = "sometext")) +
       geom_point()

我想在每个图形中以相同的方式添加要打印的标签。调用以下命令只是在提供给 aes.

的每个 xy 值处打印文本
sp + geom_text()

我可以操纵提供给 geom_text()xy 数据,以确保文本在图表之间保持相同的相对位置,但是没有一种简单的方法可以按 "top""bottom" 等默认位置调用位置? IE。 sp + geom_text(position = "top").

geom_text 想根据您的数据集绘制标签。听起来您想要在绘图中添加一段文本,在这种情况下,annotate 是更好的选择。无论图中的单位如何,要强制标签出现在相同位置,您可以利用 Inf 值:

sp <- ggplot(mpg, aes(hwy, cty, label = "sometext"))+
  geom_point() +
  annotate(geom = 'text', label = 'sometext', x = -Inf, y = Inf, hjust = 0, vjust = 1)
print(sp)

我避免 annotate 就像瘟疫一样,只是为 geom_text 使用空数据框 data 参数:

ggplot(mpg, aes(hwy, cty, label = "sometext"))+
  geom_point() +
  geom_text(data=data.frame(), aes(label = 'sometext', x = -Inf, y = Inf),
            hjust = 0, vjust = 1)

使用无穷大的解决方案很好,绝对是最简单的选择。

但是,如果您想要更好地控制标签的位置(例如,如果您希望它们居中,或者如果您想要更多 space 在轴线和注释之间),您可以使用一些与绘图标题的 min()max() 进行数学运算,以在顶部、底部、右侧或左侧创建居中标题。下面的代码有点冗长,但如果您的绘图中的值发生变化,仍会正确放置标签。此外,要复制到其他绘图,您无需手动计算值,只需更改 x 和 y 变量的名称即可。

sp <- ggplot(mpg, aes(hwy, cty)) +
  geom_point() +
  theme_classic() +
  annotate("text", label = "top", 
           x = 0.5*(min(mpg$hwy) + max(mpg$hwy)), y = max(mpg$cty), vjust = 1) +
  annotate("text", label = "bottom", 
           x = 0.5*(min(mpg$hwy) + max(mpg$hwy)), y = min(mpg$cty), vjust = 0) +
  annotate("text", label = "right", 
           x =  max(mpg$hwy), y = 0.5*(min(mpg$cty) + max(mpg$cty)), hjust = 1) +
  annotate("text", label = "left", 
            x =  min(mpg$hwy), y = 0.5*(min(mpg$cty) + max(mpg$cty)), hjust = 0)

sp   

当然可以编写一个包装器,但是定义单位和理由的方式使其相当冗长,

library(ggplot2)

qplot(1,1) + 
  annotation_compass('testN') + 
  annotation_compass('testE','E') + 
  annotation_compass('testSW','SW') + 
  annotation_compass('testW','W')

annotation_compass <- function(label,
                               position = c('N','NE','E','SE','S','SW','W','NW'),
                               padding = grid::unit(c(0.5,0.5),"line"), ...){
  position <- match.arg(position)
  x <- switch (position,
    N = 0.5,
    NE = 1,
    E = 1,
    SE = 1,
    S = 0.5, 
    SW = 0,
    W = 0, 
    NW = 0
  )
  y <- switch (position,
               N = 1,
               NE = 1,
               E = 0.5,
               SE = 0,
               S = 0, 
               SW = 0,
               W = 0.5, 
               NW = 1
  )
  hjust <- switch (position,
               N = 0.5,
               NE = 1,
               E = 1,
               SE = 1,
               S = 0.5, 
               SW = 0,
               W = 0, 
               NW = 0
  )
  vjust <- switch (position,
               N = 1,
               NE = 1,
               E = 0.5,
               SE = 0,
               S = 0, 
               SW = 0,
               W = 0.5, 
               NW = 1
  )
  f1 <- switch (position,
                   N = 0,
                   NE = -1,
                   E = -1,
                   SE = -1,
                   S = 0, 
                   SW = 1,
                   W = 1, 
                   NW = 1
  )
  f2 <- switch (position,
                   N = -1,
                   NE = -1,
                   E = 0,
                   SE = 1,
                   S = 1, 
                   SW = 1,
                   W = 0, 
                   NW = -1
  )
  annotation_custom(grid::textGrob(label, 
                                   x=grid::unit(x,"npc") + f1*padding[1] , 
                                   y=grid::unit(y,"npc") + f2*padding[2],
                                   hjust=hjust,vjust=vjust, ...))
}

ggpmisc::geom_text_npc中,xy位置以npc单位(0-1)给出。但是,位置也可以指定为“单词”:

d = data.frame(x = rep(c("left", "center", "right"), each = 3),
               y = rep(c("bottom", "middle", "top"), 3))
d$lab = with(d, paste0(x, "-", y))
d
#        x      y           lab
# 1   left bottom   left-bottom
# 2   left middle   left-middle
# 3   left    top      left-top
# 4 center bottom center-bottom
# 5 center middle center-middle
# 6 center    top    center-top
# 7  right bottom  right-bottom
# 8  right middle  right-middle
# 9  right    top     right-top

ggplot(d) +
  geom_text_npc(aes(npcx = x.chr, npcy = y.chr, label = lab))