使用scale_y_time将MS转换为箱线图(GGPLOT)中的分钟和秒

Iva*_*her 4 r ggplot2

我有一个如下所示的日期框,其中列时间以毫秒为单位

id | group | time
1    |   A   | 1400003
2    |   B   | 1604203
3    |   A   | 170203
Run Code Online (Sandbox Code Playgroud)

我正在使用 GGplot 创建箱线图

ggplot(df, aes(x=group, y=time, fill=group)) + 
  geom_boxplot()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如何使用Y 轴刻度线scale_y_datetime()或将其转换为以下格式?scale_y_time()M:S

GGa*_*mba 5

我们可以将时间值转换为 difftime 对象(以秒为单位),然后格式化 y 轴标签:

df <- read.table(text = 'id | group | time
1    |   A   | 1400003
2    |   B   | 1604203
3    |   A   | 170203', sep = '|', header = TRUE)

df$time <- as.difftime(df$time/1000, units = 'secs')

library(ggplot2)
ggplot(df, aes(x=group, y=time, fill=group)) + 
  geom_boxplot() +
  scale_y_time(labels = function(l) strftime(l, '%M:%S'))
Run Code Online (Sandbox Code Playgroud)

reprex 包(v0.2.0) 于 2018-05-04 创建。