ComplexHeatmap 之 Legends 续(一)
点击上方关注我们
匆匆又夏天
2、离散型图例
离散图例用于离散颜色映射。通过仅提供颜色和中断值,连续颜色映射也可以转换为离散颜色映射。
可以指定 at 或 labels,但大部分指定标签。颜色应由 legend_gp
指定:
lgd = Legend(at = 1:6, title = "foo", legend_gp = gpar(fill = 1:6))
lgd = Legend(labels = month.name[1:6], title = "foo", legend_gp = gpar(fill = 1:6))
连续颜色映射的离散图例:
at = seq(0, 1, by = 0.2)
lgd = Legend(at = at, title = "foo", legend_gp = gpar(fill = col_fun(at)))
标题位置:
lgd = Legend(labels = month.name[1:6], title = "foo", legend_gp = gpar(fill = 1:6),
title_position = "lefttop")
lgd = Legend(labels = month.name[1:6], title = "foo", legend_gp = gpar(fill = 1:6),
title_position = "leftcenter-rot")
大小由 grid_width
和 grid_height
控制:
lgd = Legend(at = 1:6, legend_gp = gpar(fill = 1:6), title = "foo",
grid_height = unit(1, "cm"), grid_width = unit(5, "mm"))
标签的图形参数由 labels_gp
控制:
lgd = Legend(labels = month.name[1:6], legend_gp = gpar(fill = 1:6), title = "foo",
labels_gp = gpar(col = "red", fontsize = 14))
标题的图形参数由 title_gp
控制:
lgd = Legend(labels = month.name[1:6], legend_gp = gpar(fill = 1:6), title = "foo",
title_gp = gpar(col = "red", fontsize = 14))
标题和标签整合 gridtext
包的复杂文本:
lgd = Legend(
title = gt_render("<span style='color:orange'>**Legend title**</span>"),
title_gp = gpar(box_fill = "grey"),
at = c(-3, 0, 3),
labels = gt_render(c("**negative** three", "*zero*", "**positive** three")),
legend_gp = gpar(fill = 1:3)
)
网格的边界由 border
控制:
lgd = Legend(labels = month.name[1:6], legend_gp = gpar(fill = 1:6), title = "foo",
border = "red")
离散图例可以将网格排列成多行或/和多列。如果 ncol 设置为数字,则网格将排列为 ncol 列:
lgd = Legend(labels = month.name[1:10], legend_gp = gpar(fill = 1:10),
title = "foo", ncol = 3)
仍然根据多列图例计算标题位置:
lgd = Legend(labels = month.name[1:10], legend_gp = gpar(fill = 1:10), title = "foo",
ncol = 3, title_position = "topcenter")
通过设置 by_row = TRUE
来选择按行排列:
lgd = Legend(labels = month.name[1:10], legend_gp = gpar(fill = 1:10), title = "foo",
ncol = 3, by_row = TRUE)
两列之间的间隙由 gap
或 column_gap
控制。这两个参数的处理方式相同:
lgd = Legend(labels = month.name[1:10], legend_gp = gpar(fill = 1:10), title = "foo",
ncol = 3, gap = unit(1, "cm"))
行之间的间隙由 row_gap
控制:
lgd = Legend(labels = month.name[1:10], legend_gp = gpar(fill = 1:10), title = "foo",
ncol = 3, row_gap = unit(5, "mm"))
除了 ncol,还可以通过 nrow 指定布局。请注意,不能同时使用 ncol 和 nrow
:
lgd = Legend(labels = month.name[1:10], legend_gp = gpar(fill = 1:10),
title = "foo", nrow = 3)
一种极端情况是所有级别都 放在一排
并且标题旋转 90 度
。图例的高度将是旋转标题的高度:
lgd = Legend(labels = month.name[1:6], legend_gp = gpar(fill = 1:6), title = "foooooo",
nrow = 1, title_position = "lefttop-rot")
很多人可能会喜欢以下风格:
lgd = Legend(labels = month.name[1:6], legend_gp = gpar(fill = 1:6), title = "foooooo",
nrow = 1, title_position = "leftcenter")
Legend()
还支持使用简单的图形(例如点、线、箱线图)作为图例。type
参数可以指定为 points 或 p,可以使用数字表示 pch 或单字母表示 pch:
lgd = Legend(labels = month.name[1:6], title = "foo", type = "points",
pch = 1:6, legend_gp = gpar(col = 1:6), background = "#FF8080")
lgd = Legend(labels = month.name[1:6], title = "foo", type = "points",
pch = letters[1:6], legend_gp = gpar(col = 1:6), background = "white")
或者设置 type = "lines"/type = "l"
使用线条作为图例:
lgd = Legend(labels = month.name[1:6], title = "foo", type = "lines",
legend_gp = gpar(col = 1:6, lty = 1:6), grid_width = unit(1, "cm"))
或设置 type = "boxplot"/type = "box"
以使用箱线图作为图例:
lgd = Legend(labels = month.name[1:6], title = "foo", type = "boxplot",
legend_gp = gpar(fill = 1:6))
当 pch 为整数时,26:28 中的数字对应以下符号:
lgd = Legend(labels = paste0("pch = ", 26:28), type = "points", pch = 26:28)
在上面显示的所有示例中,标签都是单行。还支持多行标签
。如以下示例所示,多线标签的图例网格会自动延长:
lgd = Legend(labels = c("aaaaa\naaaaa", "bbbbb\nbbbbb", "c", "d"),
legend_gp = gpar(fill = 1:4))
如果图例排列成多行或多列,则图例网格的大小将调整为行数最多的标签:
lgd = Legend(labels = c("aaaaa\naaaaa", "c", "d", "bbbbb\nbbbbb"),
legend_gp = gpar(fill = 1:4), nrow = 2)
最后一个有用的参数图形可用于自定义图例图形
。图形的值应该是带有四个参数的函数列表:x 和 y
:图例网格的中心,w 和 h
:图例网格的宽度和高度。图形的长度应与 at 或标签相同。如果图形是名称与标签相对应的命名列表,则图形列表的顺序会自动调整:
lgd = Legend(labels = letters[1:4],
graphics = list(
function(x, y, w, h) grid.rect(x, y, w*0.33, h, gp = gpar(fill = "red")),
function(x, y, w, h) grid.rect(x, y, w, h*0.33, gp = gpar(fill = "blue")),
function(x, y, w, h) grid.text("A", x, y, gp = gpar(col = "darkgreen")),
function(x, y, w, h) grid.points(x, y, gp = gpar(col = "orange"), pch = 16)
))
未完待续。
收官!
代码 我上传到 QQ 群 老俊俊生信交流群
文件夹里。欢迎加入。加我微信我也拉你进 微信群聊 老俊俊生信交流群
哦。
群二维码:
老俊俊微信:
知识星球:
所以今天你学习了吗?
欢迎小伙伴留言评论!
今天的分享就到这里了,敬请期待下一篇!
最后欢迎大家分享转发,您的点赞是对我的鼓励和肯定!
如果觉得对您帮助很大,赏杯快乐水喝喝吧!
往期回顾
◀ComplexHeatmap 之 Heatmap List 续(二)
◀ComplexHeatmap 之 Heatmap List 续(一)
◀ComplexHeatmap 之 Heatmap List
◀ComplexHeatmap 之 Heatmap Annotations 续(三)
◀ComplexHeatmap 之 Heatmap Annotations 续(二)
◀ComplexHeatmap 之 Heatmap Annotations 续(一)
◀ComplexHeatmap 之 Heatmap Annotations
◀ComplexHeatmap 之 A Single Heatmap 续(二)
◀ComplexHeatmap 之 A Single Heatmap 续(一)