在 R 和 Tidy 中绘制时间序列数据
Plotting Time Series Data in R and Tidy
我正在尝试使用 lubriadte
从我的温度传感器中整理时间序列数据。我最终想要一个在 x 轴上有时间和在 y 轴上有温度的图。我一直在使用函数 parse_date_time
来尝试创建一个新的 date
变量,但我得到的只是 NA
。
temps<-temps %>% as_tibble() %>%
mutate(date = parse_date_time(Date.Time..GMT..0500, "mdYHM"))
temps
问题是您在年份部分仅包含两位数时插入了大写 Y
。所以你应该使用小写 y
,即
temps %>% as_tibble() %>%
mutate(date = parse_date_time(Date.Time..GMT..0500, "mdyHM"))
为了制作简单的情节,这里有一个基本代码
ggplot(temps) +
aes(x = date, y = TempF) +
geom_line()
关于情节本身的更多细节,我建议你看看ggplot2
documentation。
在我的示例数据中它起作用了
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"
我正在尝试使用 lubriadte
从我的温度传感器中整理时间序列数据。我最终想要一个在 x 轴上有时间和在 y 轴上有温度的图。我一直在使用函数 parse_date_time
来尝试创建一个新的 date
变量,但我得到的只是 NA
。
temps<-temps %>% as_tibble() %>%
mutate(date = parse_date_time(Date.Time..GMT..0500, "mdYHM"))
temps
问题是您在年份部分仅包含两位数时插入了大写 Y
。所以你应该使用小写 y
,即
temps %>% as_tibble() %>%
mutate(date = parse_date_time(Date.Time..GMT..0500, "mdyHM"))
为了制作简单的情节,这里有一个基本代码
ggplot(temps) +
aes(x = date, y = TempF) +
geom_line()
关于情节本身的更多细节,我建议你看看ggplot2
documentation。
在我的示例数据中它起作用了
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"