如何在 ggplot2 中组合排斥标签和阴影或光晕文本?

How to combine repelling labels and shadow or halo text in ggplot2?

ggplot2 中的任一排斥标签都有一些不错的解决方案(例如 ggrepel) or shadow text for labels (e.g. ggshadowtext and this answer)。但是没有任何东西可以让我们将这两个功能结合起来。

我试过这个 hack,它在略有不同的位置多次打印标签,但它与 geom_text_repel

不兼容
library(ggplot2)

# subset data
d <- diamonds[1:20,]

# make plot
p <- ggplot(d, aes(carat, price)) +
  geom_point()

# make halo layers
theta <- seq(pi / 8, 2 * pi, length.out = 16)
xo <- diff(range(d$carat)) / 200
yo <- diff(range(d$price)) / 200
for (i in theta) {
  p <- p + 
    geom_text_repel(data = d, 
              aes_q(x = bquote(carat + .(cos(i) * xo)),
                    y = bquote(price + .(sin(i) * yo)),
                    label = ~cut),
              size = 6,
              colour = 'black', 
              seed = 1,
              segment.colour = NA)
}

# update plot with halo and interior text
p <- p + geom_text_repel(aes(label = cut),
                   size = 6,
                   colour = 'white', 
                   seed = 1,
                   segment.colour = "grey80")

p

很慢,而且在标签比较近的地方,这个方法无效:

我们如何获得适用于 geom_text_repel 的阴影文本?我 posted an issue 前段时间向 ggrepel GitHub 回购,但一直没有回复(也许这是不可能的?)

此功能现已 added 到 ggrepel 包中,太棒了!

library(ggrepel)
library(ggplot2)

dat <- mtcars
dat$car <- rownames(dat)

ggplot(dat) +
  aes(wt, 
      mpg, 
      label = car) +
  geom_point(colour = "red") +
  geom_text_repel(bg.color = "white",
                  bg.r = 0.25)