之前在交流群中看到有小伙伴有绘制世界地图的需求,本节继续来分享一个绘制世界地图的小例子。后台回复20210708获取本文代码,喜欢的小伙伴欢迎扫描文末二维码加入我的交流群,更多精彩内容为您而准备
加载R包
library(tidyverse)
library(forcats)
library(gapminder)
library(ggthemes)
library(wesanderson)
数据清洗
数据清洗部分涉及很多的函数具体可以参考tidyverse数据处理教程汇总这篇文档
holidays <- readr::read_csv('holidays.csv') %>%
select(-event_commemorated_and_notes) %>%
mutate(independence_from = case_when(independence_from ==
"Russian Soviet Federative Socialist Republic" ~ "Soviet Union",
independence_from =="Russian Soviet Federative Socialist Republic and German Empire" ~ "Soviet Union",
independence_from =="Spanish Empire" ~ "Spain",
independence_from =="United Kingdom of Great Britain" ~ "United Kingdom",
independence_from =="Kingdom of Great Britain" ~ "United Kingdom",
independence_from =="United Kingdom of Great Britain and Ireland" ~ "United Kingdom",
independence_from =="United Kingdom of Portugal, Brazil and the Algarves" ~ "Portugal",
independence_from =="United Kingdom and the British Mandate for Palestine" ~ "United Kingdom",
independence_from =="SFR Yugoslavia" ~ "Yugoslavia",
independence_from =="Socialist Federal Republic of Yugoslavia" ~"Yugoslavia",
independence_from =="Empire of Japan and France" ~ "Empire of Japan",
independence_from == "Spanish Empire[72]" ~ "Spain")) %>%
mutate(independence_from =
recode_factor(independence_from, "Soviet Union[80]" = "Soviet Union")) %>%
mutate(independence_from =
recode_factor(independence_from, "Soviet Union[55]" = "Soviet Union")) %>%
mutate(across(independence_from,fct_explicit_na, "other"))
round_to_decade = function(value){ return(round(value / 10) * 10) }
holidays <- holidays %>%
mutate(decade = round_to_decade(year))
mapdata <- map_data("world") %>%
filter(!(region=="Antarctica")) %>%
filter(!(region=="Greenland")) %>%
filter(!(region=="French Southern and Antarctic Lands")) %>%
mutate(region = recode(region,
USA="United States",
UK="United Kingdom"))
数据可视化
holidays %>%
mutate(independence_from = fct_lump(independence_from, n=7)) %>%
ggplot() +
geom_map(dat = mapdata, map = mapdata,
aes(map_id=region),fill="#E86F00", color=NA) +
geom_map(aes(map_id=country, fill=independence_from), map=mapdata) +
expand_limits(x = mapdata$long, y = mapdata$lat) +
coord_map(projection = "gilbert", xlim = c(-180, 180)) +
ggthemes::theme_map() +
theme(
text=element_text(family="Garamond"),
plot.title = element_text(size=22, hjust = 0.5),
plot.background = element_rect(fill = "white", color=NA),
legend.position = "top",
legend.justification = "center",
legend.background=element_blank(),
legend.key = element_rect(colour = NA, fill = NA),
legend.box.background = element_blank(),
legend.text=element_text(size=rel(2))) +
scale_fill_manual(values = wes_palette("Zissou1", 8, type = "continuous"),
na.value="#E86F00", na.translate=F) +
guides(fill=guide_legend(title="", ncol=3)) +
theme(plot.caption= element_text(size=16, hjust=1))

往期精彩:
配置R与Rstudio
ggplot2中的一些关键概念
tidyverse的基础使用
ggplot2绘制终极版热图
ggtree绘制进化树
初探相关性热图
再探相关性热图
ggplot2绘制相关性热图
初探柱状图
绘制具有显著性的条形图
ggplo2绘制经典条形图
ggplo2绘制双误差线条行图
揭开ggplot2中stat图层的神秘面纱
R中的图片注释神包aplot
ggplot2使用patchwork高质量拼图
ggplot2绘制基础散点图
ggplot2绘制经典散点图-1
ggplot2绘制经典散点图-2
ggplot2绘制云雨图
ggplot2绘制经典云雨图
ggplot2绘制哑铃图
ggplot2绘制经典哑铃图
ggplot2添加gif

