thomasp85 / patchwork

The Composer of ggplots
https://patchwork.data-imaginist.com
Other
2.47k stars 163 forks source link

Let user to add their own class to put into `patchwork`. #393

Open Yunuuuu opened 1 month ago

Yunuuuu commented 1 month ago

I have modified the patchwork process to let user add their classes for alignment instead of using is_valid_plot: https://github.com/thomasp85/patchwork/blob/6d14415df326a545da4de6140f8433c679eac4fb/R/wrap_plots.R#L58

What the developer should do is to add alignpatch method (If necessary, we can define the detail behaviour with a subclass of Patch which is a ggproto class): https://github.com/Yunuuuu/ggalign/blob/01f6063311313e5dff76e34ea77e92f5e2cffbb1/R/alignpatch-.R#L146

Now, I have added methods for my own classes (two Layout) in ggalign, classes from ComplexHeatmap, and patchwork class.

knitr::opts_knit$set(upload.fun = identity)
library(ggalign)
#> Loading required package: ggplot2
  p1 <- ggplot(mtcars, aes(mpg)) +
    geom_density() +
    facet_wrap(~vs)
  p2 <- ggplot(mtcars, aes(mpg)) +
    geom_density() +
    facet_wrap(~vs)
  align_plots(p1,
    free_vp(p2, y = 0, height = unit(10, "cm"), just = "bottom"),
    ComplexHeatmap::Heatmap(rnorm(100L), column_names_rot = 0),
    patchwork::wrap_plots(p1, p2),
    nrow = 2L
  )

image

Created on 2024-09-14 with reprex v2.1.0 ~

Yunuuuu commented 1 month ago

I want to push all these features to pachwork, since ggalign focus on plot which need reorder the axis like dendrogram (we need reorder the data before using patchwork to compose the plot). See the column names of the heatmap (they are reordered to match the dendrogram):

set.seed(123)
small_mat <- matrix(rnorm(81), nrow = 9)
rownames(small_mat) <- paste0("row", seq_len(nrow(small_mat)))
colnames(small_mat) <- paste0("column", seq_len(ncol(small_mat)))
ggheatmap(small_mat) +
  hmanno("t") +
  align_dendro(aes(color = branch), k = 3) +
  geom_point(aes(color = branch, y = y))

thomasp85 commented 1 month ago

This seems like a duplicate of #193 - I agree this would be useful

Yunuuuu commented 1 month ago

Thanks, that's what I wanted, I missed the issue.