Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Order Weekday by order in ggplot2/R

The below dataset is categorized by weekdays.

> member_casual   weekday  count
1        casual Wednesday 218101
2        casual   Tuesday 214898
3        casual    Sunday 403728
4        casual  Saturday 468259
5        casual    Friday 289970
6        casual  Thursday 224154

When I plot this in bar graph the weekdays are not in chronological order:

  chart <- ggplot(member_causal_weekly, aes(x=weekday, y=count, fill=weekday))+
  geom_bar(stat="identity")+
  facet_wrap(~member_casual)+
  geom_text(aes(label=count), vjust=1.6, color="white", size=3)+
  theme (axis.text.x=element_text(angle=45, hjust=1))

Graph

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Just reorder factor levels:

  member_causal_weekly$weekday <- factor(member_causal_weekly$weekday,
    levels = c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"))

  chart <- ggplot(member_causal_weekly, aes(x=weekday, y=count, fill=weekday))+
  geom_bar(stat="identity")+
  facet_wrap(~member_casual)+
  geom_text(aes(label=count), vjust=1.6, color="white", size=3)+
  theme (axis.text.x=element_text(angle=45, hjust=1))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading