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.
library(grid)
library(gridExtra)
strip.col = likert.options()$panel.strip.color
figure_data <- likert(data, grouping=pisaitems$CNT)
figure_A <- plot(figure_data)
summary_figure_data <- summary(figure_data)
figure_B <- ggplot(summary_figure_data,
aes(x=mean, y=Group)) +
geom_errorbarh(aes(xmin=mean-sd, xmax=mean+sd), height=0.3) +
geom_point() +
labs(x="Mean/SD", y="") +
facet_wrap(~Item, ncol=1) +
theme_grey() +
theme(strip.background=element_rect(fill=strip.col, color=strip.col),
panel.background=element_rect(fill=NA, color="grey60"),
axis.text.y=element_blank(),
axis.ticks.y=element_blank())
# 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