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

相关推荐
2201_7530548918 小时前
应用回归分析,R语言,多元线性回归总结(下)
回归·r语言·线性回归
LabEx4 天前
科研数据可视化核心技术:基于 AI 与 R 语言的热图、火山图及网络图绘制实践指南
人工智能·信息可视化·r语言·r语言绘图·乐备实·labex·科研数据绘图
Jet45054 天前
第100+43步 ChatGPT学习:R语言实现特征选择曲线图
学习·chatgpt·r语言
Chef_Chen4 天前
从0开始学习R语言--Day40--Kruskal-Wallis检验
开发语言·学习·r语言
quant_19864 天前
R语言如何接入实时行情接口
开发语言·经验分享·笔记·python·websocket·金融·r语言
小白学大数据5 天前
R语言爬虫实战:如何爬取分页链接并批量保存
开发语言·爬虫·信息可视化·r语言
开开心心_Every6 天前
便捷的Office批量转PDF工具
开发语言·人工智能·r语言·pdf·c#·音视频·symfony
Chef_Chen8 天前
从0开始学习R语言--Day39--Spearman 秩相关
开发语言·学习·r语言
q5673152310 天前
R语言初学者爬虫简单模板
开发语言·爬虫·r语言·iphone
shootero@126.com11 天前
R语言开发记录,二(创建R包)
r语言