2022/04/choropleth-map/ #55
Replies: 9 comments 1 reply
-
本文没有提及地区分布图的历史演变,也没有提及与其它地理可视化图形的对比,比如变形/示意地图 cartogram,比例符号地图 proportional symbols map 等。 |
Beta Was this translation helpful? Give feedback.
-
文中 Base R 是指 R 软件内置的一些 R 包的集合,这些 R 包都打上了 base 的标签,具体的 R 包有: Pkgs <- sapply(list.files(R.home("library")), function(x)
packageDescription(pkg = x, fields = "Priority"))
names(Pkgs[Pkgs == "base" & !is.na(Pkgs)])
#> [1] "base" "compiler" "datasets" "graphics" "grDevices" "grid"
#> [7] "methods" "parallel" "splines" "stats" "stats4" "tcltk"
#> [13] "tools" "utils" Created on 2022-05-23 by the reprex package (v2.0.1) |
Beta Was this translation helpful? Give feedback.
-
笔者凭借自己的画图经验和感觉,从以下方面比较: 学习曲线难度(易、一般、难)、 文档是否丰富(少、一般、多)、 代码简洁可读(差、一般、好)、 绘图渲染效率(低、一般、高)、灵活定制能力(弱、一般、强)、 代码可维护性(差、一般、好)、图形美观程度(差、一般、好)。
|
Beta Was this translation helpful? Give feedback.
-
本文在调用不同的 R 包绘制地区分布图时,提及各种数据类型,不同的 R 包都预先设定了绘图所需的数据类型,每种类型都有自己的数据结构,如 maps 包对应 map 类型,ggplot2 包对应 data.frame 类型,sp 包对应 spatial 类型,sf 包对应 simple feature 类型,这在对应 R 包的帮助文档中有详细介绍。目前,在空间数据的表示和处理方面,sf 包无疑是最好的。 |
Beta Was this translation helpful? Give feedback.
-
有一个更简单的获取 Base R 集合的方式: tools:::.get_standard_package_names()[["base"]] |
Beta Was this translation helpful? Give feedback.
-
# 读取数据
nc_income_race_county <- readRDS(file = "data/nc_income_race_county.rds")
nc_income_race_tract <- readRDS(file = "data/nc_income_race_tract.rds")
# 计算家庭年收入和白人占比数据
nc_income_race_tract <- within(nc_income_race_tract, {
pop <- B02001_001E
pctWhite <- B02001_002E / B02001_001E
medInc <- B19013_001E
})
library(sf)
# 加载一些必要的绘图包
library(ggplot2)
library(biscale)
library(cowplot)
# 去掉收入为 NA 的社区观测值
nc_income_race_tract_sub <- subset(x = nc_income_race_tract, subset = !is.na(medInc))
nc_income_race_tract_na <- subset(x = nc_income_race_tract, subset = is.na(medInc))
# 将数据根据分位点分箱
nc_bi_data <- bi_class(nc_income_race_tract_sub,
x = pctWhite, y = medInc,
style = "quantile", dim = 3
)
# 创建主体地图
nc_bi_map <- ggplot() +
geom_sf(data = nc_income_race_tract_na, color = "gray", fill = I("white")) +
geom_sf(
data = nc_bi_data, aes(fill = bi_class),
color = "gray80", size = 0.05, show.legend = FALSE
) +
bi_scale_fill(pal = "DkViolet2", dim = 3) +
labs(
title = "2015-2019 年度北卡罗来纳州各社区家庭年收入和白人占比的空间相关性",
caption = "数据源:美国人口调查局"
) +
theme_void() +
theme(plot.title = element_text(hjust = 0.5))
# 处理中文
showtext::showtext_auto()
# # 要求 biscale 版本 >= 1.1.0
# 创建图例
nc_bi_leg <- bi_legend(
pal = "DkViolet2",
dim = 3,
xlab = "白人占比",
ylab = "收入水平",
size = 7,
arrows = TRUE,
base_family = "wqy-microhei" # 中文字体
)
# 组合地图和图例
ggdraw() +
draw_plot(nc_bi_map, x = 0, y = 0, width = 1, height = 1) +
draw_plot(nc_bi_leg, x = 0.1, y = .1, width = 0.2, height = 0.2) 中文字体除了使用 showtext 包内置的文泉驿雅黑还可以引用系统安装的其它中文字体,比如 Noto Serif CJK SC ## 宋体
sysfonts::font_add(
family = "Noto Serif CJK SC",
regular = "NotoSerifCJKsc-Regular.otf",
bold = "NotoSerifCJKsc-Bold.otf"
) |
Beta Was this translation helpful? Give feedback.
-
收入与白人占比的关系,文中提供了线性趋势拟合,毕竟过于简化了,后续可以考虑二维核密度估计,非线性趋势拟合等手段。 |
Beta Was this translation helpful? Give feedback.
-
# 加载癌症死亡率数据
data(USCancerRates, package = "latticeExtra") # 3041 个郡县有数据
# 宽格式转长格式
us_cancer_rates <- reshape(
data = USCancerRates,
# 需要转行的列
varying = c(
"LCL95.male", "rate.male", "UCL95.male",
"LCL95.female", "rate.female", "UCL95.female"
),
times = c("男性", "女性"), # 构成新列 sex 的列值
v.names = c("LCL95", "rate", "UCL95"), # 列转行 列值构成的新列,指定名称
timevar = "sex", # 列转行 列名构成的新列,指定名称
idvar = c("state", "county"), # 可识别郡的 ID
new.row.names = 1:(2 * 3041),
direction = "long"
)
# 重命名 fips_codes 的 state 列为 state_abbr
fips_codes <- within(tidycensus::fips_codes, {
state_abbr <- state
})
fips_codes <- subset(
x = fips_codes,
select = setdiff(colnames(fips_codes), "state")
)
# 癌症死亡率数据关联州、郡代码 FIPS
us_cancer_rates <- merge(
x = us_cancer_rates,
y = fips_codes,
by.x = c("state", "county"),
by.y = c("state_name", "county"),
all.x = TRUE
)
library(sf)
# 加载地图数据
us_county_map <- readRDS("data/us_county_map.rds")
us_state_map <- readRDS("data/us_state_map.rds")
# 合并地图数据和观测数据
us_cancer_rates_map <- merge(
x = us_county_map, y = us_cancer_rates,
by.x = c("STATEFP", "COUNTYFP"),
by.y = c("state_code", "county_code"), all.x = TRUE
)
# 去掉完全缺失数据
us_cancer_rates_map <- subset(x = us_cancer_rates_map, subset = !is.na(sex))
# 死亡率数据分段
us_cancer_rates_map$rate_d <- cut(us_cancer_rates_map$rate, breaks = 50 * 0:13)
# 加载绘图 R 包
library(ggplot2)
library(ggspatial)
showtext::showtext_auto()
ggplot() +
geom_sf(data = us_county_map, fill = I("grey90"), colour = NA) +
geom_sf(data = us_cancer_rates_map, aes(fill = rate_d), size = 0.1) +
geom_sf(data = us_state_map, color = I("grey90"), fill = NA, size = 0.15) +
scale_fill_viridis_d(option = "plasma", na.value = "grey90") +
facet_wrap(~sex, ncol = 1) +
labs(
fill = "死亡率", title = "1999-2003 年美国各个郡的年平均癌症死亡率",
caption = "数据源:美国国家癌症研究所"
) +
theme_void(base_size = 13) +
theme(plot.title = element_text(hjust = 0.5)) +
annotation_scale(location = "bl", width_hint = 0.3) +
annotation_north_arrow(
location = "br",
which_north = "true",
style = north_arrow_fancy_orienteering()
) 效果图如下: |
Beta Was this translation helpful? Give feedback.
-
作者您好,我在使用tidycensus下载ACS数据时出现了以下错误:Error in curl::curl_fetch_memory(url, handle = handle) : |
Beta Was this translation helpful? Give feedback.
-
地区分布图及其应用 - Xiangyun Huang
https://xiangyun.rbind.io/2022/04/choropleth-map/
Beta Was this translation helpful? Give feedback.
All reactions