# Load packages
::p_load(
pacman"tidyverse",
"cowplot",
"grid"
)
Arranging Multiple Plots
1 Example: Differential Gene Expression Analysis in pasilla
data set
Our goal in this section is to combine the results from the previous section into one image. We will use PCA plot pca_dds
, DEG heat map hm_deg
, and volcano plot p_volcano
. Your first step should be to ensure that all plot objects are available in your R environment. To make it easier to arrange in this chapter.
It is also important to make sure that all plots are gg
objects. To do so, simply type class()
followed by the plot object as follows.
# Check the class of the plots
class(pca_dds)
class(hm_deg)
class(p_volcano)
As we can see the hm_deg
that is generated by pheatmap()
function of ComplexHeatmap
library is different from the other plots. In order to make it compatible with other ggplot objects, we need to modify it using grid.grabExpr()
of the ‘grid’ library. The following functions allow you to create a “grob”, “gTree”, or “gList” object that is compatible with ggplot2.
<- grid.grabExpr(draw(hm_deg))
grob_hm
# Check class of the plot object again
class(grob_hm)
All plots can now be arranged together.
- Let’s start by combining PCA and volcano plots.
<- plot_grid(
p_volc_pca + theme(legend.position = "none"),
pca_dds + theme(legend.position = "none"),
p_volcano nrow = 2, labels = "AUTO")
- Combine legend of the PCA and volcano using
get_legend()
.
<- get_legend(pca_dds)
legend_pca <- get_legend(p_volcano) legend_volcano
- Put the first two plots together with the legends
<- plot_grid(p_volc_pca,
p_volc_pca_lgd
legend_pca,
legend_volcano,nrow = 3,
rel_heights = c(1, 0.05, 0.05))
- Then combine with heat map
<- plot_grid(p_volc_pca_lgd,
p_all
grob_hm,ncol = 2,
labels = c("", "C"),
rel_widths = c(0.5, 0.5)) +
theme(panel.background = element_rect(fill = "white", color = NA))
- Save the plot
# Save as PDF
ggsave(filename = "DEG_plots_volc_pca_hm.pdf",
plot = p_all,
scale = 1,
width = 10,
height = 10,
units = "in",
dpi = 400)
# save as PNG
ggsave(filename = "DEG_plots_volc_pca_hm.png",
plot = p_all,
scale = 1,
width = 10,
height = 10,
units = "in",
dpi = 400)
## Practice