【问题标题】:Automatically vary the positions of labels with geom_text when they overlie each other当它们相互重叠时,自动改变带有 geom_text 的标签的位置
【发布时间】:2016-01-06 21:43:06
【问题描述】:

我想创建一个带有标签而不是点的 ggplot 图,但它们相互重叠,因此您无法阅读它们。有没有一种很好的方法来自动移动它们,使其不会相互覆盖?

df = data.frame(x = c(1,4,5,6,6,7,8,8,9,1), y = c(1,1,2,5,5,5,3,5,6,4),
                label = rep(c("long_label","very_long_label"),5))
ggplot(data=df) + geom_text(data=df,aes(x=x, y=y, label = label))

谢谢

【问题讨论】:

  • 你看过this discussion吗?
  • 有一个新的包'ggrepel'应该可以帮助你。可以在 github 上找到。
  • 感谢你们!这个包“greppel”就是我要找的!

标签: r graphics ggplot2 label geom-text


【解决方案1】:

ggrepel(昨天 - 2016 年 1 月 9 日发布给 CRAN)似乎是为这些情况量身定做的。但请注意:ggrepel 需要 ggplot2 v2.0.0

df = data.frame(x = c(1,4,5,6,6,7,8,8,9,1), y = c(1,1,2,5,5,5,3,5,6,4),
                label = rep(c("long_label","very_long_label"),5))

library(ggplot2)
library(ggrepel)

# Without ggrepel
ggplot(data = df) + 
   geom_text(aes(x = x, y = y, label = label))

# With ggrepel
ggplot(data = df) + 
     geom_text_repel(aes(x = x, y = y, label = label), 
             box.padding = unit(0.1, "lines"), force = 2,
             segment.color = NA) 

【讨论】:

    猜你喜欢
    • 2021-06-20
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    相关资源
    最近更新 更多