繁体   English   中英

如何在 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可以作为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()

结构(列表(时间=结构(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("ccd",810d10cc81",810ccd10"ccd10" , "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd",8ccd1010 ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081" ", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd",8ccd1010 "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd10081", "ccd,80081", "ccd,80081", "ccd10081", 1008ccd = 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", "入”、“出”、“出”、“入”、“入”、“出”、“出”、“入”、“入”、“出”、“出”、“入”、“入” , "out", "out", "in", "in", "out", "out", "in", "in", "out", "out", "in", "in", "出“),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, 53L, 1L , 154L, 10L, 155L, 11L, 156L, 12L, 157L, 13L, 158L, 14L, 159L, 15L, 160L, 16L, 161L, 17L, 162L, 16L, 10L, 6L, 15L, 15L, 16L, 10L , 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)

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM