【问题标题】:Plotting Time Series Data in R and Tidy在 R 和 Tidy 中绘制时间序列数据
【发布时间】:2020-08-06 21:13:35
【问题描述】:

我正在尝试使用lubriadte 从我的温度传感器中整理出时间序列数据。我最终想要一个在 x 轴上有时间,在 y 轴上有温度的图。我一直在使用函数parse_date_time 来尝试创建一个新的date 变量,但我得到的只是NA

temps<-temps %>% as_tibble() %>% 
  mutate(date = parse_date_time(Date.Time..GMT..0500, "mdYHM"))
temps

【问题讨论】:

标签: r ggplot2 time-series tidyverse lubridate


【解决方案1】:

问题是当年份部分仅包含两位数字时,您插入了大写 Y。所以你应该使用小写的y,即

temps %>% as_tibble() %>% 
  mutate(date = parse_date_time(Date.Time..GMT..0500, "mdyHM"))

为了产生一个简单的情节,这里是一个基本的代码

ggplot(temps) +
  aes(x = date, y = TempF) +
  geom_line()

关于剧情本身的更多细节,我建议你看看ggplot2documentation


在我的示例数据中它有效

temps <- data.frame(
  Date.Time..GMT..0500 = c("6/18/18 12:57", "6/18/18 13:57", "6/18/18 14:57"),
  var = c(1,2,3)
)
parse_date_time(temps$Date.Time..GMT..0500, "mdYHM")
# [1] "2018-06-18 12:57:00 UTC" "2018-06-18 13:57:00 UTC" "2018-06-18 14:57:00 UTC"

【讨论】:

  • 这很好,但现在你如何使用输出日期在 ggplot 中绘制它?
  • @IDelToro 我在答案中添加了一个小示例代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-09
  • 1970-01-01
  • 2020-11-25
  • 2018-01-23
  • 2017-04-22
  • 1970-01-01
  • 2021-12-07
相关资源
最近更新 更多