TMAP 上的多层

问题描述 投票:0回答:1

我有一个如下所示的数据框:

dataframe

Id 是邮政编码,列从 2015 年到 2019 年。

链接下载数据库(带有执行该程序所需的.shp文件)




library(dplyr)
library(sf) # pour gérer les fichiers shapefile
library(ggplot2)
library(tmap) # pour créer une carte 
library(leaflet)
library(stringi) # pour le problème d'encodage du fichier shapefile
library(maptools)
library(mapview) # permet une carte dynamique

base= read.csv("../bddlongsubstance.csv", stringsAsFactors = FALSE, encoding = "UTF-8", sep= ",", dec= ".")
base= data.frame(base)
base$code_postal_acheteur= as.factor(base$code_postal_acheteur)
names(base)[1]= "ID"


codes_postaux= st_read(dsn = "../codes_postaux_V5/codes_postaux_region.shp", 
                       layer = "codes_postaux_region",
                       quiet = TRUE) %>%
  select(ID, LIB, DEP)

codes_postaux$LIB= stri_encode(codes_postaux$LIB, from= "ISO-8859-1", to= "utf8")

fusion= codes_postaux %>% 
  left_join(base[1:17], by= "ID") %>%
  st_transform(2154)
summary(fusion)


tmap_mode(mode= "view")
autre_2015= 
  tm_basemap("CartoDB.Voyager")+ tm_shape(shp= fusion)+
  tm_fill(col= "Autre_quantite.totale.2015", palette= "YlOrBr", id= "ID",
          textNA = "Valeur manquante", style = "quantile", n= 6, title= "Quantité totale : <br> Catégorie AUTRE",
          popup.vars = c("Ville"= "LIB", "Quantité"= "Autre_quantite.totale.2015"))+
  tm_borders("black", lwd= 0.3, alpha= 0.6)+
  tm_layout(title = "Quantité de substances phytopharmaceutiques achetées en 2015 (en kilogrammes)", title.position = c("center", "bottom"), legend.bg.color = "white", legend.bg.alpha = 0.4)+
  tm_scale_bar(position = c("left", "bottom"))+
  tm_view(view.legend.position = c("right", "bottom"))

tmap_mode(mode= "view")
toxique_2015=
  tm_basemap("CartoDB.Voyager")+ tm_shape(shp= fusion)+
  tm_fill(col= "Toxique_quantite.totale.2015", palette= "YlOrBr", id= "ID", 
          textNA = "Valeur manquante", style = "quantile", n= 6, title= "Quantité totale : <br> Catégorie TOXIQUE",
          popup.vars = c("Ville"= "LIB", "Quantité"= "Toxique_quantite.totale.2015"))+
  tm_borders("black", lwd= 0.3, alpha= 0.6)+
  tm_layout(title = "Quantité de substances phytopharmaceutiques achetées en 2015 (en kilogrammes)", title.position = c("center", "bottom"), legend.bg.color = "white", legend.bg.alpha = 0.4)+
  tm_scale_bar(position = c("left", "bottom"))+
  tm_view(view.legend.position = c("right", "bottom"))


使用这段代码,我只能同时生成一张如下所示的地图:

picture of the map

但理想情况下,我想要一个具有多层的系统,并且我可以从数据框中选择我想要显示的任何列,但我真的找不到如何操作。

谢谢

r leaflet layer choropleth tmap
1个回答
0
投票

这是一种替代解决方案,允许您使用

tmap
leaflet
以编程方式创建交互式分区统计图,并具有任意数量的图层。

我无法通过提供的链接下载数据,因此我使用 spData 包中的示例数据集。

library(tmap)
library(leaflet)
library(sf)
library(tidyverse)
library(spData)

#Set to interactive mode:
tmap_mode(mode = "view")

#Get data from spData package:
nz <- nz

#Create a function that allows you to make as many layers as you want:
#' create_tm_;ayer
#'
#' @param data - an sf dataframe
#' @param column - a string that is a column name in data
#'
#' @return - a tmap map

create_tm_layer <- function(data, column) {
  tm_shape(data,
           name = column) +
    tm_fill(col = column)
  
}

#Iterate over function with as many column names as you want
tmap_mode(mode="view")

columns <- c("Median_income", "Sex_ratio", "Population")

# This configures Population to be the variable that appears at first
variables_to_start_hidden <- columns[columns != "Population"]

results <- map(create_tm_layer, data = nz, .x = columns)

tmap_results <- Reduce(`+`, results)

tmap_leaflet_results <- tmap_leaflet(tmap_results) %>%
  hideGroup(variables_to_start_hidden)

© www.soinside.com 2019 - 2024. All rights reserved.