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

问题描述 投票:0回答:1

[我试图每天绘制这些点,我希望每个构面轴从“ 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)

enter image description here

我已经尝试使用scale_x_time中的限制来指定特定时间,但是它不起作用。我希望[t0]的facet_wrap用作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"))
  )

但是它似乎不起作用。

zero_range(range)中的错误:x必须为长度1或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),类= 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”),测量值= c(“输入”,“输出”,“输出”,“输入”,“输入”,“输出”,“输出”,“输入”,“输入”,“输出”,“输出”,“输入”,“输入”,“输出”,“输出”,“输入”,“输入”,“输出”,“输出”,“输入”,“输入”,“输出”,“输出”,“输入”,“输入”,“ out”,“ out”,“ in”,“ in”,“ out”,“ out”,“ in”,“ in”,“ out”,“输出”,“输入”,“输入”,“输出”,“输出”,“输入”,“输入”,“输出”,“输出”,“ in”,“ in”,“ out”,“ out”,“ in”,“ in”,“ out”),dateonly =结构(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 ,8公升,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),等级=“ data.frame”)

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

r datetime ggplot2 posixct
1个回答
0
投票

一种方法是创建一个辅助日期时间列,以绘制所有时间在同一天的位置。这样做的好处是可以利用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)
© www.soinside.com 2019 - 2024. All rights reserved.