ggplot 中的 xlim 与 POSIXct 日期

tnt*_*tnt 4 r ggplot2

我试图限制具有时态数据(POSIXct 格式)的图表的 x 轴范围。

str(df.alltags_barn.path$ts.h)
 POSIXct[1:61558], format: "2018-07-04 22:48:08" "2018-07-04 22:48:46" "2018-07-04 23:05:17" ...
Run Code Online (Sandbox Code Playgroud)

我尝试了以下两种具有不同错误消息的方法

1

p <- ggplot(data = filter(df.alltags_barn.path, mfgID %in% c(52)), 
        aes(ts.h, recvLon))
p + geom_point() + geom_path() + theme_bw() + 
  facet_wrap(~mfgID, scales = "free", ncol = 4) + 
  xlim(as.Date(c("2018-08-13", "2018-08-20")), format="%d/%m/%Y") +
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1))
Run Code Online (Sandbox Code Playgroud)

limit.Date(c(...), "x") 中的错误:length(lims) == 2 不为 TRUE

2

p <- ggplot(data = filter(df.alltags_barn.path, mfgID %in% c(52)), 
        aes(ts.h, recvLon))
p + geom_point() + geom_path() + theme_bw() + 
  facet_wrap(~mfgID, scales = "free", ncol = 4) + 
scale_x_date(limits=as.Date(c("2018-08-13", "2018-08-20")), labels=date_format("%b-%Y")) +
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1))
Run Code Online (Sandbox Code Playgroud)

错误:输入无效:date_trans 仅适用于 Date 类的对象

帮助使这些选项中的一个或两个起作用将不胜感激。

Jon*_*ing 6

我可以让它使用一些虚假数据并替换为scale_x_datetime.

编辑:从日期更改为 POSIXct 日期时间。

library(lubridate)
sample_data <- data.frame(dates = seq.POSIXt(from = ymd_h("2018-01-01 00"),
                                           to   = ymd_h("2019-01-31 23"),
                                           by   = dhours(10)),
                          data = rnorm(951),
                          mfgID = sample(LETTERS[1:2], 951, replace = T))


p <- ggplot(data = sample_data,
            aes(dates, data)) + 
  geom_point() + geom_path() + theme_bw() + 
  facet_wrap(~mfgID, scales = "free", ncol = 4) + 
  scale_x_datetime(limits = ymd_h(c("2018-08-13 00", "2018-08-20 23"))) +
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1))
p
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述