Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 带有POSIXct日期的ggplot中的xlim_R_Ggplot2 - Fatal编程技术网

R 带有POSIXct日期的ggplot中的xlim

R 带有POSIXct日期的ggplot中的xlim,r,ggplot2,R,Ggplot2,我试图限制一个具有时间数据(POSIXct格式)的图形的x轴范围 我用不同的错误消息尝试了以下两种方法 1. p我可以让它处理一些假数据,并用scale\u x\u datetime替换 编辑:从日期更改为POSIXct日期时间。 库(lubridate) 示例\u data我可以让它与一些假数据一起工作,并用scale\u x_udatetime 编辑:从日期更改为POSIXct日期时间。 库(lubridate) 样本数据感谢Jon Spring。不幸的是,当我在young解决方案中放置变量

我试图限制一个具有时间数据(POSIXct格式)的图形的x轴范围

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

1.
p我可以让它处理一些假数据,并用
scale\u x\u datetime
替换

编辑:从日期更改为POSIXct日期时间。
库(lubridate)

示例\u data我可以让它与一些假数据一起工作,并用
scale\u x_udatetime

编辑:从日期更改为POSIXct日期时间。
库(lubridate)

样本数据感谢Jon Spring。不幸的是,当我在young解决方案中放置变量时,仍然会出现相同的错误:“错误:无效输入:date_trans仅适用于类date的对象”。我还尝试将上述日期替换为“2018-08-13 00:00:00 UTC”、“2018-08-20 23:59:59 UTC”,但仍然没有成功。如果使用POSIXct日期时间x值,最好使用
scale\u x\u datetime
并指定日期时间范围。更新的例子。谢谢!使用scale_x_datetime解决了这个问题。谢谢Jon Spring。不幸的是,当我在young解决方案中放置变量时,仍然会出现相同的错误:“错误:无效输入:date_trans仅适用于类date的对象”。我还尝试将上述日期替换为“2018-08-13 00:00:00 UTC”、“2018-08-20 23:59:59 UTC”,但仍然没有成功。如果使用POSIXct日期时间x值,最好使用
scale\u x\u datetime
并指定日期时间范围。更新的例子。谢谢!使用scale_x_datetime解决了这个问题。
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" ...
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))
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))
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