Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patchwork compatibility #2

Closed
jenellewallace opened this issue Jun 20, 2024 · 6 comments
Closed

Patchwork compatibility #2

jenellewallace opened this issue Jun 20, 2024 · 6 comments

Comments

@jenellewallace
Copy link

Is there a way to make heatmaps drawn with these objects compatible with patchwork? I tried below but got an error. Thank you

> g1 <- ggheat(small_mat, height = unit(2,'in'), width = unit(2,'in'))
> p1 <- ggplot(mtcars) + geom_point(aes(mpg, disp))
> 
> g1 + p1
Error in g1 + p1 : non-numeric argument to binary operator
In addition: Warning message:
Incompatible methods ("+.AdditiveUnit", "+.gg") for "+" 
> sessionInfo()
R version 4.4.0 (2024-04-24)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 20.04.6 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3;  LAPACK version 3.9.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

time zone: America/Los_Angeles
tzcode source: system (glibc)

attached base packages:
[1] grid      stats     graphics  grDevices datasets  utils     methods   base     

other attached packages:
[1] patchwork_1.2.0       dplyr_1.1.4           progress_1.2.3        eheat_0.0.0.9000      ggplot2_3.5.1         ComplexHeatmap_2.15.4

loaded via a namespace (and not attached):
 [1] utf8_1.2.4          generics_0.1.3      tidyr_1.3.1         renv_1.0.7          shape_1.4.6.1       hms_1.1.3           digest_0.6.35       magrittr_2.0.3     
 [9] RColorBrewer_1.1-3  iterators_1.0.14    circlize_0.4.16     foreach_1.5.2       doParallel_1.0.17   GlobalOptions_0.1.2 purrr_1.0.2         fansi_1.0.6        
[17] scales_1.3.0        codetools_0.2-19    textshaping_0.4.0   cli_3.6.2           rlang_1.1.4         crayon_1.5.2        munsell_0.5.1       withr_3.0.0        
[25] tools_4.4.0         parallel_4.4.0      colorspace_2.1-0    GetoptLong_1.0.5    BiocGenerics_0.50.0 vctrs_0.6.5         R6_2.5.1            png_0.1-8          
[33] matrixStats_1.3.0   stats4_4.4.0        lifecycle_1.0.4     S4Vectors_0.42.0    IRanges_2.38.0      clue_0.3-65         ragg_1.3.2          cluster_2.1.6      
[41] pkgconfig_2.0.3     pillar_1.9.0        gtable_0.3.5        glue_1.7.0          systemfonts_1.1.0   tibble_3.2.1        tidyselect_1.2.1    rstudioapi_0.16.0  
[49] farver_2.1.2        rjson_0.2.21        labeling_0.4.3      Cairo_1.6-2         compiler_4.4.0      prettyunits_1.2.0  
@Yunuuuu
Copy link
Owner

Yunuuuu commented Jun 21, 2024

Sorry, I think it's not possible to do this since ComplexHeatmap will draw the picture directly instead of returning a gtable object, in such way, patchwork cannot deal with it. but the ComplexHeatmap can align multiple pictures directly, why not just use the ComplexHeatmap annotation function, and gganno can make use of all ggplot2 geometries. A simple example similar with yours can be found in the README.

anno_data <- sample(1:10, nrow(small_mat))
draw(ggheat(small_mat,
  top_annotation = HeatmapAnnotation(
    foo = gganno(
      # Note: vector will be converted one-column data.frame
      # with a column names `V1`
      matrix = anno_data,
      function(p) {
        p + geom_point(aes(.x, V1))
      }
    ), which = "column"
  )
))

@Yunuuuu
Copy link
Owner

Yunuuuu commented Jun 21, 2024

Maybe you can try jokergoo/ComplexHeatmap#261 and jokergoo/ComplexHeatmap#110

@Yunuuuu
Copy link
Owner

Yunuuuu commented Jun 21, 2024

A anno_gg function has been added, which can be used to insert ggplot2 object directly in the annotation.

  library(eheat)
  g <- ggplot(mpg, aes(displ, hwy, colour = class)) +
    geom_point()
  m <- matrix(rnorm(100), 10)
  ggheat(m,
    top_annotation = HeatmapAnnotation(
      ggplot = anno_gg(g, "panel",
        height = unit(6, "cm"),
        show_name = FALSE
      )
    )
  )

image

  ggheat(m,
    top_annotation = HeatmapAnnotation(
      ggplot = anno_gg(g, "plot",
        height = unit(6, "cm"),
        show_name = FALSE
      )
    )
  )

image

@jenellewallace
Copy link
Author

Great, thank you!!

@Yunuuuu
Copy link
Owner

Yunuuuu commented Jun 22, 2024

Another function anno_gg2 has also been added, which provide more control over the ggplot2 elements, please see the documents. In addition eheat_grob accepts a ComplexHeatmap object and return a gTree object, which can be wrap by pathwork.

g <- ggplot(mpg, aes(displ, hwy, colour = class)) +
  geom_point()
h <- ggheat(matrix(rnorm(100), 10))
g <- ggplot(mtcars) +
  geom_point(aes(mpg, disp))
patchwork::wrap_elements(eheat_grob(h)) + g

image

@Yunuuuu
Copy link
Owner

Yunuuuu commented Jun 22, 2024

Close by daa6fd7

@Yunuuuu Yunuuuu closed this as completed Jun 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants