In the image below, each of the 33 points/errorbars in Figure B correspond to one of the 33 bars in Figure A. Currently, the figures are in the correct order such that the first point/errorbar in Figure B corresponds to the first bar in Figure A and so on. However, the alignment of the two figures is off. Is there a way to arrange the figures such that each point/errorbar in Figure B is in alignment with its corresponding bar in Figure A? Here are the figures:
The code below has three major steps to allow these plots to be more easily aligned:
Set up the right-hand plot so that it has the same x aesthetic and facetting as the left_hand plot.
Even with corresponding x-ticks and facets, the plots will still not line up because of the legend in the left-hand plot. So, remove the legend from the left-hand plot and then lay out the legend as a separate grob (graphical object) below the left-hand plot.
Add blank vertical space below the right-hand plot equal to the vertical space taken up by the legend we added in step 2.
# Copy legend grob from figure_A
leg = get_legend(figure_A)
# Remove legend from figure_A and lay out figure_A and legend as two separate grobs
figure_A = arrangeGrob(figure_A + guides(fill=FALSE), leg, ncol=1,
heights=c(10,1))
# Lay out figure_B with blank space below it
figure_B = arrangeGrob(figure_B, nullGrob(), ncol=1, heights=c(10,1))
# Lay out the two plots together
figure_final <- ggarrange(figure_A, figure_B, widths=c(1.0, 0.5),
labels=c("A", "B"))
figure_final
But instead of my last block of code (the one that begins # Copy legend grob from figure_A), use @baptiste's code (which uses the ggarrange function from the egg package), which avoids the need to extract and lay out the legend.