Home > Mobile >  How to work with yearmonth from tsibble in ggplot and dplyr
How to work with yearmonth from tsibble in ggplot and dplyr

Time:09-25

Working example:

library(fpp3)

Data <- aus_retail %>%
  filter(State == "Western Australia",
         Industry == "Takeaway food services")

autoplot(Data)   xlim()

How would I go about doing something like limit the x axis or filter the data such that only dates after 2000 are stored? Not sure how to work with tsibble's yearmonth variables.

Thanks!

CodePudding user response:

You can use year function from lubridate to extract year from yearmonth object and keep data only after 2000.

library(dplyr)
library(lubridate)

tsibbledata::aus_retail %>%
  filter(State == "Western Australia",
         Industry == "Takeaway food services") %>%
  filter(year(Month) >= 2000)

#   State             Industry               `Series ID`    Month Turnover
#   <chr>             <chr>                  <chr>          <mth>    <dbl>
# 1 Western Australia Takeaway food services A3349435A   2000 Jan     61  
# 2 Western Australia Takeaway food services A3349435A   2000 Feb     56  
# 3 Western Australia Takeaway food services A3349435A   2000 Mar     61.1
# 4 Western Australia Takeaway food services A3349435A   2000 Apr     64.2
# 5 Western Australia Takeaway food services A3349435A   2000 May     65.7
# 6 Western Australia Takeaway food services A3349435A   2000 Jun     64.9
# 7 Western Australia Takeaway food services A3349435A   2000 Jul     62.6
# 8 Western Australia Takeaway food services A3349435A   2000 Aug     62.8
# 9 Western Australia Takeaway food services A3349435A   2000 Sep     62.1
#10 Western Australia Takeaway food services A3349435A   2000 Oct     66.6
# … with 218 more rows
  • Related