1
2
3
4
5
6
7
8
# install.packages("funkyheatmap")
library(funkyheatmap)
library(tidyverse)

data("mtcars")

vignette("dynbenchmark", "funkyheatmap")
#vignette("mtcars", "funkyheatmap")

image-20230613211126672

1、简单用法

对于文本列,直接显示相应文本;对于数值列,使用几何图形映射数值大小。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
data <- mtcars[10:20,1:5] %>%
  rownames_to_column("id") %>%
  arrange(desc(mpg))
data$symbol =sample(c("X","√",""),11,replace=T)
data$item =sample(c("word","ppt","excel"),11,replace=T)
#               id  mpg cyl disp hp drat symbol  item
# 1 Toyota Corolla 33.9   4 71.1 65 4.22      X excel
funky_heatmap(data)
## 设置列标签角度
# col_annot_offset = 3, 
# col_annot_angle = 30,
## 设置四周留白大小
# expand = c(xmin = 0, xmax = 2, ymin = 0, ymax = 0)
image-20230613205816873

2、自定义每列方案

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
data <- mtcars[10:20,1:5] %>%
  rownames_to_column("id") %>%
  arrange(desc(mpg))
data$symbol =sample(c("X","√",""),11,replace=T)
data$item =sample(c("word","ppt","excel"),11,replace=T)
#通过column_info设置每一列方案
column_info = data.frame(
	id = colnames(data),
	#设置每列的标签名
	name = paste0("feature-",seq(ncol(data))), 
	# #设置列的分组情况 
	# group = c(rep("group1",3),rep("group2",3),rep("group3",2)),
	#设置每列的可视化类型 "funkyrect", "circle", "rect", "bar", "pie", or "text"
	geom = c("text", "bar","funkyrect","rect","circle","circle","text","text"),
	#设置每列的颜色方案
	palette = c(NA, "palette1","palette1","palette2","palette3","palette2",NA,NA),
	#设置每列的宽度
	width = c(3,2,1,1,1,1,2,2)
	)
# 此外也可通过option的list列单独设置,支持所有参数
# 对于text列:size,hjust,vjust可分别设置字体大小,左右对齐,上下对齐
# 对于bar列:hjust可分别设置柱子的左右对齐
column_info$options = list(list(size=3,hjust=0,vjust=0),list(hjust=1),list(),list(),list(),list(),list(),list())

palettes = list(
	# 本质通过两个或者两个以上颜色,设置渐变色
	palette1 = RColorBrewer::brewer.pal(n = 9, name = "Blues"),
	palette2 = c("grey","red"),
	palette3 = c("red","red")
	)

funky_heatmap(data, column_info = column_info, palettes = palettes)
image-20230613210207464

*funkyrect是funkyheatmap(funkyheatmap::geom_rounded_rect())自定义的一种可视化方法:从小到大,从圆到方

3、设置列(特征)/行(样本)分组

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
data <- mtcars[10:20,1:5] %>%
  rownames_to_column("id") %>%
  arrange(desc(mpg))
data$symbol =sample(c("X","√",""),11,replace=T)
data$item =sample(c("word","ppt","excel"),11,replace=T)

#首先设置列的分组
column_info = data.frame(
	id = colnames(data),
	#设置列的分组情况 
	group = c(rep("group1",3),rep("group2",3),rep("group3",2))
	)
#设置每组的配色名
column_groups = data.frame(
	group = c("group1","group2","group3"),
	# level = c("major1","major2","major2"),
	palette = c("palette1","palette2","palette3")
	)
#设置具体配色方案
palettes = list(
	palette1 = c("red","red"),
	palette2 = c("green","green"),
	palette3 = c("brown","brown")
	)

# 对于行,仅需要设置列的分组即可
row_info = data.frame(
	id = data$id,
	group = c(rep("A",5),rep("B",3),rep("C",3))
	)


funky_heatmap(data, 
	column_info = column_info, column_groups = column_groups,
	palettes = palettes,
	row_info = row_info
	)
image-20230613210436698

4、设置饼图

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
data <- mtcars[10:20,1:5] %>%
  rownames_to_column("id") %>%
  arrange(desc(mpg))
data$pie_dat = list(
	c(a=1,b=2,c=3),
	c(a=1,b=2,c=3),
	c(a=1,b=2,c=3),
	c(a=1,b=2,c=3),
	c(a=1,b=2,c=3),
	c(a=1,b=2,c=3),
	c(a=1,b=2,c=1),
	c(a=1,b=2,c=1),
	c(a=1,b=2,c=1),
	c(a=1,b=2,c=1),
	c(a=1,b=2,c=1)
	)

column_info = data.frame(
	id = colnames(data),
	geom = c("text", "bar","funkyrect","rect","circle","circle","pie"),
	palette = c(NA,"palette0","palette0","palette0","palette0","palette0","palette1")
	)
palettes = list(
	palette1 = c(a="red",b="green",c="blue")
	)

funky_heatmap(data, 
	column_info=column_info,
	palettes = palettes)
image-20230613210541071

5、几何图形叠加文本

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
data <- mtcars[10:20,1:5] %>%
  rownames_to_column("id") %>%
  arrange(desc(mpg))

# 将该列转换为字符列,添加在原列后面
data = data %>%
	dplyr::mutate(disp_str=as.character(disp),.before=5) %>%
	dplyr::mutate(hp_str=as.character(hp),.before=7)

column_info = data.frame(
	id = colnames(data),
	geom = c("text", "bar","funkyrect","rect","text","circle","text","circle"),
	palette = c(NA,"palette0","palette0","palette0",NA,"palette1",NA,"palette1")
	)

column_info$options = list(list(),list(),list(),list(),
						   list(label="disp_str",overlay=T,scale=F),list(),
						   list(label="hp_str",overlay=T,scale=F),list())

funky_heatmap(data, 
	column_info=column_info)
image-20230613210657499