Jorge Paredes
Jorge Paredes

Reputation: 1078

Wrong years in quarterly data scales

my dataset looks like this:

> head(df,10)
# A tibble: 10 x 2
   quarters            `GDP Cycle`
   <dttm>                    <dbl>
 1 2000-01-01 00:00:00   -0.00973 
 2 2000-04-01 00:00:00   -0.000653
 3 2000-07-01 00:00:00    0.0125  
 4 2000-10-01 00:00:00    0.0195  
 5 2001-01-01 00:00:00    0.00608 
 6 2001-04-01 00:00:00    0.00562 
 7 2001-07-01 00:00:00   -0.00471 
 8 2001-10-01 00:00:00   -0.00357 
 9 2002-01-01 00:00:00   -0.00137 
10 2002-04-01 00:00:00    0.00144 

And I am using this code on ggplot2 to make this graph:

df %>%
 filter(quarters >= "2014-04-01 09:04:23" & quarters <= "2020-07-01 00:00:00") %>%
 ggplot() +
 aes(x = quarters, y = `GDP Cycle`) +
 geom_line(size = 1L, colour = "#cb181d") +
 labs(x = "Quarters", y = "% Standard deviation from trend", title = "Ecuador's Real GDP Cycle", subtitle = "Hodrick Prescott Filter | λ=1600 ", caption = "BCE GDP") +
 theme_light()+ theme(plot.title = element_text(face = "bold", size = 15))+ theme(plot.subtitle = element_text(size = 10))+
  geom_hline(yintercept = 0,linetype="dashed", size=1)+scale_x_yearqtr(format = '%Y.q%q',n=5)

The problem is the x axis, I need that appears like "2000.q1" and so on.

enter image description here

Upvotes: 1

Views: 60

Answers (1)

Sirius
Sirius

Reputation: 5419

It is not particularly evident from the manual, but if you turn your dates to yearqtr it works. This I would say is quite the (undocumented) weakness, you might want to drop them a line and double check if this is the intended way for it to work.

One way to achieve this is to pile on to more chained stuff on the data:

(remember to chante type after you filter on dates!)


df %>% filter(quarters >= "2014-04-01 09:04:23" & quarters <= "2020-07-01 00:00:00") %>% mutate( quarters = as.yearqtr(quarters) ) %>%
    ggplot( aes(x = quarters, y = `GDP Cycle`) ) +
    geom_line(size = 1L, colour = "#cb181d") +
    labs(x = "Quarters", y = "% Standard deviation from trend", title = "Ecuador's Real GDP Cycle", subtitle = "Hodrick Prescott Filter | λ=1600 ", caption = "BCE GDP") +
    theme_light() +
    theme(plot.title = element_text(face = "bold", size = 15)) +
    theme(plot.subtitle = element_text(size = 10)) +
    geom_hline(yintercept = 0,linetype="dashed", size=1) +
    scale_x_yearqtr(format = '%Yq%q',n=5) +
    geom_blank()

Upvotes: 1

Related Questions