saezlab / liana

LIANA: a LIgand-receptor ANalysis frAmework
https://saezlab.github.io/liana/
GNU General Public License v3.0
169 stars 30 forks source link

Drawing heatmaps and chord diagrams with patchwork #84

Closed rbutleriii closed 1 year ago

rbutleriii commented 1 year ago

Hi,

I have several groups in a list of liana aggregate results, and I have been trying to get the heat_freq and chord_freq plots to cooperate with patchwork, but no success this far

# chord plot for frequency
x11(type='cairo')
p.list = lapply(seq(liana.agg), function(i){
  wrap_elements(chord_freq(liana.agg[i])) + 
    ggtitle(names(liana.agg)[i])
})
wrap_plots(p.list)

# built in dot plots
p.list = lapply(seq(liana.agg), function(i){
  liana_dotplot(liana.agg[[i]], source_groups=c("MG"), ntop=20) + 
    theme(
      axis.text.x=element_text(size=8, angle=45, vjust=1, hjust=1),
      axis.text.y=element_text(size=8),
      axis.title=element_text(size=10)
    ) +
    ggtitle(names(liana.agg)[i])
})
wrap_plots(p.list) + plot_layout(guides="collect")

It does work for liana_dotplot, but I cannot figure out how to wrap or draw the objects with wrap_elements or grid.grabExpr

dbdimitrov commented 1 year ago

Hi,

Likely it does not work with wrap_plots as neither the heatmap nor the chord are ggplot2 objects.

The chord_diagram is a circlelize object and the heatmap is generated with ComplexHeatmap.

You could check how both of these types are usually appended, e.g. https://jokergoo.github.io/ComplexHeatmap-reference/book/a-list-of-heatmaps.html, https://jokergoo.github.io/circlize_book/book/advanced-layout.html#arrange-multiple-plots

Hope this helps!

rbutleriii commented 1 year ago

Success! I eventually figured out to pass the base R objects (heatmap + circlize plots) by passing the function as a formula instead of calling it:

# in this case, liana.agg is a list of aggregated liana results 
liana.trunc = lapply(liana.agg, filter, aggregate_rank <= 1e-5)
outnames = c("Wild Type Veh", "Wild Type C31", "PS19 Veh", "PS19 C31")

# chord plot for frequency
circos.clear()
p.list = lapply(seq(liana.trunc), function(i){
  wrap_elements(full= ~ chord_freq(liana.trunc[[i]])) + 
    ggtitle(outnames[i])
})
ggsave(filename=paste(round_num, nameset, cate, "all_filtered_1e-5.png", sep="."),
       plot=wrap_plots(p.list), dpi=300, width=16, height=16, type="cairo")

This also works if you want to pass additional arguments to chordDiagram, although I haven't played with it a lot yet. Next stop, trying to get the tensor c2c running.