绘图技巧 | 绘制带饼图的地图可视化技巧分享

2021-04-16 12:02:11 浏览数 (1)

这一期呢,小编回应读者的需求,绘制在地图上添加饼图的可视化作品,这类图表在GIS或生态方面涉及的比较多哈。本期内容主要如下:

  • R-scatterpie包主要函数介绍
  • R-scatterpie包实例演示

R-scatterpie包主要函数介绍

要想在地图上绘制饼图,使用常规的ggplot2较为繁琐,得益于丰富第三方包,我们可以使用scatterpie包轻松实现该需求。scatterpie包主要有:

  • geom_scatterpie()
  • geom_scatterpie_legend()

前者主要用于绘制饼图绘制,后者则对应绘制饼图大小的图例,具体可参考如下链接:https://cran.r-project.org/web/packages/scatterpie/vignettes/scatterpie.html。

接下来,小编将结合具体实际例子用于帮助小伙伴们理解scatterpie绘制函数,快速绘制带饼图的空间地图可视化作品。

R-scatterpie包实例演示

这一部分我们分别通过虚构数据绘制美国和中国的饼图地图作品,所使用的数据也会在文末给出免费获取方式。

  • 「以美国地图为例」数据处理代码如下(较为简单且都有注释):
代码语言:javascript复制
#导入包
library(readxl)
library(albersusa)
library(biscale)
library(sf)
library(tidyverse)
library(hrbrthemes)
library(ggtext)
library(scatterpie)
library("ggsci")

#读入虚构数据
data_test <- read_excel("usa_lon_lat.xlsx")
#转换投影
data_test_pro <- data_test %>% sf::st_as_sf(coords = c("lon", "lat"),crs = 4326)%>% st_transform(crs = 2163)
#将结果经纬度信息单独列出并准换成DF类型
data_test_pro  <-  data_test_pro %>% dplyr::mutate(lon = sf::st_coordinates(.)[,1],
                                lat = sf::st_coordinates(.)[,2])%>% select(values01,values02,values03,total,lon,lat)%>�ta.frame(stringsAsFactors = F) 

#可视化绘制
US_map <- ggplot() 
    geom_sf(data = us_sf,size=.125,aes(geometry = geometry))  
    scatterpie::geom_scatterpie(data=data_select,
                                aes(x = lon,y = lat,r=total*40),
                                cols=c("values01","values02","values03"),
                                alpha=.9
                                )  
    geom_scatterpie_legend(data_select$total*40, x=2068885.4, y=-2242789,n = 3,
                           labeller=function(x) x/40)  
    scale_fill_jco(name="Type")  

  labs(x="",y="",
    title = "Example of <span style='color:#D20F26'>scatterpie::geom_scatterpie function</span>",
    subtitle = "processed charts with <span style='color:#1A73E8'>usa map()</span>",
    caption = "Visualization by <span style='color:#DD6449'>DataCharm</span>")  
    theme_ipsum(base_family = "Roboto Condensed")  
    theme(
    plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
                                  size = 20, margin = margin(t = 1, b = 12)),
    plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
    plot.caption = element_markdown(face = 'bold',size = 12)) 

注意:geom_scatterpie_legend()中x,y的位置为转换后的数据哈。可视化结果如下:

Scatterpie Map Example of USA

  • 「以中国地图为例」同样的,我们还是虚构数据构建关于中国的类似地图的绘制,详细绘制代码如下:
代码语言:javascript复制
china_data <- read_excel("China_lon_lat.xlsx")
china_data_pro <- china_data %>% sf::st_as_sf(coords = c("lon", "lat"),crs = 4326)%>% st_transform(crs = 2343)%>% dplyr::mutate(lon = sf::st_coordinates(.)[,1],
                                lat = sf::st_coordinates(.)[,2])%>% select(data01,data02,data03,total,lon,lat) %>% 
               data.frame(stringsAsFactors = F) 
#读取中国地图文件信息
china_shp <- "中国省级地图GS(2019)1719号.geojson"
nine <- "九段线GS(2019)1719号.geojson"
china <- sf::read_sf(china_shp)
nine_line <- sf::read_sf(nine)
# 可视化绘制
china_map <- ggplot() 
     geom_sf(data = china,fill="gray90",size=.125,color="black")   
     geom_sf(data = nine_line,size=.125)   
     coord_sf(crs = 2343)  
    scatterpie::geom_scatterpie(data=china_data_select,
                                aes(x = lon,y = lat,r=total*500),
                                cols=c("data01","data02","data03"),
                                alpha=.9
                                )  
    geom_scatterpie_legend(china_data_select$total*500,x = 25000,y=1500000,n = 3,
                           labeller=function(x) x/500)  
    scale_fill_nejm(name="Type")  

  labs(x="",y="",
    title = "Example of <span style='color:#D20F26'>scatterpie::geom_scatterpie function</span>",
    subtitle = "processed charts with <span style='color:#1A73E8'>China map</span>",
    caption = "Visualization by <span style='color:#DD6449'>DataCharm</span>")  
    theme_ipsum(base_family = "Roboto Condensed")  
    theme(
    plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
                                  size = 20, margin = margin(t = 1, b = 12)),
    plot.subtitle = element_markdown(hjust = 0,vjust = 0,size=15),
    plot.caption = element_markdown(face = 'bold',size = 12)) 

可视化结果如下(小编使用了ggsci包的不同颜色主题哦):

Scatterpie Map Example of China

以上,就是本期推文的全部内容,较为简单且常用,希望小伙伴们能够喜欢,别忘了获取练习数据进行复现哦~

总结

本期算是解决一个读者的提问了哈,希望大家有所收获,数据免费获取方式如下哈~~

0 人点赞