Search code examples
rggplot2geom-vline

ggplot geom_vline with binary variable and x-axis dates and long data format


I am trying to create a plot where I have a vline corrispondent to the crisis quarter (crisis variable is binary 1 (crisis)-0 (no-crisis). The piece of code

geom_vline(xintercept = as.yearqtr(c("2016-01- 
01","2017-01-01")), linetype=4)+ #I should have a line only 
in the date where che crisis is 1 (different per each 
country)

Should allow me to have a stright line when the crisis variable is 1.

Here a working example:

# Load Packages
library(ggplot2)
library(zoo)

date <- as.yearqtr(c("2015-01-01","2016-03-01","2017-04-06","2015-01-01","2016-03-01","2017-04-06","2015-01-01","2016-03-01","2017-04-06"))
variable <- c('var1','var1','var1','var2','var2','var2','crisis','crisis','crisis')
value <- c(12,15,18,120,155,175,0,0,1)
specification <- c(1,1,1,1,1,1,1,1,1)
country <- c("AT","AT","AT","AT","AT","AT","AT","AT","AT")

df1 <- data.frame(country, date, variable, specification, value)
View(df1)

date <- as.yearqtr(c("2015-01-01","2016-03-01","2017-04-06","2015-01-01","2016-03-01","2017-04-06","2015-01-01","2016-03-01","2017-04-06"))
variable <- c('var1','var1','var1','var2','var2','var2','crisis','crisis','crisis')
value <- c(15,17,221,150,135,155,0,1,0)
specification <- c(1,1,1,1,1,1,1,1,1)
country <- c("BE","BE","BE","BE","BE","BE","BE","BE","BE")

df2 <- data.frame(country, date, variable, specification, value)
View(df2)



df3 <- rbind(df1,df2)
View(df3)

ch_1 <- ggplot()+
  geom_line(data = df3[df3$specification == levels(factor(df3$specification))[1] & df3$variable == "var1" ,], 
            aes(x = date, 
                y = value,
                #colour = specification ##in the actual code this is uncommented
                ))+
  geom_line(data = df3[df3$specification == levels(factor(df3$specification))[1] & df3$variable == "var2" ,], 
            aes(x = date, 
                y = value,
                #colour = specification ##in the actual code this is uncommented
                ))+

  #should change here ---
  geom_vline(xintercept = as.yearqtr(c("2016-01-01","2017-01-01")), linetype=4)+ #I should have a line only in the date where che crisis is 1 (different per each country)
  # ---------------------
  facet_wrap(~country, scales = 'free_y')+
  theme(axis.text.x = element_text(angle = 45, hjust = 1,face="bold",size=9))

ch_1

Thanks


Solution

  • If I get you correct, "2016-01-01" is meant for AT and "2017-01-01" for BE. So you need to specify this group in geom_vline for it to allocate to the correct panel:

    Make data frame for crisis years with country:

    crisis_year=df3[df3$value == 1,c("country","date")]
    

    Plot, making sure to pass this into geom_vline:

    ch_1 <- ggplot()+
      geom_line(data = df3[df3$specification == levels(factor(df3$specification))[1] & df3$variable == "var1" ,], 
                aes(x = date, 
                    y = value
                    #colour = specification ##in the actual code this is uncommented
                    ))+
      geom_line(data = df3[df3$specification == levels(factor(df3$specification))[1] & df3$variable == "var2" ,], 
                aes(x = date, 
                    y = value
                    ))+
      geom_vline(data=crisis_year,aes(xintercept = date), linetype=4)+
      facet_wrap(~country, scales = 'free_y')+
      theme(axis.text.x = element_text(angle = 45, hjust = 1,face="bold",size=9))
    
    ch_1
    

    enter image description here