0

I have data which looks like this:

Data picture

And here is my code:

ggplot(df, aes(Month.of.Death, fill=as.factor(Month.of.Death)))+
  geom_bar(color="black",fill="brown3")+
  theme(text = element_text(size=15),axis.text.x = element_text(angle=90, hjust=1))+
  labs(title = "Liczba śmierci dziennikarzy w danym miesiącu", x="Miesiąc", y="Liczba dziennikarzy")

How chart looks like

So now what is my problem. I was trying to make put these columns in order but I do not know how to do it. I have been trying to do it using this:

miesiac <- c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "Novermber", "December")

but when I try to add it as my new X axis it says it has different amount of elements then Y. I do understand the problem but I am unable to find solution I saw people in internet doing it this way but either I am stupid or it does not work for my problem. Please tell me how to sort these columns. Thanks in advance!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • If you have a specific order you need (in this case, the order of months), you should make that variable a factor. Otherwise ggplot will order alphabetically. Your chart shows 13 categories, but the axis labels you gave have 12, and only 11 match what's in the data: "unknown" is missing and November is misspelled. Beyond that, there are lots of SO posts on ordering axis labels already; we'd need to have a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to know how this isn't covered by any of those – camille Dec 27 '19 at 18:00
  • Also see https://stackoverflow.com/q/20524408/5325862, https://stackoverflow.com/q/30217594/5325862, https://stackoverflow.com/q/12774210/5325862, https://stackoverflow.com/q/35559058/5325862 – camille Dec 27 '19 at 18:21

1 Answers1

0

You should try to add scale_x_discrete functions and pass the ordering in the argument limits:

ggplot(df, aes(Month.of.Death, fill=Month.of.Death))+
  geom_bar(color="black",fill="brown3")+
  theme(text = element_text(size=15),axis.text.x = element_text(angle=90, hjust=1))+
  labs(title = "Liczba śmierci dziennikarzy w danym miesiącu", x="Miesiąc", y="Liczba dziennikarzy")+
  scale_x_discrete(limits = c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December","Unknown"))

If this is not working, please consider to add a reproducible example of your dataset as specified here: How to make a great R reproducible example

dc37
  • 15,840
  • 4
  • 15
  • 32