0

I'm trying to make a simple bargraph in ggplot to show the number of police officer deaths attributed to various causes each year. The widths of the columns is not consistent. For example, deaths due to Covid were only in 2020, and the pink bar showing that is as wide as all the bars for each year in other categories. How can I made the bars all be the same width even when there is only one year that has data in a given cause of death?

Data table "cause"

"cause"

ggplot(data = cause, aes(x=causeMonthState, y=deaths, fill = as.factor(year)))+
  geom_bar(stat="identity", position = "dodge", width = .5)+
  theme(axis.text.x = element_text(angle = 90))

enter image description here

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58
kxb35
  • 41
  • 5
  • 2
    Try `position = position_dodge(preserve = "single")`. – stefan Sep 02 '20 at 18:16
  • Perfect, thank you! – kxb35 Sep 02 '20 at 18:20
  • 1
    You may want to use `position_dodge2(preserve="single")` instead of `position_dodge()`. The difference is that `position_dodge()` will preserve the positioning of each of your years, whereas `position_dodge2()` will maintain positioning such that the bars are a group which is centered on your x value. Try both and you should see what I mean. – chemdork123 Sep 02 '20 at 18:46

0 Answers0