3.14文字標(biāo)注
文本可以用anno_text()注釋。圖形參數(shù)由gp控制。
ha = rowAnnotation(foo = anno_text(month.name, gp = gpar(fontsize = 1:12+4)))

位置由location和just控制。旋轉(zhuǎn)由rot控制。
ha = rowAnnotation(foo = anno_text(month.name, location = 1, rot = 30,
just = "right", gp = gpar(fontsize = 1:12+4)))

ha = rowAnnotation(foo = anno_text(month.name, location = 0.5, just = "center"))

location和just根據(jù)放置在熱圖中的注釋的位置自動計算(例如,如果文本是熱圖的右注釋,則文本向左對齊,如果它是左注釋則向右對齊)。
寬度/高度是根據(jù)所有文本自動計算的。通常你不需要手動設(shè)置它的寬度/高度。
可以通過gp設(shè)置背景顏色。這里fill控制填充背景顏色,col控制文本顏色,border控制背景邊框顏色。
您可以看到我們明確設(shè)置width為最長文本寬度的 1.2 倍。
ha = rowAnnotation(foo = anno_text(month.name, location = 0.5, just = "center",
gp = gpar(fill = rep(2:4, each = 4), col = "white", border = "black"),
width = max_text_width(month.name)*1.2))

可以通過與gridtext 包集成來繪制更復(fù)雜的文本(gridtext注釋)。一個簡單的例子如下:
text = sapply(LETTERS[1:10], function(x) {
qq("<span style='color:red'>**@{x}**<sub>@{x}</sub></span>_@{x}_<sup>@{x}</sup>")
})
ha = rowAnnotation(
foo = anno_text(gt_render(text, align_widths = TRUE,
r = unit(2, "pt"),
padding = unit(c(2, 2, 2, 2), "pt")),
gp = gpar(box_col = "blue", box_lwd = 2),
just = "right",
location = unit(1, "npc")
))

與其他注釋不同,默認(rèn)情況下文本注釋沒有注釋標(biāo)題。標(biāo)題可以在anno_text()通過設(shè)置show_name = TRUE被添加:
m = matrix(rnorm(100), 10)
Heatmap(m) + rowAnnotation(month = anno_text(month.name[1:10], just = "center",
location = unit(0.5, "npc"), show_name = TRUE),
annotation_name_rot = 0)

3.15標(biāo)記注釋
有時熱圖中有很多行或列,我們想標(biāo)記其中的一些。anno_mark()用于標(biāo)記行或列的子集并用線連接到標(biāo)簽。
anno_mark()至少需要兩個參數(shù):at、labels,其中at是原始矩陣的索引和labels相應(yīng)的文本。
m = matrix(rnorm(1000), nrow = 100)
rownames(m) = 1:100
ha = rowAnnotation(foo = anno_mark(at = c(1:4, 20, 60, 97:100), labels = month.name[1:10]))
Heatmap(m, name = "mat", cluster_rows = FALSE, right_annotation = ha,
row_names_side = "left", row_names_gp = gpar(fontsize = 4))
Heatmap(m, name = "mat", cluster_rows = FALSE, right_annotation = ha,
row_names_side = "left", row_names_gp = gpar(fontsize = 4), row_km = 4)

文本位置的計算取決于圖形設(shè)備的絕對大小。如果您調(diào)整當(dāng)前交互設(shè)備的大小或grid.grabExpr() 用于捕獲當(dāng)前繪圖,您可能會看到文本的位置都已損壞。解決方法請參考cowplot。
3.16 摘要注釋
有一個特殊的注釋anno_summary()只適用于一列熱圖或一行熱圖(我們可以說熱圖只包含一個向量)。它顯示了熱圖中向量的匯總統(tǒng)計信息。如果相應(yīng)的向量是離散的,則摘要注釋顯示為條形圖,如果向量是連續(xù)的,則摘要注釋是箱線圖。 anno_summary()總是在熱圖分割時使用,以便可以在熱圖切片之間比較統(tǒng)計數(shù)據(jù)。
第一個示例顯示了離散熱圖的摘要注釋。條形圖顯示每個切片中每個級別的比例。熱圖切片的高度已經(jīng)可以看到絕對值。
條形圖的顏色模式是從熱圖中自動獲取的。
ha = HeatmapAnnotation(summary = anno_summary(height = unit(4, "cm")))
v = sample(letters[1:2], 50, replace = TRUE)
split = sample(letters[1:2], 50, replace = TRUE)
Heatmap(v, name = "mat", col = c("a" = "red", "b" = "blue"),
top_annotation = ha, width = unit(2, "cm"), row_split = split)

第二個示例顯示了連續(xù)熱圖的摘要注釋。圖形參數(shù)應(yīng)由gp手動設(shè)置??梢园凑?a target="_blank">離散圖例最后第二段中的介紹創(chuàng)建和添加箱線圖的圖例。
ha = HeatmapAnnotation(summary = anno_summary(gp = gpar(fill = 2:3),
height = unit(4, "cm")))
v = rnorm(50)
Heatmap(v, name = "mat", top_annotation = ha, width = unit(2, "cm"),
row_split = split)

通常我們不會繪制這個單列熱圖。它總是與其他“主要熱圖”結(jié)合在一起。例如,帶有單列熱圖的基因表達(dá)矩陣,顯示該基因是蛋白質(zhì)編碼基因還是 linc-RNA 基因。
在下面,我們展示了一個帶有兩個單列熱圖的“主熱圖”的簡單示例。熱圖連接的功能將在熱圖列表介紹。
m = matrix(rnorm(50*10), nrow = 50)
ht_list = Heatmap(m, name = "main_matrix")
ha = HeatmapAnnotation(summary = anno_summary(height = unit(3, "cm")))
v = sample(letters[1:2], 50, replace = TRUE)
ht_list = ht_list + Heatmap(v, name = "mat1", top_annotation = ha, width = unit(1, "cm"))
ha = HeatmapAnnotation(summary = anno_summary(gp = gpar(fill = 2:3),
height = unit(3, "cm")))
v = rnorm(50)
ht_list = ht_list + Heatmap(v, name = "mat2", top_annotation = ha, width = unit(1, "cm"))
split = sample(letters[1:2], 50, replace = TRUE)
lgd_boxplot = Legend(labels = c("group a", "group b"), title = "group",
legend_gp = gpar(fill = c("red", "blue")))
draw(ht_list, row_split = split, ht_gap = unit(5, "mm"),
heatmap_legend_list = list(lgd_boxplot))

3.17 縮放/鏈接注釋
anno_mark()將熱圖上的單行或單列連接到標(biāo)簽,下一個注釋功能anno_link()將行或列的子集連接到可以添加更全面圖形的繪圖區(qū)域。請參閱以下示例,其中我們?yōu)槊總€行組制作箱線圖。
set.seed(123)
m = matrix(rnorm(100*10), nrow = 100)
subgroup = sample(letters[1:3], 100, replace = TRUE, prob = c(1, 5, 10))
rg = range(m)
panel_fun = function(index, nm) {
pushViewport(viewport(xscale = rg, yscale = c(0, 2)))
grid.rect()
grid.xaxis(gp = gpar(fontsize = 8))
grid.boxplot(m[index, ], pos = 1, direction = "horizontal")
popViewport()
}
anno = anno_link(align_to = subgroup, which = "row", panel_fun = panel_fun,
size = unit(2, "cm"), gap = unit(1, "cm"), width = unit(4, "cm"))
Heatmap(m, name = "mat", right_annotation = rowAnnotation(foo = anno), row_split = subgroup)

anno_zoom()的重要參數(shù)是:
-
align_to:它定義了繪圖區(qū)域(或框)如何對應(yīng)于熱圖中的行或列。如果該值是一個索引列表,則每個框?qū)?yīng)于列表中一個向量中帶有索引的行或列。如果該值是與熱圖中的行或列具有相同長度的分類變量(例如因子或字符向量),則每個框?qū)?yīng)于分類變量中每個級別的行/列。 -
panel_fun: 自定義函數(shù),定義如何在框中繪制圖形。該函數(shù)必須有一個index參數(shù),它是框?qū)?yīng)的行/列的索引。它可以有第二個參數(shù)nm,即熱圖中所選部分的“名稱”。如果將其指定為分類變量或帶有名稱的列表,則對應(yīng)的值nm來自align_to。 -
size: 盒子的大小。它們可以是純數(shù)字,它們被視為熱圖總高度/寬度的相對分?jǐn)?shù)。size的值也可以是絕對單位。 -
gap: 盒子之間的間隙。它應(yīng)該是一個unit對象。
anno_link() 也適用于列注釋。
另一個使用anno_link()示例是將詞云列表與行組對應(yīng),請參閱博客文章將詞海應(yīng)用到熱圖注釋以了解如何使用anno_link().

與anno_mark()類似,繪圖區(qū)域的位置也取決于圖形設(shè)備的絕對大小。如果您調(diào)整當(dāng)前交互設(shè)備的大小或grid.grabExpr()用于捕獲當(dāng)前繪圖,您可能會看到文本的位置都已損壞。解決方法請參考cowplot。
3.18 多重注解
3.18.1 一般設(shè)置
如前所述,要在HeatmapAnnotation()中放置多個注釋,只需將它們指定為名稱-值對。在HeatmapAnnotation()中,有一些參數(shù)控制多個注釋。對于這些參數(shù),它們被指定為長度與注釋數(shù)量相同的向量,或具有注釋子集的命名向量。
被指定為向量、矩陣和數(shù)據(jù)框的簡單注釋將自動在熱圖上有圖例。show_legend控制是否為它們繪制圖例。請注意,如果show_legend是向量,則show_legend的值應(yīng)為以下格式之一:
- 長度與簡單注釋數(shù)量相同的邏輯向量。
- 一個與totla注釋數(shù)量相同長度的邏輯向量。復(fù)雜注釋的值將被忽略。
- 用于控制簡單注釋子集的命名向量。
自定義注釋圖例,請參考熱圖圖例注釋節(jié)。
ha = HeatmapAnnotation(foo = 1:10,
bar = cbind(1:10, 10:1),
pt = anno_points(1:10),
show_legend = c("bar" = FALSE)
)
Heatmap(matrix(rnorm(100), 10), name = "mat", top_annotation = ha)

gp控制簡單注釋的圖形參數(shù)(除了fill),例如注釋網(wǎng)格的邊框。
ha = HeatmapAnnotation(foo = 1:10,
bar = cbind(1:10, 10:1),
pt = anno_points(1:10),
gp = gpar(col = "red")
)

border控制每個注釋的邊界。 show_annotation_name控制是否顯示注釋名稱。如前所述,該值可以是單個值、向量或命名向量。
ha = HeatmapAnnotation(foo = 1:10,
bar = cbind(1:10, 10:1),
pt = anno_points(1:10),
show_annotation_name = c(bar = FALSE), # only turn off `bar`
border = c(foo = TRUE) # turn on foo
)

annotation_name_gp, annotation_name_offset,annotation_name_side和 annotation_name_rot控制注釋名稱的樣式和位置。后三個可以指定為命名向量。如果 annotation_name_offset指定為命名向量,則可以指定為字符而不是unit對象:annotation_name_offset = c(foo = "1cm")。
gap控制每兩個相鄰注釋之間的空間。該值可以是單個值或單位向量。
ha = HeatmapAnnotation(foo = 1:10,
bar = cbind(1:10, 10:1),
pt = anno_points(1:10),
gap = unit(2, "mm"))

ha = HeatmapAnnotation(foo = 1:10,
bar = cbind(1:10, 10:1),
pt = anno_points(1:10),
gap = unit(c(2, 10), "mm"))

3.18.2注釋的大小
height, width,annotation_height并annotation_width控制完整熱圖注釋的高度或?qū)挾?。通常你不需要設(shè)置它們,因為所有單個注釋都有固定的高度/寬度,整個熱圖注釋的最終高度/寬度是它們的總和。調(diào)整這些值的大小將涉及相當(dāng)復(fù)雜的調(diào)整,具體取決于它是簡單注釋還是復(fù)雜注釋。調(diào)整熱圖列表時也會調(diào)整熱圖注釋的大小。在下面的例子中,我們以列注解為例,展示了一些調(diào)整大小的場景。
首先是默認(rèn)高度ha:
# foo: 1cm, bar: 5mm, pt: 1cm
ha = HeatmapAnnotation(foo = cbind(1:10, 10:1),
bar = 1:10,
pt = anno_points(1:10))

如果設(shè)置height,則簡單注釋的大小不會改變,而只調(diào)整復(fù)雜注釋。如果有多個復(fù)雜的注解,則按照其原始大小的比例進(jìn)行調(diào)整。
# foo: 1cm, bar: 5mm, pt: 4.5cm
ha = HeatmapAnnotation(foo = cbind(1:10, 10:1),
bar = 1:10,
pt = anno_points(1:10),
height = unit(6, "cm"))

simple_anno_size控制所有簡單注釋的高度。ht_opt$simple_anno_size可以設(shè)置為全局控制所有熱圖中簡單注釋的大小。
# foo: 2cm, bar:1cm, pt: 3cm
ha = HeatmapAnnotation(foo = cbind(1:10, 10:1),
bar = 1:10,
pt = anno_points(1:10),
simple_anno_size = unit(1, "cm"), height = unit(6, "cm"))

如果annotation_height設(shè)置為絕對單位向量,則所有三個注釋的高度都會相應(yīng)調(diào)整。
# foo: 1cm, bar: 2cm, pt: 3cm
ha = HeatmapAnnotation(foo = cbind(1:10, 10:1),
bar = 1:10,
pt = anno_points(1:10),
annotation_height = unit(1:3, "cm"))

如果annotation_height設(shè)置為純數(shù)字作為注釋的相對比例,height也應(yīng)該設(shè)置為絕對單位,每個注釋的大小由比例調(diào)整。
# foo: 1cm, bar: 2cm, pt: 3cm
ha = HeatmapAnnotation(foo = cbind(1:10, 10:1),
bar = 1:10,
pt = anno_points(1:10),
annotation_height = 1:3, height = unit(6, "cm"))

annotation_height可以與相對單位(null 單位)和絕對單位混合使用。
# foo: 1.5cm, bar: 1.5cm, pt: 3cm
ha = HeatmapAnnotation(foo = cbind(1:10, 10:1),
bar = 1:10,
pt = anno_points(1:10),
annotation_height = unit(c(1, 1, 3), c("null", "null", "cm")), height = unit(6, "cm")
)

# foo: 2cm, bar: 1cm, pt: 3cm
ha = HeatmapAnnotation(foo = cbind(1:10, 10:1),
bar = 1:10,
pt = anno_points(1:10),
annotation_height = unit(c(2, 1, 3), c("cm", "null", "cm")), height = unit(6, "cm")
)

如果只有簡單的注釋,簡單的設(shè)置height不會改變高度。
# foo: 1cm, bar: 5mm
ha = HeatmapAnnotation(foo = cbind(1:10, 10:1),
bar = 1:10,
height = unit(6, "cm"))

除非simple_anno_size_adjust設(shè)置為TRUE。
# foo: 4cm, bar: 2cm
ha = HeatmapAnnotation(foo = cbind(1:10, 10:1),
bar = 1:10,
height = unit(6, "cm"),
simple_anno_size_adjust = TRUE)

調(diào)整注釋大小介紹了如何在熱圖列表中調(diào)整注釋大小。
3.18.3注釋標(biāo)簽
從2.3.3 版本開始,可以通過annotation_label參數(shù)設(shè)置注釋的替代標(biāo)簽:
ha = HeatmapAnnotation(foo = 1:10,
bar = cbind(1:10, 10:1),
pt = anno_points(1:10),
annotation_label = c("Annotation_foo", "Annotation_bar", "Annotation_pt")
)

也可以使用復(fù)雜的文本設(shè)置注釋標(biāo)簽:
ha = HeatmapAnnotation(foo = 1:10,
bar = cbind(1:10, 10:1),
pt = anno_points(1:10),
annotation_label = gt_render(
c("**Annotation**_<span style='color:red'>foo</span>",
"**Annotation**_<span style='color:blue'>bar</span>",
"**Annotation**_<span style='color:green'>pt</span>"),
gp = gpar(box_fill = "grey")
)
)

從 2.5.6 版本開始,可以配置注釋名稱的輪換。
ha = HeatmapAnnotation(foo = 1:10,
bar = cbind(1:10, 10:1),
pt = anno_points(1:10),
annotation_name_rot = 45
)
Heatmap(matrix(rnorm(100), 10), name = "mat", top_annotation = ha)

或在行上:
ha = rowAnnotation(foo = 1:10,
bar = cbind(1:10, 10:1),
pt = anno_points(1:10),
annotation_name_rot = 45
)
Heatmap(matrix(rnorm(100), 10), name = "mat", left_annotation = ha)

3.19實用功能
有一些實用函數(shù)可以使熱圖注釋的操作更容易。請參閱以下示例。
ha = HeatmapAnnotation(foo = 1:10,
bar = cbind(1:10, 10:1),
pt = anno_points(1:10))
length(ha)
## [1] 3
nobs(ha)
## [1] 10
獲取或設(shè)置注釋的名稱:
names(ha)
## [1] "foo" "bar" "pt"
names(ha) = c("FOO", "BAR", "PT")
names(ha)
## [1] "FOO" "BAR" "PT"
如果兩個HeatmapAnnotation對象包含相同數(shù)量的觀察名和不同的注釋名稱,則可以將它們連接起來。
ha1 = HeatmapAnnotation(foo = 1:10,
bar = cbind(1:10, 10:1),
pt = anno_points(1:10))
ha2 = HeatmapAnnotation(FOO = runif(10),
BAR = sample(c("a", "b"), 10, replace = TRUE),
PT = anno_points(rnorm(10)))
ha = c(ha1, ha2)
names(ha)
## [1] "foo" "bar" "pt" "FOO" "BAR" "PT"

HeatmapAnnotation對象有時是允許有子集的。行索引對應(yīng)于注釋中的觀察值,列索引對應(yīng)于注釋。如果注釋都是簡單注釋或者ComplexHeatmap包中的anno_*()函數(shù)創(chuàng)建的復(fù)雜注解,則HeatmapAnnotation對象始終是子集化的。
ha_subset = ha[1:5, c("foo", "PT")]
ha_subset
## A HeatmapAnnotation object with 2 annotations
## name: heatmap_annotation_109
## position: column
## items: 5
## width: 1npc
## height: 15.3514598035146mm
## this object is subsetable
## 7.57106666666667mm extension on the left
## 6.75733333333333mm extension on the right
##
## name annotation_type color_mapping height
## foo continuous vector random 5mm
## PT anno_points() 10mm

熱圖和注釋的構(gòu)建可以分開,以后可以通過attach_annotation()函數(shù)將注釋填充到熱圖對象中。
# code only for demonstration
ha1 = HeatmapAnnotation(foo = 1:10)
ha2 = rowAnnotation(bar = letters[1:10])
ht = Heatmap(mat)
ht = attach_annotation(ht, ha1, side = "top")
ht = attach_annotation(ht, ha2, side = "left")
3.20 實現(xiàn)新的注解功能
ComplexHeatmap中定義的所有注釋函數(shù)都是由AnnotationFunction該類構(gòu)造的。的AnnotationFunction類不僅存儲了“REAL R函數(shù)”,其繪制的圖形,它也計算產(chǎn)生的注釋軸的空間,更重要的是,它可以根據(jù)主熱圖的分割而分裂的注釋的圖形。
正如預(yù)估,AnnotationFunction類的主要部分是一個函數(shù),它定義了如何在與熱圖中的行或列相對應(yīng)的特定位置進(jìn)行繪制。該函數(shù)應(yīng)具有三個參數(shù):index,k 和n(參數(shù)的名稱可以是任意的)其中k和n是可選的。index對應(yīng)于熱圖的行或列的索引。index的值不一定是熱圖中的整個行索引或列索引。如果根據(jù)熱圖的拆分將注釋拆分為切片,則它也可以是索引的子集。 index根據(jù)熱圖行或列的重新排序(例如通過聚類重新排序)。所以,index 實際上包含行或列重新排序后當(dāng)前切片的行或列索引列表。
如前所述,注釋可以分成多個切片。k對應(yīng)當(dāng)前切片,n對應(yīng)切片總數(shù)。注釋函數(shù)在每個切片中重復(fù)繪制。k 和n有時是有用的信息,例如,我們要在注釋中添加軸,如果它是一個列注釋和軸畫在最右邊標(biāo)注的面積,軸只繪制時k == n。
由于該函數(shù)只允許index,k和n,該函數(shù)有時會使用多個無法在函數(shù)內(nèi)部定義的外部變量,例如注釋的數(shù)據(jù)點(diǎn)。這些變量應(yīng)該被導(dǎo)入到AnnotationFunction類中,var_import這樣函數(shù)才能正確地找到這些變量。
AnnotationFunction類的一個重要特性是它可以是可子集的,這是拆分的基礎(chǔ)。為了允許對象的子集化,用戶需要為導(dǎo)入的變量定義規(guī)則(如果有相應(yīng)的規(guī)則)。規(guī)則是簡單的函數(shù),它接受變量和索引,并返回變量的子集。在這個包中實現(xiàn)的子集規(guī)則函數(shù)是 subset_gp(),subset_matrix_by_row()和subset_vector()。如果未提供子集規(guī)則,則通過對象的類型來推斷。
我們首先構(gòu)造一個需要外部變量并支持子集化的AnnotationFunction對象。
x = 1:10
anno1 = AnnotationFunction(
fun = function(index, k, n) {
n = length(index)
pushViewport(viewport(xscale = c(0.5, n + 0.5), yscale = c(0, 10)))
grid.rect()
grid.points(1:n, x[index], default.units = "native")
if(k == 1) grid.yaxis()
popViewport()
},
var_import = list(x = x),
n = 10,
subsetable = TRUE,
height = unit(2, "cm")
)
anno1
## An AnnotationFunction object
## function: user-defined
## position: column
## items: 10
## width: 1npc
## height: 2cm
## imported variable: x
## this object is subsetable
然后我們可以在HeatmapAnnotation()函數(shù)中賦值anno1。由于anno1是子集化的,您可以拆分熱圖的列。
m = rbind(1:10, 11:20)
Heatmap(m, top_annotation = HeatmapAnnotation(foo = anno1))
Heatmap(m, top_annotation = HeatmapAnnotation(foo = anno1),
column_split = rep(c("A", "B"), each = 5))

第二種方式是將所有數(shù)據(jù)變量放在函數(shù)內(nèi)部,不需要導(dǎo)入其他變量。
# code only for demonstration
anno2 = AnnotationFunction(
fun = function(index) {
x = 1:10
n = length(index)
pushViewport(viewport())
grid.points(1:n, x[index])
popViewport()
},
n = 10,
subsetable = TRUE
)
僅向構(gòu)造函數(shù)指定函數(shù)的最緊湊方法。
# code only for demonstration
anno3 = AnnotationFunction(
fun = function(index) {
x = 1:10
n = length(index)
pushViewport(viewport())
grid.points(1:n, x[index])
popViewport()
}
)
anno_*()本節(jié)介紹的所有函數(shù)實際上都不是真正的注釋函數(shù),而是生成特定配置的注釋函數(shù)的函數(shù)。作為用戶就不需要知道。
anno_points(1:10)
## An AnnotationFunction object
## function: anno_points()
## position: column
## items: 10
## width: 1npc
## height: 1cm
## imported variable: data_scale, axis_param, border, size, value, pch_as_image, axis, gp, axis_grob, pch
## subsetable variable: gp, value, size, pch
## this object is subsetable
## 5.13831111111111mm extension on the left
在大多數(shù)情況下,您不需要手動構(gòu)建AnnotationFunction 對象。ComplexHeatmap 中anno_*()實現(xiàn)的標(biāo)注功能,對于大部分的分析任務(wù)來說已經(jīng)足夠了。另一方面,用戶還可以使用和快速添加自定義標(biāo)注圖形。例如,我們可以將之前的熱圖重新實現(xiàn)為:anno_empty() decorate_annotation()
ht = Heatmap(m, top_annotation = HeatmapAnnotation(foo = anno_empty(height = unit(2, "cm"))),
column_split = rep(c("A", "B"), each = 5))
ht = draw(ht)
co = column_order(ht)
decorate_annotation("foo", slice = 1, {
od = co[[1]]
pushViewport(viewport(xscale = c(0.5, length(od) + 0.5), yscale = range(x)))
grid.points(seq_along(od), x[od])
grid.yaxis()
popViewport()
})
decorate_annotation("foo", slice = 2, {
od = co[[2]]
pushViewport(viewport(xscale = c(0.5, length(od) + 0.5), yscale = range(x)))
grid.points(seq_along(od), x[od])
popViewport()
})

為了簡化 的使用AnnotationFunction(),從 2.9.3 版本開始,它有一個新參數(shù)cell_fun,該參數(shù)接受僅在單個“注釋單元格”中繪制的自定義函數(shù)。請參閱以下示例:
anno_pct = function(x) {
max_x = max(x)
text = paste0(sprintf("%.2f", x*100), "%")
cell_fun_pct = function(i) {
pushViewport(viewport(xscale = c(0, max_x)))
grid.roundrect(x = unit(1, "npc"), width = unit(x[i], "native"), height = unit(1, "npc") - unit(4, "pt"),
just = "right", gp = gpar(fill = "#0000FF80", col = NA))
grid.text(text[i], x = unit(1, "npc"), just = "right")
popViewport()
}
AnnotationFunction(
cell_fun = cell_fun_pct,
var_import = list(max_x, x, text),
which = "row",
width = max_text_width(text)*1.25
)
}
x = runif(10)
ha = rowAnnotation(foo = anno_pct(x), annotation_name_rot = 0)
m = matrix(rnorm(100), 10)
rownames(m) = x
ha + Heatmap(m)
