![](/img/trans.png)
[英]How to customize and add discrete time steps to x-axis in time series plot in ggplot2 in R?
[英]how to plot weekly time series data in r show the date in the x axis in r or ggplot?
我有時間序列數據集
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")
如果我有其他時間序列
q<-c(1,13,18,18,20,30,40,50)
我怎么能把它放在同一個 plot 中?
plot 這個數據集如何在 x 軸上顯示日期? 謝謝
將Date
轉換為 Date class 然后將這兩個向量添加到 dataframe 中以在ggplot
中使用。
library(ggplot2)
df <- data.frame(y, Date = as.Date(Date))
ggplot(df) + aes(Date, y) + geom_line()
對於多個值,您可以獲取長格式數據,然后獲取 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.