首页 > 解决方案 > 当 ggrepel 不工作时,如何修复我的重叠标签?

问题描述

我正在做一个项目并尝试制作一些图形。我对编码很陌生,所以任何帮助将不胜感激。我创建了一个图表,但底部的标签是重叠的,所以我尝试在代码的不同位置自己添加 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")

以及原始图的图像:(原始图

标签: rlabelggrepel

解决方案


您拥有的一种选择是简单地旋转标签。尝试添加

+ 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))

结果是

在此处输入图像描述


推荐阅读