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

  • 7
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值