当ggrepel无法使用时,如何修复重叠的标签?

问题描述 投票:0回答:1

我正在研究一个项目,并试图制作一些图形。我对编码还很陌生,因此任何帮助将不胜感激。我创建了一个图形,但底部的标签重叠,因此我尝试在代码的不同位置单独添加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

r label ggrepel
1个回答
0
投票

您有一个选择就是简单地旋转标签。尝试添加

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

到您的ggplot。

示例:

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

结果:

enter image description here

地点

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

结果在

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.