rfairy
rfairy

Reputation: 51

Combine plots with same y-axes different x-axes

I am looking into how the bond market reacts to certain events in a seven day window.

Therefore, I want to create a figure that plots the bond yield reactions around the dates. My data looks like following

df <- Date       APP  DE10 
      2014-09-22 0    1.010 
      2014-09-19 0    1.043
      2014-09-18 0    1.081
      2014-09-17 0    1.050
      2014-09-16 0    1.061
      2014-09-15 0    1.067
      2014-09-12 1    1.082
      2014-09-11 0    1.041
      2014-09-10 0    1.047
      2014-09-09 0    0.996
      2014-09-08 0    0.953
      2014-09-05 0    0.928
      2014-09-04 1    0.970
      2014-09-03 0    0.955
      2014-09-02 0    0.931
      2014-09-01 0    0.882

APP is a dummy that indicates the date of events. I made the figure below using grid.arrange to put together each country's bond yields plotted as

DE10 <- ggplot(Bond10) + 
   geom_line(aes(x=Date, y=DE10)) +
   labs(title="Germany", x="", y="Bond yield") +
   scale_x_date(limits = as.Date(c('2017-10-23','2017-10-30')), 
   expand=c(0.1,0))+
   geom_vline(aes(xintercept=as.Date("2017-10-26"), color="Event"))+
   scale_y_continuous(limits = c(0.3, 0.6), expand = c(0.1,0)) +  
   theme(legend.position='none',
       axis.text.x=element_blank(),axis.ticks.x=element_blank(), 
       plot.margin=unit(c(0.2,0.2,0,0.2), "cm"))

I want the figure below to include multiple lines in each plot under each country plot to see how strong the reactions have been to different events.

Figure of bond yields

enter image description here

Upvotes: 1

Views: 71

Answers (1)

tjebo
tjebo

Reputation: 23747

Additionally to what @MichaelChirico suggested, here a solution

require(tidyverse)
Bond10$Date <- ymd(Bond10$Date) #needed to do this because the dates were imported as characters 

Bond10 <- Bond10 %>% mutate(APP_date = ifelse(APP == 1, Date, NA))

ggplot(Bond10) + 
  geom_line(aes(x=Date, y=DE10)) +
  geom_vline(aes(xintercept= APP_date , color="Event"))

#I removed the redundant bits and pieces, especially your totally wrong x-limits

enter image description here

Upvotes: 1

Related Questions