其他
ggplot2轻松绘制令人惊艳的地图
❝本节来介绍如何使用「ggplot2」绘制「2020美国大选地图」,本文由「RGzxs」投稿,小编进行了简单排版,下面来看实际操作
❞
加载R包
my_packages <- c("tidyverse", "broom", "coefplot", "cowplot",
"gapminder", "GGally", "ggrepel", "ggridges", "gridExtra",
"here", "interplot", "margins", "maps", "mapproj",
"mapdata", "MASS", "quantreg", "rlang", "scales",
"survey", "srvyr", "viridis", "viridisLite", "devtools","ggplot2",
"ggthemes")
for (package in my_packages) {
if (!require(package,character.only=T, quietly=T)) {
install.packages(package)
library(package, character.only=T)
}
}
绘制美国各州地图
加载数据,选择几列并随机选择几行
load("election.rda")
election %>% select(state,total_vote,
r_points,pct_trump,party,census) %>%
sample_n(5)
party_colors <- c("#2E74C0", "#CB454A")
绘制基础地图
us_states <- map_data("state")
head(us_states)
p <- ggplot(data = us_states,
mapping = aes(x = long, y = lat,
group = group))
p + geom_polygon(fill = "white", color = "black")
以region为填充
p <- ggplot(data = us_states,
aes(x = long, y = lat,
group = group, fill = region))
p + geom_polygon(color = "gray90", size = 0.1) + guides(fill = FALSE)
将投影设置为Albers
p <- ggplot(data = us_states,
mapping = aes(x = long, y = lat,
group = group, fill = region))
p + geom_polygon(color = "gray90", size = 0.1) +
coord_map(projection = "albers", lat0 = 39, lat1 = 45) +
guides(fill = FALSE)
将2020年美国大选结果添加到地图上
election$region <- tolower(election$state)
us_states_elec <- left_join(us_states, election)
p <- ggplot(data = us_states_elec,
aes(x = long, y = lat,
group = group, fill = party))
p + geom_polygon(color = "gray90", size = 0.1) +
coord_map(projection = "albers", lat0 = 39, lat1 = 45)
添加题目,将图例移到下面,修改颜色
p0 <- ggplot(data = us_states_elec,
mapping = aes(x = long, y = lat,
group = group, fill = party))
p1 <- p0 + geom_polygon(color = "gray90", size = 0.1) +
coord_map(projection = "albers", lat0 = 39, lat1 = 45)
p2 <- p1 + scale_fill_manual(values = party_colors) +
labs(title = "Election Results 2016", fill = NULL)
p2 + theme_map()
映射连续变量-川普的选票
p0 <- ggplot(data = us_states_elec,
mapping = aes(x = long, y = lat,
group = group, fill = pct_trump))
p1 <- p0 + geom_polygon() +
coord_map(projection = "albers", lat0 = 39, lat1 = 45)
p2 <- p1+scale_fill_gradient(low = "white", high = "#CB454A")+
labs(title = "Trump vote")
p2 + theme_map() +labs(fill="Percent")
等值线图(ur-choropleths)
加载数据
load("county_data.rda")
load("county_map.rda")
county_map %>% sample_n(5)
county_data %>% select(
id,name,state,pop_dens,pct_black) %>%
sample_n(5)
合并
county_full <- left_join(county_map,county_data,by="id")
绘制每平方英里中的人口密度图
p <- ggplot(data = county_full,
mapping = aes(x = long, y = lat,
fill = pop_dens,
group = group))
p1 <- p + geom_polygon() + coord_equal()
p2 <- p1 + scale_fill_brewer(palette="Blues",
labels = c("0-10", "10-50", "50-100", "100-500",
"500-1,000", "1,000-5,000", ">5,000"))
p2 + labs(fill = "Population per\nsquare mile") +
theme_map() +
guides(fill = guide_legend(nrow = 1)) +
theme(legend.position = "bottom")
参考资料:Data Visualization 作者:Kieran Healy 链接:https://socviz.co/index.html#preface
❝好了今天的介绍到此结束,喜欢的小伙伴欢迎「付费99元」可加入我的「VIP交流群」,即同步获取公众号为期一年推文的代码与PDF文档,当然也可以对此文进行任意金额打赏,后台会发送给您数据&代码,感谢各位的支持
❞
欢迎大家扫描下方二位码加入「QQ交流群」,与全国各地上千位小伙伴交流
作者微信
「关注下方公众号下回更新不迷路」添加作者微信请备注单位-专业-姓名
2022-01-17
2022-01-14
2022-01-13
2022-01-11
2022-01-09
2022-01-08