
30. TreeMap树图绘制
清除当前环境中的变量
rm(list=ls())
复制
设置工作目录
setwd("C:/Users/Dell/Desktop/R_Plots/30treemap/")
复制
使用treemap包绘制矩形树状图
# 安装并加载所需的R包
#install.packages("treemap")
library(treemap)
# 加载并查看示例数据
data(GNI2014)
head(GNI2014)
## iso3 country continent population GNI
## 3 BMU Bermuda North America 67837 106140
## 4 NOR Norway Europe 4676305 103630
## 5 QAT Qatar Asia 833285 92200
## 6 CHE Switzerland Europe 7604467 88120
## 7 MAC Macao SAR, China Asia 559846 76270
## 8 LUX Luxembourg Europe 491775 75990
# 使用treemap函数绘制矩形树状图
treemap(GNI2014,
index="continent", #指定分组的列
vSize="population", #指定面积大小的列
vColor="GNI", #指定颜色深浅的列
type="value", #指定颜色填充数据的类型
format.legend = list(scientific = FALSE, big.mark = " "))复制

# colors indicate density (like a population density map)
treemap(GNI2014,
index=c("continent","country"), #指定多个分组的列,先按continent分组,再按country分组
vSize="population", #指定面积大小的列
vColor="GNI", #指定颜色深浅的列
type="dens")复制

# manual set the color palettes
treemap(GNI2014,
index=c("continent","country"), #指定多个分组的列
vSize="population", #指定面积大小的列
vColor="GNI", #指定颜色深浅的列
type="manual", #自定义颜色类型
palette = terrain.colors(10))复制

treemap(GNI2014,
index=c("continent","country"), #指定多个分组的列
vSize="population", #指定面积大小的列
vColor="GNI", #指定颜色深浅的列
type = "value",
palette = "RdYlBu", #自定义颜色画板
#range = c(100,10000), #设置颜色的范围值
fontsize.labels=c(12, 10), #设置标签字体大小
align.labels=list(c("center", "center"), c("left", "top")), #设置标签对齐的方式
border.col=c("black","red"), #设置边框的颜色
border.lwds=c(4,2), #设置边框的线条的宽度
title = "My TreeMap")复制

使用treemapify包绘制矩形树状图
# 安装并加载所需的R包
#install.packages("treemapify")
library(treemapify)
library(ggplot2)
# 查看内置示例数据
head(G20)
## region country gdp_mil_usd hdi econ_classification
## 1 Africa South Africa 384315 0.629 Developing
## 2 North America United States 15684750 0.937 Advanced
## 3 North America Canada 1819081 0.911 Advanced
## 4 North America Mexico 1177116 0.775 Developing
## 5 South America Brazil 2395968 0.730 Developing
## 6 South America Argentina 474954 0.811 Developing
## hemisphere
## 1 Southern
## 2 Northern
## 3 Northern
## 4 Northern
## 5 Southern
## 6 Southern
# 使用geom_treemap函数绘制矩形树状图
ggplot(G20, aes(area = gdp_mil_usd, fill = hdi)) +
geom_treemap()复制

# 添加label标签,设置字体大小和类型
ggplot(G20, aes(area = gdp_mil_usd, fill = hdi, label = country)) +
geom_treemap() +
geom_treemap_text(fontface = "italic", colour = "white",
size = 16, place = "centre")复制

# 添加多个分组信息
ggplot(G20, aes(area = gdp_mil_usd, fill = hdi,
label = country,
subgroup = region)) +
geom_treemap() +
geom_treemap_subgroup_border() +
geom_treemap_subgroup_text(place = "centre", alpha = 0.5,
colour = "black", fontface = "italic") +
geom_treemap_text(colour = "white", place = "topleft", reflow = T) +
scale_fill_gradientn(colours = c("blue","white","tomato"))复制

sessionInfo()
## R version 3.6.0 (2019-04-26)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18363)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=Chinese (Simplified)_China.936
## [2] LC_CTYPE=Chinese (Simplified)_China.936
## [3] LC_MONETARY=Chinese (Simplified)_China.936
## [4] LC_NUMERIC=C
## [5] LC_TIME=Chinese (Simplified)_China.936
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] ggplot2_3.2.0 treemapify_2.5.3 treemap_2.4-2
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.5 compiler_3.6.0 pillar_1.4.2
## [4] later_0.8.0 RColorBrewer_1.1-2 tools_3.6.0
## [7] ggfittext_0.9.0 digest_0.6.20 lifecycle_0.2.0
## [10] evaluate_0.14 tibble_2.1.3 gtable_0.3.0
## [13] gridBase_0.4-7 pkgconfig_2.0.2 rlang_0.4.7
## [16] igraph_1.2.4.1 shiny_1.3.2 yaml_2.2.0
## [19] xfun_0.8 withr_2.1.2 stringr_1.4.0
## [22] dplyr_1.0.2 knitr_1.23 generics_0.0.2
## [25] vctrs_0.3.2 tidyselect_1.1.0 grid_3.6.0
## [28] glue_1.4.2 data.table_1.12.2 R6_2.4.0
## [31] rmarkdown_1.13 purrr_0.3.2 magrittr_1.5
## [34] scales_1.0.0 promises_1.0.1 htmltools_0.3.6
## [37] mime_0.7 colorspace_1.4-1 xtable_1.8-4
## [40] httpuv_1.5.1 labeling_0.3 stringi_1.4.3
## [43] lazyeval_0.2.2 munsell_0.5.0 crayon_1.3.4复制
END





文章转载自bioinfomics,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
数据库国产化替代深化:DBA的机遇与挑战
代晓磊
1258次阅读
2025-04-27 16:53:22
2025年4月国产数据库中标情况一览:4个千万元级项目,GaussDB与OceanBase大放异彩!
通讯员
731次阅读
2025-04-30 15:24:06
国产数据库需要扩大场景覆盖面才能在竞争中更有优势
白鳝的洞穴
599次阅读
2025-04-14 09:40:20
【活动】分享你的压箱底干货文档,三篇解锁进阶奖励!
墨天轮编辑部
510次阅读
2025-04-17 17:02:24
一页概览:Oracle GoldenGate
甲骨文云技术
478次阅读
2025-04-30 12:17:56
GoldenDB数据库v7.2焕新发布,助力全行业数据库平滑替代
GoldenDB分布式数据库
471次阅读
2025-04-30 12:17:50
优炫数据库成功入围新疆维吾尔自治区行政事业单位数据库2025年框架协议采购!
优炫软件
362次阅读
2025-04-18 10:01:22
给准备学习国产数据库的朋友几点建议
白鳝的洞穴
304次阅读
2025-05-07 10:06:14
XCOPS广州站:从开源自研之争到AI驱动的下一代数据库架构探索
韩锋频道
285次阅读
2025-04-29 10:35:54
国产数据库图谱又上新|82篇精选内容全览达梦数据库
墨天轮编辑部
275次阅读
2025-04-23 12:04:21