简体   繁体   English

如何在 r 中的 plot 每周时间序列数据在 r 或 ggplot 中的 x 轴上显示日期?

[英]how to plot weekly time series data in r show the date in the x axis in r or ggplot?

I have time series data set我有时间序列数据集

y<-c(1,5,12,21,30,50,90,100)

Date = c("2020/07/16","2020/07/23","2020/07/30","2020/08/06","2020/08/13","2020/08/20","2020/08/27","2020/09/13")

if I have an other time series如果我有其他时间序列

 q<-c(1,13,18,18,20,30,40,50)

how can I plot it in the same plot?我怎么能把它放在同一个 plot 中?

how to plot this data set show the date on the x-axis? plot 这个数据集如何在 x 轴上显示日期? Thanks谢谢

Convert Date to Date class and then add these 2 vectors in a dataframe to use in ggplot .Date转换为 Date class 然后将这两个向量添加到 dataframe 中以在ggplot中使用。

library(ggplot2)
df <- data.frame(y, Date = as.Date(Date))
ggplot(df) + aes(Date, y) + geom_line()

For multiple values you could get the data in long format and then plot.对于多个值,您可以获取长格式数据,然后获取 plot。

df <- data.frame(y, Date = as.Date(Date), q)
df %>%
  tidyr::pivot_longer(cols = -Date) %>%
  ggplot() + aes(Date, value, color = name) + geom_line()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM