如何在 ggplot2 中使用 facet_wrap(~day) 设置轴每日时间限制

How to set axis daily time limits with facet_wrap(~day) in ggplot2

我试图为每一天绘制这些点,我希望每个小平面轴从“00:00”延伸到“23:59”。

现在看起来像这样,每个面 x 轴从第一次出现到最后一次出现,但我希望每个面 x 轴从“00:00”开始到“23:59”结束" 当天:

ggplot(minidate, aes(x=time,y=measurement, group=EPC))+
  geom_point()+ geom_line()+
  facet_wrap(~dateonly,ncol=1)

我曾尝试使用 scale_x_time 中的限制来指定特定时间,但它不起作用。我希望 facet_wrap 像 tidyr 的 group_by 一样工作,所以我尝试了这个

+ scale_x_datetime(limits=
                     c(as.POSIXct(paste0(unique(minidate$dateonly),"00:00"),format="%Y-%m-%d %H:%M"),
                       as.POSIXct(paste0(unique(minidate$dateonly),"23:59"),format="%Y-%m-%d %H:%M"))
  )

但是好像不行。

Error in zero_range(range) : x must be length 1 or 2

这是数据集:

dput()

structure(list(time = structure(c(1523883481, 1523883481, 1523883957, 1523883957, 1524059523, 1524059523, 1524059913, 1524059913, 1524138446, 1524138446, 1524138488, 1524138488, 1524139045, 1524139045, 1524139241, 1524139241, 1524139855, 1524139855, 1524139978, 1524139978, 1524215195, 1524215195, 1524215406, 1524215406, 1524230989, 1524230989, 1524231221, 1524231221, 1524309944, 1524309944, 1524310193, 1524310193, 1524323456, 1524323456, 1524324053, 1524324053, 1524385173, 1524385173, 1524385310, 1524385310, 1524403469, 1524403469, 1524403674, 1524403674, 1524403762, 1524403762, 1524403834, 1524403834, 1524473794, 1524473794), class = c("POSIXct", "POSIXt"), tzone = "UTC"), EPC = c("ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081"), measurement = c("in", "out", "out", "in", "in", "out", "out", "in", "in", "out", "out", "in", "in", "out", "out", "in", "in", "out", "out", "in", "in", "out", "out", "in", "in", "out", "out", "in", "in", "out", "out", "in", "in", "out", "out", "in", "in", "out", "out", "in", "in", "out", "out", "in", "in", "out", "out", "in", "in", "out"), dateonly = structure(c(17637, 17637, 17637, 17637, 17639, 17639, 17639, 17639, 17640, 17640, 17640, 17640, 17640, 17640, 17640, 17640, 17640, 17640, 17640, 17640, 17641, 17641, 17641, 17641, 17641, 17641, 17641, 17641, 17642, 17642, 17642, 17642, 17642, 17642, 17642, 17642, 17643, 17643, 17643, 17643, 17643, 17643, 17643, 17643, 17643, 17643, 17643, 17643, 17644, 17644), class = "Date")), row.names = c(1L, 146L, 2L, 147L, 3L, 148L, 4L, 149L, 5L, 150L, 6L, 151L, 7L, 152L, 8L, 153L, 9L, 154L, 10L, 155L, 11L, 156L, 12L, 157L, 13L, 158L, 14L, 159L, 15L, 160L, 16L, 161L, 17L, 162L, 18L, 163L, 19L, 164L, 20L, 165L, 21L, 166L, 22L, 167L, 23L, 168L, 24L, 169L, 25L, 170L ), class = "data.frame")

如何在使用 facet_wrap(~day) 时设置每日时间限制?

一种方法是制作一个辅助日期时间列,用于绘制所有时间都在同一天的位置。这样做的好处是它利用了 ggplot 的中断和日期时间标签:

library(dplyr); library(lubridate); library(stringr)
minidate %>%
  mutate(time_only = ymd_hms(paste("1900-01-01", 
                             str_sub(time, 12)))) %>%
ggplot(aes(x=time_only,y=measurement, group=EPC))+
  geom_point()+ geom_line()+
  expand_limits(x = c(ymd_h(1900010100), ymd_h(1900010200))) +
  scale_x_datetime(date_labels = "%H:%M") +
  facet_wrap(~dateonly,ncol=1)

或者您可以将日期时间转换为表示一天中小时的十进制值:

library(dplyr); library(lubridate)
minidate %>%
  mutate(time_dec = hour(time) + minute(time)/60 + second(time)/3600) %>%
ggplot(aes(x=time_dec,y=measurement, group=EPC))+
  geom_point()+ geom_line()+
  expand_limits(x = c(0, 24)) +
  facet_wrap(~dateonly,ncol=1)