0

I am working on a project and trying to make some graphics. I am pretty new to coding, so any help would be much appreciated. I created a graph, but the labels at the bottom are overlapping, so I tried adding in geom_text_repel() and geom_label_repel() by themselves at different places in the code and I keep getting the error message: "Error in FUN(X[[i]], ...) : object 'prop' not found". I also tried adding ggrepel to the aes layer and I got the error: "Error: stat_count requires the following missing aesthetics: x". Does anyone have any ideas on how to get ggrepel to work with my code or another way to have the labels not overlap? Here is my code:

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

And an image of the original graph: (Original Graph

  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jun 09 '20 at 16:03
  • 1
    Some ideas here: https://stackoverflow.com/questions/50399838/how-to-alternate-a-new-line-for-overlapping-x-axis-labels. I think `ggrepel` is just for labels on the plot itself, not for axis labels. – MrFlick Jun 09 '20 at 16:04

1 Answers1

0

One option you have is to simply rotate your labels. Try adding

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

to your ggplot.

Example:

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

Results in:

enter image description here

Whereas

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

Results in

enter image description here

davidnortes
  • 872
  • 6
  • 14