Piyush Shah
Piyush Shah

Reputation: 321

Creating vertical line in ggplot with time series data

I have data in tsibble format that I labelled tsdb:

A tsibble: 15,000 x 6 [1M]
# Key:       Industry [60]
# Groups:    Industry [60]
   Industry                Period Sales inventory Purchase Sales.diff
   <chr>                    <mth> <dbl>     <dbl>    <dbl>      <dbl>
1  Apparel manufacturing 2000 Feb  5321     12215     5228         NA

I needed to make plots grouped by industry and so I wrote this code for one industry:

tsdb %>% 
  filter(Industry == "Manufacturing industries") %>% 
  pivot_longer(c(Sales, inventory, Purchase), names_to = "Series", values_to = "Million_Dollars") %>% 
  ggplot(aes(x = Period, y = Million_Dollars, color = Series)) +
  geom_line() +
  facet_grid(vars(Series), scales = "free_y") +
  ggtitle("Manufacturing industries") + 
  theme(plot.title = element_text(hjust = 0.5)) 

This works fine and I get this plot enter image description here

I want to add vertical lines in all the three graphs at select points say "2005 May" and "2008 Jun". To do this, I tried adding:

 geom_vline(xintercept = yearmonth("2005 May"), linetype = 4) +
 

and I get this error

 Error in UseMethod("rescale") : 
   no applicable method for 'rescale' applied to an object of class "c('yearmonth', 'vctrs_vctr')"

I even tried:

 geom_vline(xintercept = yearmonth(424), linetype = 4) +

 geom_vline(xintercept = scale_x_yearmonth(424), linetype = 4) +

 geom_vline(xintercept = scale_x_yearmonth("2005 May"), linetype = 4) +

And I get the error here as:

 Error in xj[i] : object of type 'environment' is not subsettable

I guess the yearmonth needs to be scaled for ggplot to understand, but I’m not sure how to do this. I was trying to get one vertical line and then was hoping that I could use c() and get all the vertical lines that I need. My objective is to get the vertical lines by specifying the time period, e.g. "2005 May".

Upvotes: 2

Views: 1460

Answers (1)

Isabella Ghement
Isabella Ghement

Reputation: 775

One way to get what you want is to create a Date variable from your Period variable. The Period only lists the year and month, so you need to add an artificial day of month (e.g., 1) to the year and month to get the date. Something like this:

tsdb$Date <- paste(tsdb$Period, "1") 

This Date variable is a character variable but you can convert it to a Date variable using the ymd() function in the lubridate package:

tsdb$Date <- lubridate::ymd(tsdb$Date) 

Now you can re-create your plot by replacing x = Period with x = Date in your ggplot. To add your vertical line, just add this layer to your ggplot:

geom_vline(xintercept = lubridate::ymd("2005 May 1"), linetype = 4) 

Upvotes: 2

Related Questions