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

时间:2017-03-13 11:09:31

标签: r ggplot2

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

我的数据框如下所示:

public func ==<Element : Equatable>(lhs: [[Element]], rhs: [[Element]]) -> Bool {
   ...
}

当我尝试绘制特定日期的概述时,让我们说2017年2月23日,我使用以下代码:

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" 

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

enter image description here

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

非常感谢提前!

EDIT / UPDATE: 添加以下内容会导致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")) +
      theme_linedraw()

唯一的事情是我在执行后得到以下消息:'scale for'x'已经存在。为'x'添加另一个比例,它将取代现有比例。'

这意味着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() 被覆盖: scale_x_datetime(labels=date_format("%H:%m"), breaks = date_breaks("2 hours")

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

4 个答案:

答案 0 :(得分:2)

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

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)

接下来,以下是如何使用此数据重现问题:

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

最后,这是一个使用limits_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()

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

lims <- as.POSIXct(strptime(c("2017-02-23 00:00", "2017-02-24 00:00"), 
                   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()

答案 1 :(得分:1)

expand中使用scale_x_datetime参数并将其设置为0

scale_x_datetime(labels=date_format("%H:%m"), breaks = date_breaks("2 hours"), expand=c(0,0))

答案 2 :(得分:0)

除了我的评论:

library(ggplot2)
library(lubridate)
df <- data.frame(x = seq(Sys.time(), Sys.time()+days(1), by = "2 mins"))
df$y = runif(nrow(df))

lims <- c(floor_date(min(df$x), "day"), ceiling_date(max(df$x), "day"))
f <- function(lims)
  ggplot(data = df, aes(x, y)) + 
    geom_line() +  
    scale_x_datetime(
      date_labels = "%H:%M", 
      date_breaks = "2 hours", 
      limits = lims,
      timezone = Sys.timezone(),
      expand = c(0,0)
    ) + 
    theme(
      plot.margin = margin(10,20,10,10), 
      axis.text.x = element_text(angle=90, vjust=.5)
    )

f(lims)
f(lims-c(days(1),0))

答案 3 :(得分:0)

我要添加另一个答案以阐明使用<div class="container position-container"> <div class="box1 green"></div> <div class="box2 blue"></div> </div> <hr /> <div class="container flex-container"> <div class="box3 green"></div> <div class="box4 blue"></div> </div>scale_x_datetime之间的区别。与coordinate_cartesian不同,coordinate_cartesian将缩放图,而不会删除任何超出图限制的数据。

例如。如果我们使用上面的图:

scale_x_datetime

原始图(用一条平滑线可以更容易地看到library(ggplot2) library(scales) set.seed(1) ParkingSub4 <- data.frame(DateTime = seq(as.POSIXlt('2017-02-22 23:00', tz = 'UTM'), as.POSIXlt('2017-02-24 01:00', tz = 'UTM'), len = 42), OccupancyRateShort = runif(42, 0, 1)) ParkingSub4$Weekday <- weekdays(ParkingSub4$DateTime) lims <- as.POSIXct(strptime(c("2017-02-23 00:00", "2017-02-23 23:59"), format = "%Y-%m-%d %H:%M"), tz = 'UTM') 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)) + geom_smooth()+ theme_linedraw() scale_x_datetime之间的区别): enter image description here

如果出于某种原因,您想将注意力集中在上午7点至晚上7点,则有两种选择。您可以使用coordinate_cartesian(或scale_x_datetime)删除多余的数据:

xlim

哪个产生以下情节: enter image description here

或者,您可以使用lims2 <- as.POSIXct(strptime(c("2017-02-23 07:00", "2017-02-23 19:00"), format = "%Y-%m-%d %H:%M"), tz = 'UTM') 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 = lims2, expand = c(0, 0)) + geom_smooth()+ theme_linedraw() 放大图,而无需删除任何数据:

coordinate_cartesian

请注意,平滑度与原始图中的相同: enter image description here