美文网首页绘图技巧
蓝色与地图更配-ggplot2地图进阶

蓝色与地图更配-ggplot2地图进阶

作者: R语言数据分析指南 | 来源:发表于2021-08-27 21:15 被阅读0次

    最近有小伙伴让在出几篇关于地图绘制的教程,观众老爷的需求这个当然要满足;今天继续来介绍ggplot2绘制地图,同样的操作不同的感觉,下面将详细介绍如何绘制下图

    1.安装加载R包

    package.list=c("tidyverse","cowplot","ggspatial","ggsci")
    
    for (package in package.list) {
      if (!require(package,character.only=T, quietly=T)) {
        install.packages(package)
        library(package, character.only=T)
      }
    }
    

    2.构建世界地图数据集

    world_map <- map_data("world") %>% dplyr::rename(country=region) %>%
      mutate(country=str_replace(country,"Taiwan","China")) %>% 
      filter(country != "Antarctica") # 剔除南极洲数据
    

    3.构建需要展示的国家数据

    map <- c("Australia","China","Denmark","Finland","South Africa",
             "France","Germany","Russia","Egypt",
             "Hungary","Japan","Brazil","UK","USA","Canada") %>% 
      as.data.frame() %>% dplyr::rename(country=".") %>% 
      mutate(country=str_replace(country,"Korea","South Korea")) %>% 
      mutate(cou=country)
    

    4.绘制世界地图

    p1 <- world_map %>% left_join(.,map,by="country") %>% 
      ggplot()+
      geom_polygon(aes(x = long, y = lat,group = group,fill = cou),
                   color = "black",size = 0.2,show.legend = F)+
      scale_fill_simpsons(na.value="gray70")+  # 将无需显示的国家设置为灰色
      theme_void()+
      theme(plot.background = element_rect(fill="#3ED5F7",
      color="#3ED5F7",size = 1))+
      annotation_north_arrow(location = "bl",pad_x = unit(0.2,"in"),  
                             pad_y = unit(6,"in"),
                             style = north_arrow_nautical(fill = c("grey40","white"),
                             line_col = "grey20")) # 添加指北针
    p1
    

    5.构建欧洲主要国家数据集

    data <- world_map %>% 
      filter(.,country %in% c("Austria","Belgium","Bulgaria","Croatia","Cyprus",
                              "Czech Rep.","Denmark","Estonia","Finland","France",
                              "Germany","Greece","Hungary","Ireland","Italy","Latvia",
                              "Lithuania","Luxembourg","Malta","Netherlands","Poland",
                              "Portugal","Romania","Slovakia","Slovenia","Spain",
                              "Sweden","UK"))
    

    6.构建国家标签

    label <- data %>% 
      filter(.,country %in% c("Denmark","Estonia","Finland","France",
                              "Germany","Sweden","UK")) %>% 
      group_by(country) %>%
      summarise(long = mean(long), lat = mean(lat))
    

    7.绘制欧洲地图

    p2 <- data %>% ggplot()+
      geom_polygon(aes(x = long, y = lat,group = group,fill = country),
                   color = "black",size = 0.2,show.legend = F)+
      geom_text(data =label,aes(long,lat,label=country),size =3, hjust = 0.5)+ # 添加国家名称
      scale_fill_manual(values= colorRampPalette(brewer.pal(8,"Set2"))(27))+
      theme_void()+
      theme(plot.background = element_rect(fill="#3ED5F7",color="black",size=0.8))
    p2
    

    8.拼图

    ggdraw(p1) + draw_plot(p2,x=-0.355,y=-0.35,scale=.315)  
    

    喜欢的小伙伴欢迎关注我的公众号

    R语言数据分析指南,持续分享数据可视化的经典案例及一些生信知识,希望对大家有所帮助

    相关文章

      网友评论

        本文标题:蓝色与地图更配-ggplot2地图进阶

        本文链接:https://www.haomeiwen.com/subject/ttnmiltx.html