-1

I'm currently graphing the cumulative miles I've run/biked/swam etc. by day. I've recorded this data via various GPS devices. I currently have a working graph that displays the days of the week out of order. How can I order the days of the week in chronological order? My code and dataframe are below. Thanks for the help.

Data Frame (3 variables)

CurrentOutput

dayofweek <- mydata %>% select(type, distance2, start_day,) %>% 
  group_by(start_day) %>% 
  summarize(distance2 = sum(distance2), na.rn=TRUE) %>% 
  ggplot(aes(x = start_day, y = distance2))+
  geom_bar(stat="identity")+
  theme_bw(10) 
dayofweek <- dayofweek + labs(title = "Workout Mileage",
                    subtitle = "Broken out by Day of the Week",
                    caption = "Data source: Strava",
                    x = "Day of the Week",
                    y= "# of Miles")
dayofweek

Edit (solved thanks to sconfluentus):

I added this at the start of my code:

mydata$start_day <- factor(mydata$start_day,
  levels = c("Sun", "Mon", 
  "Tue", "Wed", "Thu", "Fri", "Sat"))

which yields this:

Rectified Output

ADT
  • 17
  • 4
  • 3
    [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that folks can help with. We can't run your code without your data (like a workable sample of it, not a picture), and can't see any of the output that you want to change. It's likely this is a question that's been asked and answered before on SO, but hard to point you toward one without more information – camille Apr 10 '20 at 01:54
  • If you re-level the categories of start_day to reflect the order of days you wish to see then you can use that category to order the bars. See here: https://sebastiansauer.github.io/ordering-bars/ – sconfluentus Apr 10 '20 at 02:13
  • @sconfluentus thanks, that worked perfectly. I also like Daniel Jachetta's solution as it embeds the factor leveling in aes – ADT Apr 10 '20 at 02:57

1 Answers1

1
dayofweek <- mydata %>% select(type, distance2, start_day,) %>% 
group_by(start_day) %>% 
summarize(distance2 = sum(distance2), na.rn=TRUE) %>% 
ggplot(aes(x = factor(start_day, weekdays(min(my_data$date) + 0:6)),
  y = distance2))+ geom_bar(stat="identity")+
     theme_bw(10) 
      dayofweek <- dayofweek + labs(title = "Workout Mileage",
                   subtitle = "Broken out by Day of the Week",
                  caption = "Data source: Strava",
                x = "Day of the Week",
          y= "# of Miles")
     dayofweek

check out this link

Daniel_j_iii
  • 3,041
  • 2
  • 11
  • 27