Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

Order of weekdays in a bar chart using lubridate and ggplot

$
0
0

When creating a bar plot where the x-axis is Weekdays (using lubridate and ggplot), the order of my weekdays does't follow the chronological "mon, tues, wed... "

I've tried to replicate the problem but as you can see below, in my replication, the order of labels on the x-axis if fine.

library(lubridate)
library(dplyr)

my_data <- data.frame(dates = sample(seq(as.Date('2010/01/01'), as.Date('2020/01/01'), by="day"), 100), 
                      group = rep(c(1,2,3,4), times = 5)) 
my_data <- my_data %>% 
  mutate(Weekdays = wday(dates, label = TRUE)) %>% 
  filter(Weekdays != "Sat"&
           Weekdays != "Sun")


ggplot(my_data, aes(Weekdays))+
  geom_bar()+
  facet_wrap(~group)

Output:

enter image description here

The code below is what I have used with the real data. As you can see the week days are not in order. I'm not sure where my actual code differs from the attempted replication above.

library(ggplot2)

df1 <- filter(df, wday != "Sat")

ggplot(df1, aes(x = wday))+
  geom_bar()+
  facet_wrap(~Grouping)+
  theme(axis.text.x = element_text(angle =90, hjust = 1))

Output:

enter image description here

Any help / advice would be most appreciated.


Viewing all articles
Browse latest Browse all 201867

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>