R语言ggplot2包绘制世界地图

数据和代码获取:请查看主页个人信息!!!

1. 数据读取与处理

首先,从CSV文件中读取数据,并计算各国每日收入的平均签证成本。

复制代码
library(tidyverse)
​
df <- read_csv("df.csv") %>% 
  group_by(source, source_iso3) %>% 
  summarise(avg_work=mean(work_perdailyincome, na.rm=TRUE)) %>% 
  mutate_all(~ifelse(is.nan(.), NA, .))

2. 数据合并

将处理后的数据与世界地图数据进行合并,以便在地图上进行可视化。

复制代码
library(rnaturalearth)
library(sf)
​
world <- ne_countries(scale = "medium", returnclass = "sf") %>% 
  select(iso_a3,geometry)
​
df2 <- left_join(
  world,
  df,
  by=c("iso_a3"="source_iso3")
)

3. 可视化设置

使用 ggplot2scico 包进行地图绘制,采用自定义主题和配色方案,确保地图清晰、美观。

复制代码
library(showtext)
library(scico)
library(rnaturalearthdata)
​
showtext_auto(enable = TRUE)
font <- "Fira Sans Condensed"
font_add_google(family=font, font)
theme_set(theme_minimal(base_family = font))
bg <- "#1F1D36"
txt_col <- "grey95"

4. 绘制初步地图

根据处理后的数据生成初步地图,展示全球各国工作签证的平均成本。

复制代码
df2 %>% 
  ggplot() +
  geom_sf(aes(fill=avg_work, geometry=geometry), color="grey20", size=.1) +
  scale_fill_scico(palette = "imola",
                   direction = 1,
                   na.value="grey95",
                   limits=c(0,180),
                   breaks=seq(0,180,45),
                   begin=.5,
                   end=1)
​
ggsave("pic1.png", width = 7, height = 4) 

5. 美化地图

进一步美化地图,使其更具吸引力和可读性。

复制代码
df2 %>% 
  ggplot() +
  geom_sf(aes(fill=avg_work, geometry=geometry), color="grey20", size=.1) +
  scale_fill_scico(palette = "imola",
                   direction = 1,
                   na.value="grey95",
                   limits=c(0,180),
                   breaks=seq(0,180,45),
                   begin=.5,
                   end=1) +
  coord_sf(crs = "+proj=merc", ylim = c(-7000000,11000000)) +
  theme(
    panel.grid = element_blank(),
    axis.title = element_blank(),
    axis.text = element_blank(),
    
    plot.margin = margin(30,30,30,30),
    plot.background = element_rect(color=bg, fill=bg),
    
    plot.title = element_text(hjust=0,size=18, color=txt_col,lineheight=.8, face="bold", margin=margin(0,0,0,0)),
    plot.subtitle = element_text(hjust=0,size=12, color=txt_col,lineheight=.8, margin=margin(10,0,20,0)),
    plot.caption = element_text(hjust=.5,margin=margin(10,0,0,0), size=8, color=txt_col, face="bold"),
    legend.position = "bottom",
    legend.title = element_text(size=7, color=txt_col),
    legend.text = element_text(size=6, color=txt_col)
  )  +
  guides(fill = guide_colourbar(ticks.colour = NA,
                                title.position="top",
                                title.hjust = 0.5,
                                barwidth = unit(6, "cm"),
                                barheight = unit(.4,"cm"),))
​
ggsave("pic2.png", width = 7, height = 4) 

可视化结果

这张地图展示了全球各国申请工作签证的平均成本,单位为2019年的美元。深色区域表示成本较高的国家,特别是撒哈拉以南非洲和南亚地区,这些区域的平均成本显著高于其他地区。

相关推荐
qunshankeji21 小时前
战场目标检测:Faster R-CNN与RegNetX-800MF融合实现建筑物人员坦克车辆识别_2
目标检测·r语言·cnn
Tiger Z2 天前
R 语言科研绘图第 83 期 --- 3D折线图-渐变
r语言·论文·科研·绘图·研究生
权泽谦2 天前
R Shiny 交互式网页实战:从零到上线可视化应用
开发语言·信息可视化·r语言
高-老师3 天前
基于OpenLCA、GREET、R语言的生命周期评价方法、模型构建及典型案例应用
r语言·生命周期评价
生信小窝4 天前
基于R获取全球海岸线数据获取与导出
开发语言·r语言
图灵信徒6 天前
R语言绘图与可视化第六章总结
python·数据挖掘·数据分析·r语言
Tiger Z8 天前
《R for Data Science (2e)》免费中文翻译 (第12章) --- Logical vectors(1)
数据分析·r语言·数据科学·免费书籍
AI纪元故事会8 天前
《目标检测全解析:从R-CNN到DETR,六大经典模型深度对比与实战指南》
人工智能·yolo·目标检测·r语言·cnn
小八四爱吃甜食10 天前
【R语言】构建GO、KEGG相关不同物种的R包
开发语言·golang·r语言
梦想的初衷~10 天前
生命周期评价(LCA):理论、方法与工具、典型案例全解析
r语言·农业·林业·环境科学·地理·气候变化·生命周期评价