首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >R 绘制交互式地图 Mapview

R 绘制交互式地图 Mapview

作者头像
Jamesjin63
发布2022-10-25 14:56:16
发布2022-10-25 14:56:16
1.4K0
举报
文章被收录于专栏:EpiHubEpiHub

R 绘制交互式地图 Mapview

leaflet可以实现交互式地图,这里直接一中国为例,展示不同省份的population以及mapview上的实现。 leaflet基础篇可以去官网

该文章内容的地图图层文件,均是sf形式。leaflet可以直接加载sf,省去转换Polygons的麻烦。

1.leaflet

1.1 加载China地图
代码语言:javascript
复制
library(ggplot2)
library(dplyr)
library(tibble)
library(sf)
library(leaflet)
library(leaflet.extras)
rm(list = ls())
China=read_sf("https://geo.datav.aliyun.com/areas_v2/bound/100000_full.json")
Anhui=read_sf("https://geo.datav.aliyun.com/areas_v2/bound/340000_full.json")
franconia
breweries


CHNmap=leaflet() %>% 
  addProviderTiles("CartoDB.Positron") %>% 
  addPolygons( data=China,   stroke = TRUE,
               color = "#444444",
               weight = 1,
               smoothFactor = 0.5,
               opacity = 1.0,
               fillOpacity = 0.1,
                  label = ~ name,
               highlightOptions = highlightOptions(color = "white",
                                                   weight = 2,
                                                   bringToFront = TRUE),
               labelOptions = labelOptions(
                 style = list("font-weight" = "normal", padding = "3px 8px"),
                 textsize = "15px",
                 direction = "auto"))
CHNmap

image.png

这里直接加载到leaflet图层上,可以看到China的轮廓及各个省份的位置

1.2 添加安徽地图

在上述的图层中,再添加安徽内部的市及区的地图。

代码语言:javascript
复制
CHNmap %>% 
  addPolygons( data=Anhui,   stroke = T,
               color = "blue",
               weight = 1,
               opacity = 1,
               fillOpacity = 0.1)

image.png

1.3.根据各个省含有的市及区多少,添加颜色

有时候,需要根据不同省份的人口或者经济,进行不同颜色渲染,突出地区间的比较。现在以各个省份内部所包含的市及县数量,来进行一个等级划分。其中重点是将连续性变量转成分类变量,还要匹配上对应颜

# set categories and color bins bins = c(0, 5, 10, 15, 20, 30, Inf) pal = colorBin("Reds", domain = China$childrenNum, bins = bins)

代码语言:javascript
复制
##  set categories and color bins
bins = c(0, 5, 10, 15, 20, 30, Inf)
pal = colorBin("Reds", domain = China$childrenNum, bins = bins)

## leaflet
map=leaflet() %>% 
  addProviderTiles("CartoDB.Positron") %>% 
  addPolygons( data=China,   stroke = TRUE,
               fillColor = ~pal(childrenNum),
               color = "black",
               weight = 1,
               smoothFactor = 0.5,
               opacity = 0.5,
               fillOpacity = 0.5,
               label = ~ name,
               highlightOptions = highlightOptions(color = "green",
                                                   weight = 2,
                                                   bringToFront = TRUE),
               labelOptions = labelOptions(
                 style = list("font-weight" = "normal", padding = "3px 8px"),
                 textsize = "15px",
                 direction = "auto")) %>% 
  addLegend(data=China, pal = pal,
            values = ~childrenNum,
            opacity = 0.7,
            title = "Number",
            position = "bottomright") 
            
# plot
map

image.png

1.4.添加点的信息
代码语言:javascript
复制
China_point = st_centroid(China) %>% 
  st_coordinates() %>% 
  as_data_frame() %>% 
  slice(-35)

map %>% 
  addCircles(data = China_point,
                  lng = ~X, lat = ~Y,
                  fillOpacity =2)

image.png

2.Mapview 绘图

其实谈到交互地图,mapview包已经做到了精简,详细教程见官网, 这里只需要一行code即可; 但是缺点是,不容易个性化设置,譬如legend名称,legend设置等。 主要是简单,快速。 但是官方文档,里面有更详细的操作步骤。

代码语言:javascript
复制
## 
mapview::mapview(China)
mapview::mapview(China,zcol = "childrenNum")
mapview(China,  col.regions = "white", lwd = 0.5,legend = F)

image.png

image.png

后续还会更新,包括怎样将leaflet与mapview结合到shiny中。 及legend细化。

参考

  1. Drawing interactive maps with Leaflet
  2. Buffer areas for nearest neighbours or national marine areas delineation
  3. Interactive Choropleths with Shiny and Leaflet
  4. Lesson 2 Interactive Maps
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-10-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • R 绘制交互式地图 Mapview
    • 1.leaflet
      • 1.1 加载China地图
      • 1.2 添加安徽地图
      • 1.3.根据各个省含有的市及区多少,添加颜色
      • 1.4.添加点的信息
    • 2.Mapview 绘图
    • 参考
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档