【问题标题】:How can I fix my overlapping labels when ggrepel isn't working?当 ggrepel 不工作时,如何修复我的重叠标签?
【发布时间】:2020-06-09 15:54:46
【问题描述】:

我正在做一个项目并尝试制作一些图形。我对编码很陌生,所以任何帮助将不胜感激。我创建了一个图表,但底部的标签是重叠的,所以我尝试在代码中的不同位置自己添加 geom_text_repel() 和 geom_label_repel() 并且我不断收到错误消息:“FUN 中的错误(X [[ i]], ...) : 找不到对象'prop'”。我还尝试将 ggrepel 添加到 aes 层,但出现错误:“错误:stat_count 需要以下缺失的美学:x”。有没有人对如何让 ggrepel 使用我的代码或以其他方式使标签不重叠有任何想法? 这是我的代码:

df %>%
  filter(!is.na(`Self Reported Race (roll up)_Cleaned`)) %>%
  ggplot() +
  aes(
    x = C19_Employment,
    y = ..prop..,
    group = `Self Reported Race (roll up)_Cleaned`,
    fill = `Self Reported Race (roll up)_Cleaned`,
    na.rm = TRUE
    ) +
  labs(
    title = "Employment Status by Self-Reported Race",
    x = "Employment Status", 
    y = "Proportion of Race", 
    fill = "Self-Reported Race"
  ) +
  geom_bar(position = "dodge", na.rm = TRUE) +
  theme(legend.position = "bottom")

还有一张原图的图片:(Original Graph

【问题讨论】:

  • 如果您包含一个简单的reproducible example,其中包含可用于测试和验证可能解决方案的示例输入和所需输出,则更容易为您提供帮助。
  • 这里有一些想法:stackoverflow.com/questions/50399838/…。我认为ggrepel 仅用于绘图本身的标签,而不用于轴标签。

标签: r label ggrepel


【解决方案1】:

您可以选择简单地旋转标签。尝试添加

+ theme(axis.text.x = element_text(angle = 90)

到你的 ggplot。

例子:

library(ggplot2)
ggplot(data=iris) + 
       geom_bar(aes(x = Species))

结果:

ggplot(data=iris) + 
       geom_bar(aes(x = Species)) + 
       theme(axis.text.x = element_text(angle = 90))

结果

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    • 1970-01-01
    • 1970-01-01
    • 2015-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多