RGLab / COMPASS

Combinatorial Polyfunctionality Analysis of Single Cells
6 stars 11 forks source link

Displaying COMPASS heatmap with other plots in a figure #55

Closed malisas closed 7 years ago

malisas commented 7 years ago

I have a COMPASS heatmap I would like to display in a figure with some other plots, like the figure shown here from this paper: f2 large I am curious how the figure was created. This is the closest I can get using cowplot and some example data:

library(COMPASS) # modified
library(cowplot)
library(ggplot2)
library(gtable)

# Prepare heatmap
cytokine_annotation_colors <- c("black", "black", "black", "black", "black", "black", "black")
grouping <- "Status"
compassResult <- readRDS("/path/to/compassResult.rds")
# Modified plot.COMPASSResult to return heatmap as grob
heatmap_grob <- plot_1.COMPASSResult(compassResult, grouping, show_rownames = FALSE,
                            main = "Heatmap of Mean Probability of Response",
                            fontsize=14, fontsize_row=13, fontsize_col=11,
                            cytokine_annotation_colors=cytokine_annotation_colors)
class(heatmap_grob) # [1] "gTree" "grob"  "gDesc"
# Turn grob into gtable to make compatible with cowplot
heatmap_gtable <- gtable(unit(1, c("grobwidth"), data=heatmap_grob), unit(1, "grobheight", data=heatmap_grob))
heatmap_gtable <- gtable_add_grob(heatmap_gtable, heatmap_grob, 1, 1)

example_plot <- ggplot(data=mtcars, aes(x=mpg, y=cyl)) + geom_point() + labs(title="mtcars example plot")

cowplot_figure <- plot_grid(heatmap_gtable, NULL, example_plot, rel_widths = c(2, 4, 2), labels = c("A", "", "B"), nrow=1)
ggsave(filename="cowplot_fig_example.png",
       plot=cowplot_figure, path="/home/malisa/Desktop", device="png",
       width=9, height=6, units="in")

cowplot_fig_example

This required modifying plot.COMPASSResult to return the final heatmap as a grob and then turning it into a gtable to make compatible with cowplot. As you can see, the plot positions are still all bungled and I'm not sure how to make it look neat. If I do show_rownames = TRUE it looks even worse.

Is there an easy solution using R? If not, I could perhaps save the heatmap as an svg and then put together a figure using some other software...?

gfinak commented 7 years ago

Last time I did something like this, I plot the heatmap to screen, then save it to an object using hmap_fig = grid.grab(wrap=TRUE). Then that can be combined with other ggplot figures using cowplot and wrapping hmap_fig in arrangeGrob(). I'm sure there's a better way, but this worked for me.

malisas commented 7 years ago

Thanks @gfinak . grid.grab() helped so that I don't need to modify plot.COMPASSResult to get something compatible with plot_grid().

plot(compassResult, show_rownames = FALSE,
                     main = "Heatmap of Mean Probability of Response",
                     fontsize=14, fontsize_row=13, fontsize_col=11)
hmap_fig <- grid.grab(wrap=TRUE)

# How to proceed?
hmap_fig_ag <- arrangeGrob(hmap_fig, example_plot, widths = c(2,1))
hmap_fig_ag_g <- grid.arrange(hmap_fig_ag, ncol=1)
plot_grid(hmap_fig_ag_g, labels = "A")

I am having trouble figuring out the correct parameters to pass to arrangeGrob()/grid.arrange()/ plot_grid(). I still get a plot like the one I posted above, where the two plots are on top of each other. Did you need to set the margins or plot size somehow?

gfinak commented 7 years ago
plot_grid(example_plot,arrangeGrob(hmap_fig),labels=c("A","B"))
malisas commented 7 years ago

Thanks, that almost fixes it except for the "B" panel label in the wrong place...I guess I'll look into other solutions, maybe Inkscape? save2

gfinak commented 7 years ago

You can adjust the location of the labels in plot_grid(). Read the docs on that function, they are helpful.

malisas commented 7 years ago

Thank you, that helps.