【问题标题】:Adding dates to x-axis in time series plot [closed]在时间序列图中将日期添加到 x 轴 [关闭]
【发布时间】:2020-09-29 10:58:38
【问题描述】:

我有以下代码:

library(ggplot2)
library(fpp2)
library(tidyverse)
library(tidyr)
library(lubridate)
library(writexl)
library(plyr)
library(forecast)

    Sales171819 <- SalesNL[SalesNL$TransactionDate >= "2017-01-01" & SalesNL$TransactionDate <= "2019-12-31",]
    #create time series
    myts <- ts(Sales171819[,2],start = decimal_date(as.Date("2017-05-01")), frequency = 365)
    #plot time series
    view(myts)
    autoplot(myts) + ggtitle("TAF Sales NL 2017/2018")+
     ylab("SalesQty") + xlab("days")

    # seasonal plot sales
    ggseasonplot(myts) + ggtitle("Sales Per Dag")+
    ylab("Sales") + xlab("Days")

我想将实际日期绘制到 x 轴上的 autoplot 和 ggseasonplot 上,而不是第 1、2、3 天……等等。我还想用实际日期突出显示图中的点。如何编辑我的代码以便完成这项工作?

数据如下:

  TransactionDate NetSalesQty 
1      2017-05-01        1221   
2      2017-05-02        1275   
3      2017-05-03        1198   
4      2017-05-04        1792   
5      2017-05-05        1842   
6      2017-05-06        1183   

structure(list(TransactionDate = structure(c(17287, 17288, 17289, 
17290, 17291, 17292), class = "Date"), NetSalesQty = c(1221, 
1293, 1525, 1475, 1854, 2189)), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

提前致谢。

【问题讨论】:

  • 您好 :) 为了让我们帮助您,请提供reproducible 示例。例如,要生成最小数据集,可以使用head()subset()。然后使用dput() 给我们一些可以立即放入R 的东西。或者,您可以使用基本 R 数据集,例如 mtcarsirisetc
  • 感谢您的回复,我提供了我正在使用的数据样本
  • 感谢您的数据。我的代码遇到了一些问题。你用什么包? 2)您可以发布您的示例数据的dput()(我对数据的类型有一些疑问,这无法通过您的示例的复制/粘贴来检查)?另外,我有很多错误,例如Objects of type ts not supported by autoplot.。你也有这些吗?
  • 我猜你对“日期”的编码方式有些问题。
  • 好的,我已经添加了数据的 dput() 以及我使用的包。我没有收到那个错误,自动绘图对我来说很好用

标签: r time-series ggtimeseries


【解决方案1】:

好吧,我无法使autoplot()myts 对象一起工作,但基于ylab()xlab(),我制作了这个情节:

当然你可以加geom_line()或者其他的,让它看起来和你期望的一样。

代码:

library(ggplot2)


SalesNL <- structure(list(TransactionDate = structure(c(17287, 17288, 17289, 
                                             17290, 17291, 17292), class = "Date"), NetSalesQty = c(1221, 
                                                                                                    1293, 1525, 1475, 1854, 2189)), row.names = c(NA, -6L), class = c("tbl_df", 
                                                                                                                                                                      "tbl", "data.frame"))


Sales171819 <- SalesNL[SalesNL$TransactionDate >= "2017-01-01" & SalesNL$TransactionDate <= "2019-12-31",]


ggplot(data = Sales171819, 
       aes(x = TransactionDate, 
           y = NetSalesQty, 
           color = ifelse(TransactionDate %in% as.Date(c("2017-05-02", "2017-05-04")), "outstanding", "normal")
                               )
       ) +
  geom_point() +
  scale_x_date(name = "Days",
               # date_breaks = "1 day", # uncheck to get all labels
               breaks = as.Date(c("2017-05-02", "2017-05-04"))) + # just pass a vector with dates you want to highlight
  scale_y_continuous(name = "Sales") +
  scale_color_manual(name = "highlights",
                     values = c("outstanding" = "red", "normal" = "black"))

你也可以反过来,使用基于 y 值的颜色:

ggplot(data = Sales171819,
       aes(x = TransactionDate,
           y = NetSalesQty,
           color = ifelse(
             NetSalesQty >= 1500, 
             "outstanding", # name for the legend and the choice of the color, see scale_color_manual
             "normal") # name for the legend and the choice of the color, see scale_color_manual
       )) +
  geom_point() +
  scale_x_date(name = "Days",
               # date_breaks = "1 day",
               breaks = Sales171819[Sales171819$NetSalesQty >= 1500, 1]$TransactionDate) +
  scale_y_continuous(name = "Sales") +
  scale_color_manual(name = "highlights",
                     values = c("outstanding" = "red", "normal" = "black"))

输出:

【讨论】:

  • 谢谢,这有帮助。我还可以突出显示图表中的点吗?这样我就可以在图表中的某个时间点看到确切的日期?因为我的数据集比样本大得多,所以只有轴上的日期不会清楚地显示哪个日期属于图中的哪个点。
  • 它们是实现高亮的多种方式。我用一种我觉得很好的方式编辑了答案,因为您不需要为突出显示的点创建另一个包含数据的数据框。它提供对图例的完全控制(请参阅 ifelse()scale_color_manual() 之间的链接以创建带有适当标签的图例)。
  • 我将添加 x 轴标签控件以仅在亮点上显示标记
  • @AtalAtmar 我添加了一些线来自定义 x 轴。它符合您的需求吗?请注意,它们是实现您可能喜欢的相同结果的其他方法。
  • 另外,您可以使用基于 y 值的颜色来反之。
猜你喜欢
  • 1970-01-01
  • 2016-03-17
  • 2021-06-10
  • 1970-01-01
  • 2015-08-13
  • 2020-04-12
  • 1970-01-01
  • 2017-11-17
  • 1970-01-01
相关资源
最近更新 更多