12.22 树状图
数据集 GNI2014 来自 treemap 包,是一个 data.frame 类型的数据对象,记录了 2014 年每个国家的人口总数 population 和国民人均收入 GNI,数据样例见下方:
library(treemap)
data(GNI2014, package = "treemap")
subset(GNI2014, subset = grepl(x = country, pattern = 'China'))
## iso3 country continent population GNI
## 7 MAC Macao SAR, China Asia 559846 76270
## 33 HKG Hong Kong SAR, China Asia 7061200 40320
## 87 CHN China Asia 1338612970 7400
数据呈现明显的层级结构,从大洲到国家记录人口数量和人均收入,矩阵树图以方块大小表示人口数量,以颜色深浅表示人均收入,见图12.63
treemap(GNI2014,
index = c("continent", "iso3"),
vSize = "population",
vColor = "GNI",
type = "value",
format.legend = list(scientific = FALSE, big.mark = " ")
)
treemapify 包基于 ggplot2 制作树状图,类似地,该 R 包内置了数据集 G20,记录了世界主要经济体 G20 (https://en.wikipedia.org/wiki/G20) 的经济和人口信息,国家 GDP (单位:百万美元)gdp_mil_usd
和人类发展指数 hdi
。相比于 GNI2014,它还包含了两列标签信息:经济发展阶段和所处的半球。图@(fig:treemap-ggplot2)以南北半球 hemisphere 分面,以色彩填充区域 region,以 gdp_mil_usd
表示区域大小
library(treemapify)
ggplot(G20, aes(
area = gdp_mil_usd, fill = region,
label = country, subgroup = region
+
)) geom_treemap() +
geom_treemap_text(grow = T, reflow = T, colour = "black") +
facet_wrap(~hemisphere) +
scale_fill_brewer(palette = "Set1") +
theme(legend.position = "bottom") +
labs(
title = "The G-20 major economies by hemisphere",
caption = "The area of each tile represents the country's GDP as a
proportion of all countries in that hemisphere",
fill = "Region"
)