treemap

安装

1
install.packages("treemap")

读取数据

1
contig_length <- read.table("genome_length.txt", header = FALSE, sep = '\t')

画图

1
treemap(contig_length, index = "V1", vSize = "V2", palette = "Reds", title = "Contig length of xxx genome")

指定每格的颜色与边框宽度

1
2
3
4
#添加一列作为颜色列,为每一行指定颜色
OS_contig_length$V3 <- rep("#ffffff", 23)
#指定type为color,不显示index
treemap(OS_contig_length, index = "V1", vSize = "V2", vColor = "V3", type = "color", border.lwds = 1, title = "Contig length of XXX genome", fontsize.labels = 0)

treemapify

使用treemap绘图产生的图形不是ggplot对象,而trrmapify则解决了这一问题。

安装

1
install.packages("treemapify")

使用

1
2
3
4
5
6
7
library(ggplot2)
library(treemapify)
#使用treemapify自带的示例数据G20
ggplot(data = G20, 
       aes(area = gdp_mil_usd)  # 输入的映射中,只有area为必须+
       geom_treemap()

img

调整

默认的输出为黑色背景,这一点可以手动赋值给fill映射修改

1
2
ggplot(G20, aes(area = gdp_mil_usd)) + 
 geom_treemap(fill="white")

还可以向fill映射另一个变量

1
2
3
ggplot(G20, aes(area = gdp_mil_usd, fill = hdi)) + 
 geom_treemap() +
 scale_fill_distiller(palette="Greens") #使用内置的配色方案

使用geom_treemap_text添加标签,不支出ggplot通用的geom_text()函数

1
2
3
4
ggplot(G20, aes(area = gdp_mil_usd, fill = hdi, label = country)) +
 geom_treemap() +
 geom_treemap_text(fontface = "italic", colour = "red", place = "centre",grow = TRUE,alpha=.6)+
 scale_fill_distiller(palette="Greens")

参考来源

https://www.r-bloggers.com/simple-steps-to-create-treemap-in-r/

https://rdrr.io/cran/treemap/man/treemap.html

https://rpubs.com/brandonkopp/creating-a-treemap-in-r

https://cran.r-project.org/web/packages/treemap/treemap.pdf

https://github.com/wilkox/treemapify

https://zhuanlan.zhihu.com/p/29834340