【问题标题】:How to combine repelling labels and shadow or halo text in ggplot2?如何在 ggplot2 中结合排斥标签和阴影或光环文本?
【发布时间】:2019-06-27 21:07:37
【问题描述】:

对于 ggplot2 中的排斥标签(例如ggrepel)或标签的阴影文本(例如ggshadowtextthis answer),有一些很好的解决方案。但是没有任何东西可以让我们将这两个功能结合起来。

我尝试了这种在略有不同的位置多次打印标签的技巧,但它不适用于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 repo,但是一直没有回复(也许不可能吧?)

【问题讨论】:

    标签: r ggplot2 ggrepel


    【解决方案1】:

    这个功能现在已经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)
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多