Skip to content

Commit

Permalink
Merge pull request #91 from stemangiola/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
stemangiola authored Jan 28, 2022
2 parents c829c8b + 53cfafb commit b41f17e
Show file tree
Hide file tree
Showing 49 changed files with 3,808 additions and 1,768 deletions.
8 changes: 5 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: tidyHeatmap
Title: A Tidy Implementation of Heatmap
Version: 1.5.1
Version: 1.6.0
Authors@R:
c(person(given = "Stefano",
family = "Mangiola",
Expand Down Expand Up @@ -45,7 +45,8 @@ Imports:
grid,
grDevices,
lifecycle (>= 0.2.0),
dendextend
dendextend,
patchwork
Suggests:
spelling,
testthat,
Expand All @@ -56,7 +57,8 @@ Suggests:
qpdf,
covr,
roxygen2,
forcats
forcats,
ggplot2
VignetteBuilder:
knitr
RdMacros:
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@ export(save_pdf)
export(scale_robust)
export(split_columns)
export(split_rows)
export(wrap_heatmap)
import(ComplexHeatmap)
import(dplyr)
import(grDevices)
import(tidyr)
importFrom(ComplexHeatmap,anno_barplot)
importFrom(ComplexHeatmap,anno_lines)
importFrom(ComplexHeatmap,anno_points)
importFrom(ComplexHeatmap,draw)
importFrom(RColorBrewer,brewer.pal)
importFrom(circlize,colorRamp2)
importFrom(dendextend,cutree)
importFrom(grDevices,colorRampPalette)
importFrom(grid,gpar)
importFrom(grid,grid.grabExpr)
importFrom(grid,grid.points)
importFrom(grid,unit)
importFrom(grid,unit.c)
Expand All @@ -37,6 +40,7 @@ importFrom(magrittr,equals)
importFrom(magrittr,set_rownames)
importFrom(methods,new)
importFrom(methods,show)
importFrom(patchwork,wrap_elements)
importFrom(purrr,as_mapper)
importFrom(purrr,map)
importFrom(purrr,map2)
Expand Down
56 changes: 56 additions & 0 deletions R/patchwork.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#' Wrap tidyHeatmap (ComplexHeatmap) in a patchwork-compliant patch
#'
#' In order to add tidyHeatmap (ComplexHeatmap) element to a patchwork they can be
#' converted to a compliant representation using the `wrap_heatmap()` function.
#' This allows you to position either grobs, ggplot objects, patchwork
#' objects, or even base graphics (if passed as a formula) in either the full
#' area, the full plotting area (anything between and
#' including the axis label), or the panel area (only the actual area where data
#' is drawn).
#'
#' @importFrom patchwork wrap_elements
#' @importFrom grid grid.grabExpr
#' @importFrom ComplexHeatmap draw
#' @importFrom methods show
#'
#' @param panel,plot,full A grob, ggplot, patchwork, formula, raster, or
#' nativeRaster object to add to the respective area.
#'
#' @param clip Should the grobs be clipped if expanding outside its area
#'
#' @param ignore_tag Should tags be ignored for this patch. This is relevant
#' when using automatic tagging of plots and the content of the patch does not
#' qualify for a tag.
#'
#' @docType methods
#' @rdname wrap_heatmap-method
#'
#' @return A wrapped_patch object
#'
#' @export
#'
#' @examples
#'
#'
#' tidyHeatmap::N52 |>
#' tidyHeatmap::heatmap(
#' .row = symbol_ct,
#' .column = UBR,
#' .value = `read count normalised log`,
#' ) |>
#' wrap_heatmap()
#'
setGeneric(
"wrap_heatmap",
function(panel = NULL, plot = NULL, full = NULL, clip = TRUE, ignore_tag = FALSE) standardGeneric("wrap_heatmap")
)

#' Wrap tidyHeatmap (ComplexHeatmap) in a patchwork-compliant patch
#'
#' @docType methods
#' @rdname wrap_heatmap-method
#'
#' @return A wrapped_patch object
setMethod("wrap_heatmap", "InputHeatmap", function(panel = NULL, plot = NULL, full = NULL, clip = TRUE, ignore_tag = FALSE){
patchwork::wrap_elements(grid::grid.grabExpr(ComplexHeatmap::draw(ComplexHeatmap::draw(methods::show(panel)))))
})
3 changes: 3 additions & 0 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,9 @@ combine_elements_with_the_same_name = function(x){

# Fix CRAN notes
my_class = NULL
value = NULL
name = NULL
data = NULL

if(length(unlist(x))==0) return(unlist(x))
else {
Expand Down
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ mtcars_tidy <-
mtcars_tidy
```

## # A tibble: 288 x 5
## # A tibble: 288 × 5
## `Car name` hp vs Property Value[,1]
## <chr> <dbl> <dbl> <chr> <dbl>
## 1 Mazda RX4 110 0 mpg 0.151
Expand Down Expand Up @@ -407,6 +407,11 @@ mtcars_tidy |>

``` r
library(forcats)
```

## Warning: package 'forcats' was built under R version 4.1.2

``` r
mtcars_tidy |>
mutate(`Car name` = fct_reorder(`Car name`, `Car name`, .desc = TRUE)) %>%
heatmap(
Expand Down Expand Up @@ -446,3 +451,27 @@ mtcars_tidy |>
```

![](man/fragments/figures/unnamed-chunk-25-1.png)<!-- -->

## Using patchwork to integrate heatmaps

``` r
library(ggplot2)
library(patchwork)

p_heatmap =
mtcars_tidy |>
heatmap(
`Car name`, Property, Value,
show_heatmap_legend = FALSE,
row_names_gp = gpar(fontsize = 7)
)

p_ggplot = tibble(value = 1:10) %>% ggplot(aes(value)) + geom_density()

wrap_heatmap(p_heatmap) +
p_ggplot +
wrap_heatmap(p_heatmap) +
plot_layout(width = c(1, 0.3, 1))
```

![](man/fragments/figures/unnamed-chunk-26-1.png)<!-- -->
Binary file modified man/fragments/figures/unnamed-chunk-10-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/fragments/figures/unnamed-chunk-11-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/fragments/figures/unnamed-chunk-12-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/fragments/figures/unnamed-chunk-13-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/fragments/figures/unnamed-chunk-14-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/fragments/figures/unnamed-chunk-15-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/fragments/figures/unnamed-chunk-16-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/fragments/figures/unnamed-chunk-17-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/fragments/figures/unnamed-chunk-18-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/fragments/figures/unnamed-chunk-19-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/fragments/figures/unnamed-chunk-20-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/fragments/figures/unnamed-chunk-21-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/fragments/figures/unnamed-chunk-22-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/fragments/figures/unnamed-chunk-23-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/fragments/figures/unnamed-chunk-24-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/fragments/figures/unnamed-chunk-25-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added man/fragments/figures/unnamed-chunk-26-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/fragments/figures/unnamed-chunk-7-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/fragments/figures/unnamed-chunk-9-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions man/fragments/intro.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,25 @@ mtcars_tidy |>
)
```

## Using patchwork to integrate heatmaps

```{r}
library(ggplot2)
library(patchwork)
p_heatmap =
mtcars_tidy |>
heatmap(
`Car name`, Property, Value,
show_heatmap_legend = FALSE,
row_names_gp = gpar(fontsize = 7)
)
p_ggplot = tibble(value = 1:10) %>% ggplot(aes(value)) + geom_density()
wrap_heatmap(p_heatmap) +
p_ggplot +
wrap_heatmap(p_heatmap) +
plot_layout(width = c(1, 0.3, 1))
```
60 changes: 60 additions & 0 deletions man/wrap_heatmap-method.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added tests/testthat/Rplots.pdf
Binary file not shown.
Loading

0 comments on commit b41f17e

Please sign in to comment.