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 = " "))
image.png
# colors indicate density (like a population density map)
treemap(GNI2014,
index=c("continent","country"), #指定多个分组的列,先按continent分组,再按country分组
vSize="population", #指定面积大小的列
vColor="GNI", #指定颜色深浅的列
type="dens")
image.png
# manual set the color palettes
treemap(GNI2014,
index=c("continent","country"), #指定多个分组的列
vSize="population", #指定面积大小的列
vColor="GNI", #指定颜色深浅的列
type="manual", #自定义颜色类型
palette = terrain.colors(10))
image.png
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")
image.png
使用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()
image.png
# 添加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")
image.png
# 添加多个分组信息
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"))
image.png
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
网友评论