From da2bb73509a11638534b8a053095044e2c9cb57e Mon Sep 17 00:00:00 2001
From: yun
ggplot2 object with a default data and mapping created by
gganno
ggplot(data, aes(.data$x))
(which = "column"
)
/ ggplot(data, ggplot2::aes(y = .data$y))
(which =
-"row"
). The original matrix will be converted into a
-long-data.frame (gganno
always regard row as the
-observations) with following columns.
"row"
).
+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:
.slice
: the slice row (which = "row"
) or
-column (which = "column"
) number..row_names
and .column_names
: the row and
-column names of the original matrix (only applicable when names
-exist)..row_index
and .column_index
: the row and
-column index of the original matrix.x
/ y
: indicating the x-axis (or y-axis)
-coordinates.value
: the actual matrix value of the annotation
-matrix..slice
: the slice row (which = "row"
)
+or column (which = "column"
) number.
.row_names
and .row_index
: the row
+names (only applicable when names exist) and index of the original
+data.
.column_names
and .column_index
: the
+column names (only applicable when names exist) and index of the
+original data
+(only applicable when the original data is a matrix
).
x
/ y
: indicating the x-axis (or
+y-axis) coordinates. Don’t use coord_flip
to flip
+coordinates as it may disrupt internal operations.
value
: the actual matrix value of the annotation
+matrix
+(only applicable when the original data is a matrix
).
gganno
can be seamlessly combined with both
ggheat
and ComplexHeatmap::Heatmap
, although
legends will not be extracted in the later case.
In general, we should just use ggheat
and
-gganno
.
anno_data <- sample(1:10, nrow(small_mat))
+If a matrix is provided, it will be reshaped into long-format
+data.frame
+pdf(NULL)
draw(ggheat(small_mat,
top_annotation = HeatmapAnnotation(
foo = gganno(
- matrix = anno_data,
+ data = matrix(1:10, nrow = nrow(small_mat)),
function(p) {
- p + geom_point(aes(x, value))
- }
- ), which = "column"
- )
-))
-#> ℹ convert simple vector to one-column matrix
-
+ print(head(p$data))
+ p
+ }
+ ), which = "column"
+ )
+))
+#> Warning in matrix(1:10, nrow = nrow(small_mat)): data length [10] is not a
+#> sub-multiple or multiple of the number of rows [9]
+#> .slice .row_index .column_index x value
+#> 1 1 1 1 1 1
+#> 2 1 1 2 1 10
+#> 3 1 2 1 8 2
+#> 4 1 2 2 8 1
+#> 5 1 3 1 6 3
+#> 6 1 3 2 6 2
If a data frame is provided, it will be preserved in its original +form with additional necessary column added.
+pdf(NULL)
+draw(ggheat(small_mat,
+ top_annotation = HeatmapAnnotation(
+ foo = gganno(
+ data = data.frame(
+ value = seq_len(nrow(small_mat)),
+ letter = sample(letters, nrow(small_mat), replace = TRUE)
+ ),
+ function(p) {
+ print(head(p$data))
+ p
+ }
+ ), which = "column"
+ )
+))
+#> .slice .row_names .row_index x value letter
+#> 1 1 1 1 1 1 w
+#> 2 1 2 2 8 2 r
+#> 3 1 3 3 6 3 l
+#> 4 1 4 4 2 4 r
+#> 5 1 5 5 3 5 g
+#> 6 1 6 6 7 6 z
If provided an atomic vector, it will be converted into a matrix and +then reshaped into long-format data.frame.
+pdf(NULL)
+draw(ggheat(small_mat,
+ top_annotation = HeatmapAnnotation(
+ foo = gganno(
+ data = sample(1:10, nrow(small_mat)),
+ function(p) {
+ print(head(p$data))
+ p
+ }
+ ), which = "column"
+ )
+))
+#> ℹ convert simple vector to one-column matrix
+#> .slice .column_names .row_index .column_index x value
+#> 1 1 V1 1 1 1 9
+#> 2 1 V1 2 1 8 3
+#> 3 1 V1 3 1 6 1
+#> 4 1 V1 4 1 2 10
+#> 5 1 V1 5 1 3 7
+#> 6 1 V1 6 1 7 6
Similarly, we can leverage the geometric objects (geoms) provided by
+ggplot2 in ggfn
to create annotation.
anno_data <- sample(1:10, nrow(small_mat))
+draw(ggheat(small_mat,
+ top_annotation = HeatmapAnnotation(
+ foo = gganno(
+ data = anno_data,
+ function(p) {
+ p + geom_point(aes(x, value))
+ }
+ ), which = "column"
+ )
+))
+#> ℹ convert simple vector to one-column matrix
Legends will also be extracted, in the similar manner like passing
them into annotation_legend_list
argument.
draw(ggheat(small_mat,
- top_annotation = HeatmapAnnotation(
- foo = gganno(
- matrix = anno_data,
- function(p) {
- p + geom_bar(aes(y = value, fill = factor(.row_index)), stat = "identity")
- }, height = unit(5, "cm")
- ), which = "column"
- )
-), merge_legends = TRUE)
-#> ℹ convert simple vector to one-column matrix
draw(ggheat(small_mat,
+ top_annotation = HeatmapAnnotation(
+ foo = gganno(
+ data = anno_data,
+ function(p) {
+ p + geom_bar(aes(y = value, fill = factor(.row_index)), stat = "identity")
+ }, height = unit(5, "cm")
+ ), which = "column"
+ )
+), merge_legends = TRUE)
+#> ℹ convert simple vector to one-column matrix
draw(ggheat(small_mat,
- top_annotation = HeatmapAnnotation(
- foo = gganno(
- matrix = anno_data,
- function(p) {
- p + geom_boxplot(aes(y = value, fill = factor(.slice)))
- }, height = unit(5, "cm")
- ), which = "column"
- ), column_km = 2L
-), merge_legends = TRUE)
-#> ℹ convert simple vector to one-column matrix
draw(ggheat(small_mat,
+ top_annotation = HeatmapAnnotation(
+ foo = gganno(
+ data = anno_data,
+ function(p) {
+ p + geom_boxplot(aes(y = value, fill = factor(.slice)))
+ }, height = unit(5, "cm")
+ ), which = "column"
+ ), column_km = 2L
+), merge_legends = TRUE)
+#> ℹ convert simple vector to one-column matrix
box_matrix1 <- matrix(rnorm(ncol(small_mat)^2L, 10), nrow = ncol(small_mat))
-colnames(box_matrix1) <- rep_len("group1", ncol(small_mat))
-box_matrix2 <- matrix(rnorm(ncol(small_mat)^2L, 20), nrow = ncol(small_mat))
-colnames(box_matrix2) <- rep_len("group2", ncol(small_mat))
-draw(ggheat(small_mat,
- top_annotation = HeatmapAnnotation(
- foo = gganno(
- matrix = cbind(box_matrix1, box_matrix2),
- function(p) {
- p +
- geom_violin(
- aes(
- y = value, fill = factor(.column_names),
- color = factor(.slice),
- group = paste(.slice, .row_index, .column_names, sep = "-")
- )
- ) +
- geom_boxplot(
- aes(
- y = value, fill = factor(.column_names),
- color = factor(.slice),
- group = paste(.slice, .row_index, .column_names, sep = "-")
- ),
- width = 0.2,
- position = position_dodge(width = 0.9)
- ) +
- scale_fill_brewer(
- name = "Group", type = "qual", palette = "Set3"
- ) +
- scale_color_brewer(
- name = "Slice", type = "qual", palette = "Set1"
- )
- }, height = unit(3, "cm")
- ), which = "column"
- ), column_km = 2L
-), merge_legends = TRUE)
box_matrix1 <- matrix(rnorm(ncol(small_mat)^2L, 10), nrow = ncol(small_mat))
+colnames(box_matrix1) <- rep_len("group1", ncol(small_mat))
+box_matrix2 <- matrix(rnorm(ncol(small_mat)^2L, 20), nrow = ncol(small_mat))
+colnames(box_matrix2) <- rep_len("group2", ncol(small_mat))
+draw(ggheat(small_mat,
+ top_annotation = HeatmapAnnotation(
+ foo = gganno(
+ data = cbind(box_matrix1, box_matrix2),
+ function(p) {
+ p +
+ geom_violin(
+ aes(
+ y = value, fill = factor(.column_names),
+ color = factor(.slice),
+ group = paste(.slice, .row_index, .column_names, sep = "-")
+ )
+ ) +
+ geom_boxplot(
+ aes(
+ y = value, fill = factor(.column_names),
+ color = factor(.slice),
+ group = paste(.slice, .row_index, .column_names, sep = "-")
+ ),
+ width = 0.2,
+ position = position_dodge(width = 0.9)
+ ) +
+ scale_fill_brewer(
+ name = "Group", type = "qual", palette = "Set3"
+ ) +
+ scale_color_brewer(
+ name = "Slice", type = "qual", palette = "Set1"
+ )
+ }, height = unit(3, "cm")
+ ), which = "column"
+ ), column_km = 2L
+), merge_legends = TRUE)
draw(ggheat(small_mat,
- top_annotation = HeatmapAnnotation(
- foo = gganno(
- matrix = anno_data,
- function(p) {
- p + aes(y = value) + geom_text(aes(label = .row_index))
- }, height = unit(2, "cm")
- ), which = "column"
- ),
- bottom_annotation = HeatmapAnnotation(
- foo = gganno(
- function(p) {
- p + aes(y = value) +
- geom_text(aes(label = .row_index)) +
- scale_y_reverse()
- },
- matrix = anno_data,
- which = "column", height = unit(2, "cm")
- ),
- which = "column"
- ),
- right_annotation = HeatmapAnnotation(
- foo = gganno(
- function(p) {
- p + aes(x = value) +
- geom_text(aes(label = .row_index))
- },
- matrix = anno_data,
- width = unit(3, "cm")
- ),
- which = "row"
- ),
- left_annotation = HeatmapAnnotation(
- foo = gganno(
- function(p) {
- p + aes(x = value) +
- geom_text(aes(label = .row_index)) +
- scale_x_reverse()
- },
- matrix = anno_data,
- width = unit(3, "cm")
- ),
- which = "row"
- ),
- row_km = 2L, column_km = 2L,
-), merge_legends = TRUE)
-#> ℹ convert simple vector to one-column matrix
-#> ℹ convert simple vector to one-column matrix
-#> ℹ convert simple vector to one-column matrix
-#> ℹ convert simple vector to one-column matrix
draw(ggheat(small_mat,
+ top_annotation = HeatmapAnnotation(
+ foo = gganno(
+ data = anno_data,
+ function(p) {
+ p + aes(y = value) + geom_text(aes(label = .row_index))
+ }, height = unit(2, "cm")
+ ), which = "column"
+ ),
+ bottom_annotation = HeatmapAnnotation(
+ foo = gganno(
+ function(p) {
+ p + aes(y = value) +
+ geom_text(aes(label = .row_index)) +
+ scale_y_reverse()
+ },
+ data = anno_data,
+ which = "column", height = unit(2, "cm")
+ ),
+ which = "column"
+ ),
+ right_annotation = HeatmapAnnotation(
+ foo = gganno(
+ function(p) {
+ p + aes(x = value) +
+ geom_text(aes(label = .row_index))
+ },
+ data = anno_data,
+ width = unit(3, "cm")
+ ),
+ which = "row"
+ ),
+ left_annotation = HeatmapAnnotation(
+ foo = gganno(
+ function(p) {
+ p + aes(x = value) +
+ geom_text(aes(label = .row_index)) +
+ scale_x_reverse()
+ },
+ data = anno_data,
+ width = unit(3, "cm")
+ ),
+ which = "row"
+ ),
+ row_km = 2L, column_km = 2L,
+), merge_legends = TRUE)
+#> ℹ convert simple vector to one-column matrix
+#> ℹ convert simple vector to one-column matrix
+#> ℹ convert simple vector to one-column matrix
+#> ℹ convert simple vector to one-column matrix
gganno
can work with Heatmap
function, in
-this way, legends won’t be extracted.
draw(Heatmap(small_mat,
- top_annotation = HeatmapAnnotation(
- foo = gganno(
- matrix = anno_data,
- function(p) {
- p + geom_bar(aes(y = value, fill = factor(.row_index)), stat = "identity")
- }
- ), which = "column"
- )
-), merge_legends = TRUE)
-#> ℹ convert simple vector to one-column matrix
ggheat
and gganno
.
+draw(Heatmap(small_mat,
+ top_annotation = HeatmapAnnotation(
+ foo = gganno(
+ data = anno_data,
+ function(p) {
+ p + geom_bar(aes(y = value, fill = factor(.row_index)), stat = "identity")
+ }
+ ), which = "column"
+ )
+), merge_legends = TRUE)
+#> ℹ convert simple vector to one-column matrix
anno_gg
and
anno_gg2
Both function acts similar with other annotation function in ComplexHeatmap. They accept a ggplot object and fit it in the ComplexHeatmap annotation area.
-g <- ggplot(mpg, aes(displ, hwy, colour = class)) +
- geom_point()
-m <- matrix(rnorm(100), 10)
-
-# anno_gg-panel: clip = "off" -------
-ggheat(m,
- top_annotation = HeatmapAnnotation(
- ggplot = anno_gg(g, "panel",
- clip = "off",
- height = unit(3, "cm"),
- show_name = FALSE
- )
- )
-)
g <- ggplot(mpg, aes(displ, hwy, colour = class)) +
+ geom_point()
+m <- matrix(rnorm(100), 10)
+
+# anno_gg-panel: clip = "off" -------
+ggheat(m,
+ top_annotation = HeatmapAnnotation(
+ ggplot = anno_gg(g, "panel",
+ clip = "off",
+ height = unit(3, "cm"),
+ show_name = FALSE
+ )
+ )
+)
# anno_gg-panel: clip = "on" --------
-ggheat(m,
- top_annotation = HeatmapAnnotation(
- ggplot = anno_gg(g, "panel",
- clip = "on",
- height = unit(3, "cm"),
- show_name = FALSE
- )
- )
-)
# anno_gg-panel: clip = "on" --------
+ggheat(m,
+ top_annotation = HeatmapAnnotation(
+ ggplot = anno_gg(g, "panel",
+ clip = "on",
+ height = unit(3, "cm"),
+ show_name = FALSE
+ )
+ )
+)
# anno_gg-plot --------------------
-ggheat(m,
- top_annotation = HeatmapAnnotation(
- ggplot = anno_gg(g, "plot",
- height = unit(3, "cm"),
- show_name = FALSE
- )
- )
-)
# anno_gg-plot --------------------
+ggheat(m,
+ top_annotation = HeatmapAnnotation(
+ ggplot = anno_gg(g, "plot",
+ height = unit(3, "cm"),
+ show_name = FALSE
+ )
+ )
+)
-# anno_gg-full --------------------
-ggheat(m,
- top_annotation = HeatmapAnnotation(
- ggplot = anno_gg(g, "full",
- height = unit(3, "cm"),
- show_name = FALSE
- )
- )
-)
+# anno_gg-full --------------------
+ggheat(m,
+ top_annotation = HeatmapAnnotation(
+ ggplot = anno_gg(g, "full",
+ height = unit(3, "cm"),
+ show_name = FALSE
+ )
+ )
+)
anno_gg2
is the same with anno_gg
, it
differs in terms of its arguments, and allow more precise adjustment of
the clip feature.
# anno_gg2-panel: margins = NULL -------
-ggheat(m,
- top_annotation = HeatmapAnnotation(
- ggplot = anno_gg2(g, "panel",
- margins = NULL,
- height = unit(3, "cm"),
- show_name = FALSE
- )
- )
-)
# anno_gg2-panel: margins = NULL -------
+ggheat(m,
+ top_annotation = HeatmapAnnotation(
+ ggplot = anno_gg2(g, "panel",
+ margins = NULL,
+ height = unit(3, "cm"),
+ show_name = FALSE
+ )
+ )
+)
# anno_gg2-panel: margins = "l" --------
-ggheat(m,
- top_annotation = HeatmapAnnotation(
- ggplot = anno_gg2(g, "panel",
- margins = "l",
- height = unit(3, "cm"),
- show_name = FALSE
- )
- )
-)
# anno_gg2-panel: margins = "l" --------
+ggheat(m,
+ top_annotation = HeatmapAnnotation(
+ ggplot = anno_gg2(g, "panel",
+ margins = "l",
+ height = unit(3, "cm"),
+ show_name = FALSE
+ )
+ )
+)
# anno_gg2-panel: margins = "r" --------
-ggheat(m,
- top_annotation = HeatmapAnnotation(
- ggplot = anno_gg2(g, "panel",
- margins = "r",
- height = unit(3, "cm"),
- show_name = FALSE
- )
- )
-)
# anno_gg2-panel: margins = "r" --------
+ggheat(m,
+ top_annotation = HeatmapAnnotation(
+ ggplot = anno_gg2(g, "panel",
+ margins = "r",
+ height = unit(3, "cm"),
+ show_name = FALSE
+ )
+ )
+)
# anno_gg2-plot ---------------------
-ggheat(m,
- top_annotation = HeatmapAnnotation(
- ggplot = anno_gg2(g, "plot",
- height = unit(3, "cm"),
- show_name = FALSE
- )
- )
-)
# anno_gg2-plot ---------------------
+ggheat(m,
+ top_annotation = HeatmapAnnotation(
+ ggplot = anno_gg2(g, "plot",
+ height = unit(3, "cm"),
+ show_name = FALSE
+ )
+ )
+)
# anno_gg2-full --------------------
-ggheat(m,
- top_annotation = HeatmapAnnotation(
- ggplot = anno_gg2(
- g + guides(colour = guide_legend(
- theme = theme(
- legend.key.size = unit(1, "mm"),
- legend.text = element_text(size = 10),
- legend.key.spacing = unit(0, "mm"),
- legend.title.position = "bottom",
- legend.key = element_blank()
- ),
- ncol = 2L
- )),
- align_with = "full",
- height = unit(3, "cm"),
- show_name = FALSE
- )
- )
-)
# anno_gg2-full --------------------
+ggheat(m,
+ top_annotation = HeatmapAnnotation(
+ ggplot = anno_gg2(
+ g + guides(colour = guide_legend(
+ theme = theme(
+ legend.key.size = unit(1, "mm"),
+ legend.text = element_text(size = 10),
+ legend.key.spacing = unit(0, "mm"),
+ legend.title.position = "bottom",
+ legend.key = element_blank()
+ ),
+ ncol = 2L
+ )),
+ align_with = "full",
+ height = unit(3, "cm"),
+ show_name = FALSE
+ )
+ )
+)
sessionInfo()
-#> R version 4.4.0 (2024-04-24)
-#> Platform: x86_64-pc-linux-gnu
-#> Running under: Ubuntu 24.04 LTS
-#>
-#> Matrix products: default
-#> BLAS/LAPACK: /usr/lib/x86_64-linux-gnu/libmkl_rt.so; LAPACK version 3.8.0
-#>
-#> locale:
-#> [1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8
-#> [4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
-#> [7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C
-#> [10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C
-#>
-#> time zone: Asia/Shanghai
-#> tzcode source: system (glibc)
-#>
-#> attached base packages:
-#> [1] grid stats graphics grDevices utils datasets methods
-#> [8] base
-#>
-#> other attached packages:
-#> [1] eheat_0.99.7 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
-#> [4] shape_1.4.6.1 digest_0.6.36 magrittr_2.0.3
-#> [7] evaluate_0.24.0 RColorBrewer_1.1-3 iterators_1.0.14
-#> [10] circlize_0.4.16 fastmap_1.2.0 foreach_1.5.2
-#> [13] doParallel_1.0.17 GlobalOptions_0.1.2 purrr_1.0.2
-#> [16] fansi_1.0.6 viridisLite_0.4.2 scales_1.3.0
-#> [19] codetools_0.2-20 cli_3.6.3 rlang_1.1.4
-#> [22] crayon_1.5.3 munsell_0.5.1 withr_3.0.0
-#> [25] yaml_2.3.8 ggh4x_0.2.8 tools_4.4.0
-#> [28] parallel_4.4.0 dplyr_1.1.4 colorspace_2.1-0
-#> [31] GetoptLong_1.0.5 BiocGenerics_0.50.0 vctrs_0.6.5
-#> [34] R6_2.5.1 png_0.1-8 matrixStats_1.3.0
-#> [37] stats4_4.4.0 lifecycle_1.0.4 magick_2.8.3
-#> [40] S4Vectors_0.42.0 IRanges_2.38.0 clue_0.3-65
-#> [43] cluster_2.1.6 pkgconfig_2.0.3 pillar_1.9.0
-#> [46] gtable_0.3.5 glue_1.7.0 Rcpp_1.0.12
-#> [49] highr_0.11 xfun_0.45 tibble_3.2.1
-#> [52] tidyselect_1.2.1 knitr_1.47 farver_2.1.2
-#> [55] rjson_0.2.21 htmltools_0.5.8.1 labeling_0.4.3
-#> [58] rmarkdown_2.27 Cairo_1.6-2 compiler_4.4.0
sessionInfo()
+#> R version 4.4.0 (2024-04-24)
+#> Platform: x86_64-pc-linux-gnu
+#> Running under: Ubuntu 24.04 LTS
+#>
+#> Matrix products: default
+#> BLAS/LAPACK: /usr/lib/x86_64-linux-gnu/libmkl_rt.so; LAPACK version 3.8.0
+#>
+#> locale:
+#> [1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8
+#> [4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
+#> [7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C
+#> [10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C
+#>
+#> time zone: Asia/Shanghai
+#> tzcode source: system (glibc)
+#>
+#> attached base packages:
+#> [1] grid stats graphics grDevices utils datasets methods
+#> [8] base
+#>
+#> other attached packages:
+#> [1] eheat_0.99.7 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
+#> [4] shape_1.4.6.1 digest_0.6.36 magrittr_2.0.3
+#> [7] evaluate_0.24.0 RColorBrewer_1.1-3 iterators_1.0.14
+#> [10] circlize_0.4.16 fastmap_1.2.0 foreach_1.5.2
+#> [13] doParallel_1.0.17 GlobalOptions_0.1.2 purrr_1.0.2
+#> [16] fansi_1.0.6 viridisLite_0.4.2 scales_1.3.0
+#> [19] codetools_0.2-20 cli_3.6.3 rlang_1.1.4
+#> [22] crayon_1.5.3 munsell_0.5.1 withr_3.0.0
+#> [25] yaml_2.3.8 ggh4x_0.2.8 tools_4.4.0
+#> [28] parallel_4.4.0 dplyr_1.1.4 colorspace_2.1-0
+#> [31] GetoptLong_1.0.5 BiocGenerics_0.50.0 vctrs_0.6.5
+#> [34] R6_2.5.1 png_0.1-8 matrixStats_1.3.0
+#> [37] stats4_4.4.0 lifecycle_1.0.4 magick_2.8.3
+#> [40] S4Vectors_0.42.0 IRanges_2.38.0 clue_0.3-65
+#> [43] cluster_2.1.6 pkgconfig_2.0.3 pillar_1.9.0
+#> [46] gtable_0.3.5 glue_1.7.0 Rcpp_1.0.12
+#> [49] highr_0.11 xfun_0.45 tibble_3.2.1
+#> [52] tidyselect_1.2.1 knitr_1.47 farver_2.1.2
+#> [55] rjson_0.2.21 htmltools_0.5.8.1 labeling_0.4.3
+#> [58] rmarkdown_2.27 Cairo_1.6-2 compiler_4.4.0