JVGen
JVGen

Reputation: 601

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

I want to display two plots (ggplot) side-by-side in an html report. Most resources suggest grid.arrange() from the package gridExtra is the simplest way to accomplish this. However, grid.arrange() is changing the proportions of my plots (squishing them along the x-axis). I want to maintain axis proportions. I've played with widths= and heights= options but nothing has worked.

If this isn't easily resolved with grid.arrange(), are there other alternatives for displaying images side-by-side?

P.S. I did see altering height of rows produced by grid.arrange when nrow=1, but the solution did not work for me.

Thanks!

{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)

Expected Proportions grid.arrange() output

Upvotes: 2

Views: 2287

Answers (1)

Vons
Vons

Reputation: 3325

You can control the dimensions of the plots produced in an HTML report by including fig.width and fig.height in the beginning of the code chunk. Try to include {r, fig.width=12, fig.height=4} and see if the plots looks less squashed.

Upvotes: 1

Related Questions