使用 grid.arrange() 在 r Markdown 中并排绘图

Side-By-Side ggplots within rMarkdown using grid.arrange()

我想在 html 报告中并排显示两个图 (ggplot)。大多数资源建议使用 gridExtra 包中的 grid.arrange() 是实现此目的的最简单方法。但是,grid.arrange() 正在改变我的绘图的比例(沿 x 轴挤压它们)。我想保持轴比例。我玩过 widths=heights= 选项,但没有任何效果。

如果使用 grid.arrange() 无法轻松解决此问题,是否还有其他方法可以并排显示图像?

P.S。我确实看到了 altering height of rows produced by grid.arrange when nrow=1,但该解决方案对我不起作用。

谢谢!

{r Images, echo=FALSE}
p1 <- ggplot(pcaData, aes(PC1, PC2, color=By_Experiment, shape=Date)) +
  geom_point(size=3) +
  xlab(paste0("PC1: ",percentVar[1],"% Variance")) +
  ylab(paste0("PC2: ",percentVar[2],"% Variance")) +
  labs(title="PCA of Gene Counts (rLog Transformed)") +
  theme(axis.line = element_line(color = "black"))

p2 <- ggplot(pcaData, aes(PC1, PC2, color=By_Experiment, shape=Calcium_TP)) +
  geom_point(size=3) +
  xlab(paste0("PC1: ",percentVar[1],"% Variance")) +
  ylab(paste0("PC2: ",percentVar[2],"% Variance")) +
  labs(title="PCA of Gene Counts (rLog Transformed)") +
  theme(axis.line = element_line(color = "black"))

p1
p2
grid.arrange(p1, p2, ncol=2)

您可以通过在代码块的开头包含 fig.widthfig.height 来控制 HTML 报告中生成的图的尺寸。尝试包括 {r, fig.width=12, fig.height=4} 并查看图表是否看起来不那么压扁。