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年的美元。深色区域表示成本较高的国家,特别是撒哈拉以南非洲和南亚地区,这些区域的平均成本显著高于其他地区。

相关推荐
asyxchenchong8885 天前
R语言混合效应(多水平/层次/嵌套)模型及贝叶斯实现技术应用
开发语言·r语言
临床数据科学和人工智能兴趣组7 天前
R语言中,列表是一种非常灵活的数据结构,它可以存储不同类型的对象,如向量、矩阵、数据框、甚至其他列表
数据结构·数据库·r语言·r语言-4.2.1·临床试验
临床数据科学和人工智能兴趣组8 天前
矩阵是一种二维数组,每个矩阵仅能包含一类数据(数值型、字符型或者逻辑型)
数据库·线性代数·矩阵·r语言·r语言-4.2.1·临床试验
临床数据科学和人工智能兴趣组9 天前
网络爬虫(Web Scraping)是一种用于自动提取网页内容的技术
r语言·r语言-4.2.1
生态学者10 天前
Ecological Indicators | 机场鸟调的新方法:用 AWM-PC1 读懂干扰梯度下的鸟类群落
人工智能·算法·r语言·微信公众平台
临床数据科学和人工智能兴趣组10 天前
运用rvest包进行数据爬虫
r语言·r语言-4.2.1
霸道流氓气质11 天前
基于本地 PaddleOCR 的 PDF 文字识别完整技术文档
开发语言·r语言·pdf
临床数据科学和人工智能兴趣组11 天前
SQL专为数据操作而设计,能够高效执行复杂的查询、筛选、排序、分组等操作
r语言·r语言-4.2.1
青春不败 177-3266-052012 天前
基于R、Python的Copula变量相关性分析及AI大模型应用
人工智能·python·r语言·贝叶斯·统计学·copula
统计学小王子13 天前
分类模型评价指标——R语言(数学建模常用)
数学建模·分类·r语言