jfeuerman
jfeuerman

Reputation: 177

ggplot2 Reorder Data that is in minutes:seconds Format

The first five entries (out of twenty) of my dataset:

>head(data)

 Name         SDC 
 <chr>        <Period>
1 Feuerman    1M 37S
2 Solis       1M 52S
3 Osborne     1M 47S
4 Frizzell    1M 58S
5 Moran       1M 59S

Also:

> dput(head(data))
structure(list(Name = c("Feuerman", "Solis", "Osborne", "Frizzell", 
"Moran", "Seth"), Deadlift = c(320, 250, 340, 250, 250, 200), 
Medicine_Ball = c(11.6, 8.8, 12.5, 9.2, 9.7, 9.1), HRP = c(46, 
39, 36, 33, 42, 31), SDC = new("Period", .Data = c(37, 52, 
47, 58, 59, 15), year = c(0, 0, 0, 0, 0, 0), month = c(0, 
0, 0, 0, 0, 0), day = c(0, 0, 0, 0, 0, 0), hour = c(0, 0, 
0, 0, 0, 0), minute = c(1, 1, 1, 1, 1, 2)), Leg_Tuck = c(20, 
13, 4, 10, 13, 13), Run = new("Period", .Data = c(48, 59, 
10, 53, 0, 29), year = c(0, 0, 0, 0, 0, 0), month = c(0, 
0, 0, 0, 0, 0), day = c(0, 0, 0, 0, 0, 0), hour = c(0, 0, 
0, 0, 0, 0), minute = c(13, 12, 17, 16, 0, 16)), Total = c(570, 
508, 513, 470, 410, 452), Pass_Fail = structure(c(1L, 1L, 
2L, 1L, 2L, 1L), .Label = c("Pass", "Fail"), class = "factor"), 
Date = structure(c(18522, 18522, 18522, 18522, 18522, 18522
), class = "Date")), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

As you can see, SDC is in minutes:seconds format. I achieved this using ms(data$SDC) to change the column type. I am trying to create a plot using geom_col that orders SDC from lowest to highest times. I am facing two problems:

  1. When using the reorder command, the times are not properly reordered (see plot below).
  2. The axes are being formatted by hour:minute:second but I want it to be formatted in only minute:second format (also see plot below).

Here is my code to generate the plot:

ggplot(data=data,
   aes(x=reorder(Name, -SDC), y=SDC, fill=Pass_Fail)) +
  scale_y_time(limits=c(0,200)) +
  scale_fill_manual(values=c('#00BFC4', '#F8766D')) +
  labs(x='Soldier', y='Sprint Drag Carry Time', fill='Passed/Failed ACFT', title='Sprint Drag Carry Scores') +
  geom_col() +
  geom_text(size=3, aes(label = SDC), hjust=-0.04) +
  coord_flip() +
  theme_classic()

It produces the following plot:

enter image description here

As you can see, the reordering is incorrect and the axes are not formatted the way I want them to be. Thanks in advance for your help.

Upvotes: 1

Views: 129

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389175

  1. I think reorder have trouble working with Period object. We can arrange the factor levels according to the value of SDC to get bars in increasing orders.

  2. We can pass custom function for y-axis to get only minutes and seconds in labels.

library(tidyverse)

data %>%
  arrange(SDC) %>%
  mutate(Name = factor(Name, levels = unique(Name))) %>%
  ggplot() + aes(x=Name, y=SDC, fill=Pass_Fail) +
  scale_y_time(limits=c(0,200), 
               labels = function(x) sprintf('%02s:%02s', minute(x),second(x))) +
  scale_fill_manual(values=c('#00BFC4', '#F8766D')) +
  labs(x='Soldier', y='Sprint Drag Carry Time', 
      fill='Passed/Failed ACFT', title='Sprint Drag Carry Scores') +
  geom_col() +
  geom_text(size=3, aes(label = SDC), hjust=-0.04) +
  coord_flip() +
  theme_classic()

enter image description here

Upvotes: 1

Related Questions