【问题标题】:How to repel labels in ggplot如何在ggplot中排斥标签
【发布时间】:2021-08-11 08:39:53
【问题描述】:

如果下面示例中的标签是重叠的,我们如何实现排斥选项?谢谢!

means <- df %>% 
  group_by(cyl) %>% 
  summarize(across(c(wt, mpg), mean))
ggplot(df)  +
  aes(x=wt, y=mpg, color=cyl, shape=cyl) + 
  geom_point() + 
  geom_point(size=4, data=means) + 
  geom_label(aes(label=cyl), color="black", data=means) -> fig
fig

如果我从ggrepel package 添加geom_label_repel()

 fig + geom_label_repel()

我得到错误:

geom_label_repel requires the gollowing missing aesthetics: label

【问题讨论】:

  • 您是否尝试将geom_label(aes(label=cyl), color="black", data=means) 替换为geom_label_repel(aes(label=cyl), color="black", data=means)
  • 做到了,谢谢。如果您将此写到单独的答案中,我可以接受。

标签: r ggplot2 ggrepel


【解决方案1】:

您需要映射label 以便geom_label_repel “看到”它。它没有直接查看其他几何图形的映射。只是它本身和顶部的ggplot 调用。因此,您有两个选择。

直接在函数内

geom_label_repel(mapping = aes(label = cyl))

或在顶部ggplot 通话

ggplot(data = df, mapping = aes(label = cyl)) +

请注意,如果您想标记means 点,您可能必须指定data,就像评论中提到的文森特一样。

【讨论】:

    猜你喜欢
    • 2014-07-02
    • 1970-01-01
    • 1970-01-01
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多