Skip to content

Commit

Permalink
fix lambda in eanno
Browse files Browse the repository at this point in the history
  • Loading branch information
Yunuuuu committed Jul 11, 2024
1 parent fb5ff33 commit dcdff7d
Show file tree
Hide file tree
Showing 20 changed files with 595 additions and 295 deletions.
2 changes: 1 addition & 1 deletion R/eanno.R
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ eanno <- function(draw_fn, ..., data = NULL, which = NULL, subset_rule = NULL,
anno@dots <- dots
anno@data <- data
anno@which <- which
anno@fun <- draw_fn
anno@fun <- unclass(draw_fn)
anno@fun_name <- fun_name %||% "eanno"
anno_size <- anno_width_and_height(which, width, height, unit(1, "cm"))
anno@width <- anno_size$width
Expand Down
5 changes: 3 additions & 2 deletions R/gganno.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#' @section ggfn:
#'
#' `ggfn` accept a ggplot2 object with a default data and mapping created by
#' `ggplot(data, aes(.data$x))` / `ggplot(data, ggplot2::aes(y = .data$y))`.
#'
#' `ggplot(data, aes(.data$x))` / `ggplot(data, ggplot2::aes(y = .data$y))`.
#'
#' If the original data is a matrix, it'll be reshaped into a long-format
#' data frame in the `ggplot2` plot data. The final ggplot2 plot data will
#' contain following columns:
Expand Down Expand Up @@ -40,6 +40,7 @@
#' @name gganno
gganno <- function(ggfn, ..., data = NULL,
which = NULL, width = NULL, height = NULL) {
if (missing(ggfn)) ggfn <- ~.x
out <- eanno(
draw_fn = ggfn, ..., data = data, subset_rule = NULL,
which = which, width = width, height = height,
Expand Down
678 changes: 387 additions & 291 deletions README.html

Large diffs are not rendered by default.

123 changes: 122 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,127 @@ dev.off()
#> 2
```

If no data is provided, the heatmap matrix will be used, the same
principal applied in the matrix (reshaped into a long-format data
frame). Note: for column annotations, the heatmap matrix will be
transposed, since `gganno` will always regard row as the observations.

``` r
pdf(NULL)
draw(ggheat(small_mat,
top_annotation = HeatmapAnnotation(
foo = gganno(
data = NULL,
function(p) {
print(head(p$data))
p
}
), which = "column"
)
))
#> .slice .row_names .column_names .row_index .column_index x value
#> 1 1 column1 row1 1 1 1 0.90474160
#> 2 1 column1 row2 1 2 1 0.90882972
#> 3 1 column1 row3 1 3 1 0.28074668
#> 4 1 column1 row4 1 4 1 0.02729558
#> 5 1 column1 row5 1 5 1 -0.32552445
#> 6 1 column1 row6 1 6 1 0.58403269
```

``` r
dev.off()
#> png
#> 2
```

``` r
pdf(NULL)
draw(ggheat(small_mat,
left_annotation = HeatmapAnnotation(
foo = gganno(
data = NULL,
function(p) {
print(head(p$data))
p
}
),
which = "row"
)
))
#> .slice .row_names .column_names .row_index .column_index y value
#> 1 1 row1 column6 1 6 2 0.1621522
#> 2 1 row1 column9 1 9 2 -0.1629658
#> 3 1 row1 column1 1 1 2 0.9047416
#> 4 1 row1 column7 1 7 2 -0.2869867
#> 5 1 row1 column8 1 8 2 0.6803262
#> 6 1 row1 column4 1 4 2 1.2676994
```

``` r
dev.off()
#> png
#> 2
```

You can also supply a function (`purrr-lambda` is also okay) in the
data, which will be applied in the heatmap matrix. Note: for column
annotations, the heatmap matrix will be transposed before pass into this
function.

``` r
pdf(NULL)
draw(ggheat(small_mat,
top_annotation = HeatmapAnnotation(
foo = gganno(
data = function(x) {
if (identical(x, small_mat)) {
print("matrix not transposed")
} else if (identical(x, t(small_mat))) {
print("matrix transposed")
}
rowSums(x)
}
),
which = "column"
)
))
#> [1] "matrix transposed"
#> ℹ convert simple vector to one-column matrix
```

``` r
dev.off()
#> png
#> 2
```

``` r
pdf(NULL)
draw(ggheat(small_mat,
left_annotation = HeatmapAnnotation(
foo = gganno(
data = function(x) {
if (identical(x, small_mat)) {
print("matrix not transposed")
} else if (identical(x, t(small_mat))) {
print("matrix transposed")
}
rowSums(x)
}
),
which = "row"
)
))
#> [1] "matrix not transposed"
#> ℹ convert simple vector to one-column matrix
```

``` r
dev.off()
#> png
#> 2
```

Similarly, we can leverage the geometric objects (geoms) provided by
ggplot2 in `ggfn` to create annotation.

Expand Down Expand Up @@ -856,7 +977,7 @@ sessionInfo()
#> [8] base
#>
#> other attached packages:
#> [1] eheat_0.99.7 ggplot2_3.5.1 ComplexHeatmap_2.20.0
#> [1] eheat_0.99.8 ggplot2_3.5.1 ComplexHeatmap_2.20.0
#>
#> loaded via a namespace (and not attached):
#> [1] utf8_1.2.4 generics_0.1.3 tidyr_1.3.1
Expand Down
Binary file modified man/figures/README-Heatmap_gganno-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/figures/README-anno_bar-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/figures/README-anno_box-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/figures/README-anno_gg-panel-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/figures/README-anno_gg-panel-clip-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/figures/README-anno_gg-plot-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/figures/README-anno_gg-plot-2.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/figures/README-anno_gg2-full-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/figures/README-anno_gg2-panel1-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/figures/README-anno_gg2-panel2-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/figures/README-anno_gg2-panel3-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/figures/README-anno_gg2-plot-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/figures/README-anno_point-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/figures/README-anno_violin-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/figures/README-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.
82 changes: 82 additions & 0 deletions vignettes/eheat.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,88 @@ draw(ggheat(small_mat,
dev.off()
```

If no data is provided, the heatmap matrix will be used, the same principal
applied in the matrix (reshaped into a long-format data frame). Note: for column
annotations, the heatmap matrix will be transposed, since `gganno` will always
regard row as the observations.
```{r anno_data_null_column}
pdf(NULL)
draw(ggheat(small_mat,
top_annotation = HeatmapAnnotation(
foo = gganno(
data = NULL,
function(p) {
print(head(p$data))
p
}
), which = "column"
)
))
dev.off()
```

```{r anno_data_null_row}
pdf(NULL)
draw(ggheat(small_mat,
left_annotation = HeatmapAnnotation(
foo = gganno(
data = NULL,
function(p) {
print(head(p$data))
p
}
),
which = "row"
)
))
dev.off()
```

You can also supply a function (`purrr-lambda` is also okay) in the data, which
will be applied in the heatmap matrix. Note: for column annotations, the heatmap
matrix will be transposed before pass into this function.

```{r anno_data_fun_column}
pdf(NULL)
draw(ggheat(small_mat,
top_annotation = HeatmapAnnotation(
foo = gganno(
data = function(x) {
if (identical(x, small_mat)) {
print("matrix not transposed")
} else if (identical(x, t(small_mat))) {
print("matrix transposed")
}
rowSums(x)
}
),
which = "column"
)
))
dev.off()
```

```{r anno_data_fun_row}
pdf(NULL)
draw(ggheat(small_mat,
left_annotation = HeatmapAnnotation(
foo = gganno(
data = function(x) {
if (identical(x, small_mat)) {
print("matrix not transposed")
} else if (identical(x, t(small_mat))) {
print("matrix transposed")
}
rowSums(x)
}
),
which = "row"
)
))
dev.off()
```


Similarly, we can leverage the geometric objects (geoms) provided by ggplot2 in
`ggfn` to create annotation.
```{r anno_point}
Expand Down

0 comments on commit dcdff7d

Please sign in to comment.