Adding dates to x-axis in time series plot

497 Views Asked by At

I have the following code:

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")

I would like to plot the actual dates to the autoplot and ggseasonplot on the x axis, instead of day 1, 2, 3... etc. I would also like to highlight points in the plots with the actual dates. How can I edit my code so I can get this done?

The data looks like this:

  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"))

Thanks in advance.

1

There are 1 best solutions below

5
Paul On

Well, I was not able to make autoplot() work with the myts object but based on the ylab() and xlab(), I made this plot:

enter image description here

Of course you can add geom_line() or others to make it look as you expect.

The code:

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"))

You can also do it the other way around, with a color based on the y value:

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"))

Output: enter image description here