-1

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.

Atal Atmar
  • 25
  • 5
  • 1
    Hello :) In order for us to help you, please provide a [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) example. For example, to produce a minimal data set, you can use `head()`, `subset()`. Then use `dput()` to give us something that can be put in R immediately. Alternatively, you can use base R datasets such as `mtcars`, `iris`, *etc*. – Paul Sep 29 '20 at 11:09
  • Thanks for your response, I have provided a sample of the data that I am using – Atal Atmar Sep 29 '20 at 11:57
  • Thanks for the data. I am facing some problems with your code. What packages do you use? 2) can you post `dput()` of your sample data (I have some doubt about the type of the data, this is not possible to check is with a copy/paste of your sample)? Also, I have many errors such as `Objects of type ts not supported by autoplot.`. Do you have these too? – Paul Sep 29 '20 at 12:16
  • My guess is that you have some problems with the way "dates" are encoded. – Paul Sep 29 '20 at 12:25
  • 1
    Alright, I have added the dput() of the data and also the packages that I use. I don't get that error, autoplot works just fine for me – Atal Atmar Sep 29 '20 at 12:26
  • I made an attempt to make the expected plot. As I was not able to make `autoplot()` work, I used `ggplot2` only to make something that might look like what you expect, feel free to add comments to adjust the look of it. – Paul Sep 29 '20 at 12:37

1 Answers1

1

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

Paul
  • 2,850
  • 1
  • 12
  • 37
  • Thank you, this helps. Can I also highlights points in the graph? So that I see the exact date at some point in the graph? Beause my dataset is much bigger than the sample, so just the dates on the axis wont give a clear view of which date belongs to which point in the plot. – Atal Atmar Sep 29 '20 at 12:39
  • They are multiple way to achieve highlighting. I edited the answer with one way I found nice because you do not need to create another dataframe with data for the highlighted points. It offers full control on the legend (see the link between `ifelse()` and `scale_color_manual()` to create a legend wit proper labels). – Paul Sep 29 '20 at 13:00
  • I will add x-axis label control to only display marks on hilights – Paul Sep 29 '20 at 13:03
  • @AtalAtmar I have added some lines to custom the x-axis. Does it match your needs? Be aware that they are other ways to achieve the same result that you might prefer. – Paul Sep 29 '20 at 13:10
  • Also, you can do it the other way around with color based of the y value. – Paul Sep 29 '20 at 13:19