首先,让我们创建一些示例数据。时间使用lubridate
的hm
进行存储,因为这似乎是最适合的。
library(tibble)
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#>
#> date
(
data <- tibble(
Time = hm('09:00', '10:30'),
Value = 1
)
)
#> # A tibble: 2 x 2
#> Time Value
#> <S4: Period> <dbl>
#> 1 9H 0M 0S 1
#> 2 10H 30M 0S 1
这就是我希望情节呈现的方式。目前,我已手动将休息时间设定为每半小时一次。
library(ggplot2)
library(scales)
ggplot(data, aes(Time, Value)) +
geom_point() +
scale_x_time(breaks = hm('09:00', '09:30', '10:00', '10:30'))
我想在半小时间隔自动创建这些休息时间。 尝试使用scales::date_breaks
会出现错误。
ggplot(data, aes(Time, Value)) +
geom_point() +
scale_x_time(breaks = date_breaks('30 mins'))
#> Error in UseMethod("fullseq"): no applicable method for 'fullseq' applied to an object of class "c('hms', 'difftime')"
尝试使用seq
创建断点也会出错。
seq(hm('09:00'), hm('10:30'), hm('00:30'))
#> Note: method with signature 'Period#ANY' chosen for function '-',
#> target signature 'Period#Period'.
#> "ANY#Period" would also be valid
#> estimate only: convert to intervals for accuracy
#> Error in if (sum(values - trunc(values))) {: argument is not interpretable as logical
scale_x_time
而不是你在最后一段建议的scale_x_date
。 - Greg