赞
踩
首先,从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, .))
将处理后的数据与世界地图数据进行合并,以便在地图上进行可视化。
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") )
使用 ggplot2
和 scico
包进行地图绘制,采用自定义主题和配色方案,确保地图清晰、美观。
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"
根据处理后的数据生成初步地图,展示全球各国工作签证的平均成本。
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)
进一步美化地图,使其更具吸引力和可读性。
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年的美元。深色区域表示成本较高的国家,特别是撒哈拉以南非洲和南亚地区,这些区域的平均成本显著高于其他地区。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。