简体   繁体   中英

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

I am trying to plot, for each day, these dots and I would like each facet axis to extend from "00:00" to "23:59".

Right now it looks like this, with each facet x axis extending from the first occurrence to the last one, but I would like each facet x axis to start at "00:00" and end at "23:59" on the facet's day:

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

在此处输入图片说明

I have tried specifying specific times using limits in scale_x_time but it does not work. I was hoping facet_wrap works as group_by from tidyr so I tried this

+ 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"))
  )

but it doesn't seem to work.

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

Here is the dataset:

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

How can I set daily time limits while using a facet_wrap(~day) ?

One approach would be to make a helper datetime column for plotting where all the times are in the same day. The upside of this is that it takes advantage of ggplot's breaks and labeling for datetimes:

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)

在此处输入图片说明

Or you might convert the datetimes to a decimal value representing hour of the day:

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)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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