使用scale_x_datetime在小时内没有前导零

时间:2017-04-20 20:02:35

标签: r datetime ggplot2

是否可以从x轴上显示的小时中去除前导零,同时使用ggplot2的scale_x_datetime?

1 个答案:

答案 0 :(得分:2)

我们可以使用date_format包中的scales

library(ggplot2)
library(scales)

# dummy data
set.seed(1)
df1 <- data.frame(
  x = as.POSIXct(runif(10, 0, 24 * 60 * 60), origin = "2017/04/20"),
  y = 1:10
)

# use scales::date_format
ggplot(df, aes(x = x, y = y)) + 
  geom_point() + 
  scale_x_datetime(labels = date_format("%l:%M"))

?strptime

  

%k 24小时制时钟,单个数字前面有空格   %l 12小时制的时钟,单个数字前面有空格。

enter image description here

相关问题