在 ggplot 中为日期时间设置 x 轴限制

Rob*_*ert 10 r ggplot2

我想使用 ggplot 在折线图中绘制特定日期特定停车场的占用率。

我的数据框如下所示:

head(ParkingSub4)
    FreeSpaceShort ShortCapacity            DateTime OccupancyRateShort       Date  Weekday WeekNumber  Week
310801            257           373 2017-02-25 00:04:41          0.3109920 2017-02-25 Saturday         08 FALSE
310843            260           373 2017-02-25 00:09:41          0.3029491 2017-02-25 Saturday         08 FALSE
310885            261           373 2017-02-25 00:14:41          0.3002681 2017-02-25 Saturday         08 FALSE
310927            260           373 2017-02-25 00:19:41          0.3029491 2017-02-25 Saturday         08 FALSE
310969            260           373 2017-02-25 00:24:41          0.3029491 2017-02-25 Saturday         08 FALSE
311011            263           373 2017-02-25 00:29:41          0.2949062 2017-02-25 Saturday         08 FALSE

class(ParkingSub4$DateTime)
[1] "POSIXlt" "POSIXt" 
Run Code Online (Sandbox Code Playgroud)

当我尝试绘制特定日期的概览时,假设 2017 年 2 月 23 日,我使用以下代码:

ggplot(data = ParkingSub4, 
       aes(x=DateTime, y=OccupancyRateShort)) + geom_line(size = 1.25) + facet_wrap(~Weekday) +
      scale_x_datetime(labels=date_format("%H:%m"), breaks = date_breaks("2 hours")) +
      theme_linedraw()
Run Code Online (Sandbox Code Playgroud)

然后我得到的是以下情节:

在此处输入图片说明

如您所见,情节在 23:02 开始和结束。我希望情节在特定日期的 00:00:00 开始并在 23:59:59 结束。我怎样才能做到这一点?

提前谢谢了!

编辑/更新:添加以下内容确实会导致 x 轴以 00:00 开始和结束:

ggplot(data = ParkingSub4, 
   aes(x=DateTime, y=OccupancyRateShort)) + geom_line(size = 1.25) + facet_wrap(~Weekday) +
  scale_x_datetime(labels=date_format("%H:%m"), breaks = date_breaks("2 hours"), expand=c(0,0)) +
  xlim(c(as.POSIXct('2017-02-23 00:00:00', format = "%Y-%m-%d %H:%M:%S"),
     as.POSIXct('2017-02-24 00:00:00', format = "%Y-%m-%d %H:%M:%S"))) +
  theme_linedraw()
Run Code Online (Sandbox Code Playgroud)

唯一的问题是我在执行后收到以下消息:'x' 的比例已经存在。为“x”添加另一个比例,这将替换现有比例。

这意味着scale_x_datetime(labels=date_format("%H:%m"), breaks = date_breaks("2 hours")被覆盖: xlim(c(as.POSIXct('2017-02-23 00:00:00', format = "%Y-%m-%d %H:%M:%S"), as.POSIXct('2017-02-24 00:00:00', format = "%Y-%m-%d %H:%M:%S"))

这也不是我想要的,因为现在休息时间设置为 6 小时而不是 2 小时,而且我也无法指定标签上设置了哪些信息 ("%H:%m")。

mak*_*ker 5

首先,这里有一些可重现的数据:

set.seed(1)
ParkingSub4 <- data.frame(DateTime = seq(as.POSIXlt('2017-02-22 23:00'), 
                                         as.POSIXlt('2017-02-24 01:00'), 
                                         len = 42), 
                          OccupancyRateShort = runif(42, 0, 1))
ParkingSub4$Weekday <- weekdays(ParkingSub4$DateTime)
Run Code Online (Sandbox Code Playgroud)

接下来,这里是如何用这些数据重现问题:

library(ggplot2)
library(scales)
ggplot(data = ParkingSub4[ParkingSub4$Weekday == "Thursday",], 
       aes(x = DateTime, y = OccupancyRateShort)) + 
       geom_line(size = 1.25) + 
       facet_wrap(~Weekday) +
       scale_x_datetime(labels = date_format("%H:%m"), 
                        breaks = date_breaks("2 hours")) +
       theme_linedraw()
Run Code Online (Sandbox Code Playgroud)

最后,这是使用 scale_x_datetime 的限制选项的解决方案:

lims <- as.POSIXct(strptime(c("2017-02-23 00:00", "2017-02-24 00:00"), 
                   format = "%Y-%m-%d %H:%M"))
ggplot(data = ParkingSub4[ParkingSub4$Weekday == "Thursday",], 
       aes(x = DateTime, y = OccupancyRateShort)) + 
       geom_line(size = 1.25) + 
       facet_wrap(~Weekday) +
       scale_x_datetime(labels = date_format("%H:%m"), 
                        breaks = date_breaks("2 hours"), 
                        limits = lims) +
       theme_linedraw()
Run Code Online (Sandbox Code Playgroud)

更新:以下内容将删除图表左侧和右侧的空白,休息时间将在小时而不是过去 2 分钟:

lims <- as.POSIXct(strptime(c("2017-02-23 00:00", "2017-02-23 23:59"), 
                   format = "%Y-%m-%d %H:%M"))
ggplot(data = ParkingSub4, 
       aes(x = DateTime, y = OccupancyRateShort)) + 
       geom_line(size = 1.25) +            
       scale_x_datetime(labels = date_format("%H:%M"), 
                        breaks = date_breaks("2 hours"), 
                        limits = lims, 
                        expand = c(0, 0)) +
       theme_linedraw()
Run Code Online (Sandbox Code Playgroud)

  • 谢谢!我在我应该使用 %M 的地方使用了 %m 来显示分钟。仍然指定了限制,它给了我一个从 23:00 到 23:00 的范围。显然这是一个时区问题。我现在使用了`scale_x_datetime(labels=date_format("%H:%M", tz="CET")`,它给了我我想要的东西:) (3认同)