I looked for answers in other Qs, couldn't find this Q (or Answer). Using ggplot2 to generate the two plots individually. Then using plot_grid function from the cowplot package to combine them. They two data have exactly the same number of common dates. Thus the x-axis is same time, I want the two graph's grey box to start from the same vertical spot, so that they are time aligned. Presently, due to ylabs of different size, they don't start from same vertical line. Here is a pictorial description:
2 Answers
0
votes
0
votes
If you want a solution that only uses plot_grid, you could do the following (admittedly hackier than the patchwork package):
myPlot1 <- ggplot()
myPlot2 <- ggplot()
#get a ggplot that is the axis only
myYAxis1 <- get_y_axis(myPlot1)
myYAxis2 <- get_y_axis(myPlot2)
#remove all y axis stuff from the plots themselves
myPlot1 <- myPlot1 + theme(axis.text.y = element_blank(), axis.title.y = element_blank(), axis.ticks.y = element_blank())
myPlot2 <- myPlot2 + theme(axis.text.y = element_blank(), axis.title.y = element_blank(), axis.ticks.y = element_blank())
#reassemble plots
ratioAxisToPlot = .1 #determine what fraction of the arranged plot you want to be axis and what fraction you want to be plot)
plot1Reassembled <- plot_grid(myYAxis1, myPlot1, rel_widths = c(ratioAxisToPlot, 1), ncol=2)
plot2Reassembled <- plot_grid(myYAxis2, myPlot2, rel_widths = c(ratioAxisToPlot, 1), ncol=2)
#put it all together
finalPlot <- plot_grid(plot1Reassembled, plot2Reassembled, nrow=2)