1

我的数据集的前五个条目(二十个):

>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

还:

> 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"))

如您所见,SDCminutes:seconds格式。我通过ms(data$SDC)更改列类型实现了这一点。我正在尝试使用从最低到最高时间geom_col的订单创建一个情节。SDC我面临两个问题:

  1. 使用该reorder命令时,时间未正确重新排序(见下图)。
  2. 轴正在格式化,hour:minute:second但我希望它仅以minute:second格式格式化(另见下图)。

这是我生成情节的代码:

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()

它产生以下情节:

在此处输入图像描述

如您所见,重新排序不正确,并且轴的格式不符合我的要求。在此先感谢您的帮助。

4

1 回答 1

1
  1. 我认为在使用对象reorder时遇到问题。Period我们可以根据 的值排列因子水平,SDC以得到升序的柱状图。

  2. 我们可以通过 y 轴的自定义函数来获取标签中的分钟和秒。

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()

在此处输入图像描述

于 2020-09-28T05:49:08.503 回答