我有一个如下所示的日期框,其中列时间以毫秒为单位
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
我们可以将时间值转换为 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 创建。