Code
library(tidyverse)
library(sf)
library(leaflet)
library(geojsonio)
library(leaflet.extras)
with leaflet
Tony Duan
Popups are small boxes containing arbitrary HTML, that point to a specific point on the map.
content <- paste(sep = "<br/>",
"<b><a href='https://www.samurainoodle.com/'>Samurai Noodle</a></b>",
"606 5th Ave. S",
"Seattle, WA 98138"
)
leaflet() %>% addTiles() %>%
setView(-122.327298, 47.597131,zoom = 12) %>%
addPopups(-122.327298, 47.597131, content,
#options = popupOptions(closeButton = FALSE)
options = popupOptions(closeButton = FALSE)
)
A label is a textual or HTML content that can attached to markers and shapes to be always displayed or displayed on mouse over. Unlike popups you don’t need to click a marker/polygon for the label to be shown.
library(openxlsx)
library(readxl)
per_gdp_usd=read_excel('world data.xlsx') %>% mutate(
name=case_when(
name =="United States" ~ "United States of America"
,name =="Russia" ~ "Russian Federation"
,name =="U.K. of Great Britain and Northern Ireland" ~ "United Kingdom"
,name =="U.K. of Great Britain and Northern Ireland" ~ "United Kingdom"
,name == "South Korea"~ "Republic of Korea"
,name =="Lao People's Democratic Republic" ~ "Laos"
,TRUE ~ name
)
)
Rows: 158
Columns: 12
$ name <chr> "Albania", "Algeria", "Andorra", "Angola", "A…
$ geo_point_2d <chr> "{ \"lon\": 20.068384605918776, \"lat\": 41.1…
$ iso3 <chr> "ALB", "DZA", "AND", "AGO", "ARG", "ARM", "AU…
$ status <chr> "Member State", "Member State", "Member State…
$ color_code <chr> "ALB", "DZA", "AND", "AGO", "ARG", "ARM", "AU…
$ continent <chr> "Europe", "Africa", "Europe", "Africa", "Amer…
$ region <chr> "Southern Europe", "Northern Africa", "Southe…
$ iso_3166_1_alpha_2_codes <chr> "AL", "DZ", "AD", "AO", "AR", "AM", "AU", "AT…
$ french_short <chr> "Albanie", "Algérie", "Andorre", "Angola", "A…
$ geometry <MULTIPOLYGON [°]> MULTIPOLYGON (((20.07142 42..., …
$ per_gdp_total <dbl> 1.888210e+10, 1.919130e+11, 3.352033e+09, 1.0…
$ per_gdp_usd <dbl> 6643, 4274, 41993, 2999, 13904, 7014, 64003, …
pal <- colorNumeric("viridis", NULL)
leaflet(map_sf002) %>%
addTiles() %>%
addPolygons(smoothFactor = 0.3, fillOpacity = 0.5,weight = 1,
fillColor = ~ pal(per_gdp_usd)
,popup = ~ paste0(
"地区:", name, "<br/>",
"<hr/>",
"人均gpd:", per_gdp_usd, "(千 美元)", "<br/>"
)
,label = lapply(paste0(
"地区:", "<b>", map_sf002$name, "</b>", "<br/>",
"人均gpd 美元:", round(map_sf002$per_gdp_usd/1000), "K <br/>",
"总gpd 美元:", round(map_sf002$per_gdp_total/1000000), "M <br/>"
), htmltools::HTML)
)%>% addLegend(
position = "bottomright", title = "人均gpd(美元)",
pal = pal, values = ~per_gdp_usd, opacity = 1.0
)
深圳市: ::: {.cell}
:::
中国 2022 各省 人均gpd usd:
pal <- colorNumeric("viridis", NULL)
leaflet(map_sf002) %>%
addTiles() %>%
addPolygons(smoothFactor = 0.3, fillOpacity = 0.5,weight = 1,
fillColor = ~ pal(per_gpd_usd)
,popup = ~ paste0(
"地区:", name, "<br/>",
"<hr/>",
"人均gpd:", per_gpd_usd, "(美万)", "<br/>"
)
,label = lapply(paste0(
"地区:", "<b>", map_sf002$name, "</b>", "<br/>",
"人均gpd 美元:", map_sf002$per_gpd_usd, "<br/>",
"人均gpd 人民币:", map_sf002$per_gpd_rmb, "<br/>",
"人口:", round(map_sf002$total_gpd_rmb/map_sf002$per_gpd_rmb), "<br/>"
), htmltools::HTML)
)%>% addLegend(
position = "bottomright", title = "人均gpd(美元)",
pal = pal, values = ~per_gpd_usd, opacity = 1.0
)
GPD data source:国家统计局数据库
广东省 2021 人均gpd usd:
data source:广东统计年鉴2022 ::: {.cell}
pal <- colorNumeric("viridis", NULL)
leaflet(map_sf002) %>%
addTiles() %>%
addPolygons(smoothFactor = 0.3, fillOpacity = 0.5,weight = 1,
fillColor = ~ pal(per_gpd_usd)
,popup = ~ paste0(
"城市:", name, "<br/>",
"<hr/>",
"人均gpd:", per_gpd_usd, "(美万)", "<br/>"
)
,label = lapply(paste0(
"城市:", "<b>", map_sf002$name, "</b>", "<br/>",
"人均gpd 美元:", map_sf002$per_gpd_usd, "<br/>",
"人均gpd 人民币:", map_sf002$per_gpd_rmb, "<br/>",
"人口:", round(map_sf002$total_gpd_rmb*10000000/map_sf002$per_gpd_rmb), "<br/>"
), htmltools::HTML)
)%>% addLegend(
position = "bottomright", title = "人均gpd(美元)",
pal = pal, values = ~per_gpd_usd, opacity = 1.0
)
:::
https://zh.wikipedia.org/wiki/%E5%B9%BF%E4%B8%9C%E5%90%84%E5%9C%B0%E7%BA%A7%E5%B8%82%E5%9C%B0%E5%8C%BA%E7%94%9F%E4%BA%A7%E6%80%BB%E5%80%BC%E5%88%97%E8%A1%A8
add provincial capital ::: {.cell}
:::
[1] 34
Simple feature collection with 1 feature and 4 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: 119.3183 ymin: 21.75147 xmax: 124.5656 ymax: 25.92592
Geodetic CRS: WGS 84
# A tibble: 1 × 5
adcode name center level geometry
<chr> <chr> <chr> <chr> <MULTIPOLYGON [°]>
1 710000 台湾省 121.509062,25.044332 province (((119.5543 23.68248, 119.555 23.…
[1] 334
Simple feature collection with 1 feature and 4 fields
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: 113.5215 ymin: 22.65421 xmax: 114.2603 ymax: 23.14205
Geodetic CRS: WGS 84
# A tibble: 1 × 5
adcode name center level geometry
<chr> <chr> <chr> <chr> <POLYGON [°]>
1 441900 东莞市 113.746262,23.046237 city ((114.2292 22.81251, 114.2278 22.813…
Simple feature collection with 1 feature and 4 fields
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: 113.157 ymin: 22.20104 xmax: 113.692 ymax: 22.7726
Geodetic CRS: WGS 84
# A tibble: 1 × 5
adcode name center level geometry
<chr> <chr> <chr> <chr> <POLYGON [°]>
1 442000 中山市 113.382391,22.521113 city ((113.5687 22.41193, 113.5666 22.412…
Simple feature collection with 1 feature and 4 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: 115.9267 ymin: 20.58265 xmax: 116.9338 ymax: 21.12693
Geodetic CRS: WGS 84
# A tibble: 1 × 5
adcode name center level geometry
<chr> <chr> <chr> <chr> <MULTIPOLYGON [°]>
1 442100 东沙群岛 116.887312,20.617512 city (((115.9433 21.09745, 115.95 21.11…
Simple feature collection with 1 feature and 4 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: 108.9287 ymin: 19.17894 xmax: 109.7694 ymax: 19.92575
Geodetic CRS: WGS 84
# A tibble: 1 × 5
adcode name center level geometry
<chr> <chr> <chr> <chr> <MULTIPOLYGON [°]>
1 460400 儋州市 109.576782,19.517486 city (((109.4322 19.91302, 109.4253 19.91…
Simple feature collection with 1 feature and 4 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: 97.8483 ymin: 39.65426 xmax: 98.52018 ymax: 39.99979
Geodetic CRS: WGS 84
# A tibble: 1 × 5
adcode name center level geometry
<chr> <chr> <chr> <chr> <MULTIPOLYGON [°]>
1 620200 嘉峪关市 98.277304,39.786529 city (((97.85974 39.7169, 97.85827 39.71…
X = list.files("./GeoMapData_CN/citys", recursive = T, full.names = T)
X2=X[-which(X %in% c("./GeoMapData_CN/citys/620200.json"
,"./GeoMapData_CN/citys/460400.json"
,"./GeoMapData_CN/citys/442100.json"
,"./GeoMapData_CN/citys/442000.json"
,"./GeoMapData_CN/citys/441900.json"
))]
map_list <- lapply(
X = X2,
FUN = sf::read_sf
)
$OpenStreetMap
[1] "OpenStreetMap"
$OpenStreetMap.Mapnik
[1] "OpenStreetMap.Mapnik"
$OpenStreetMap.DE
[1] "OpenStreetMap.DE"
$OpenStreetMap.CH
[1] "OpenStreetMap.CH"
$OpenStreetMap.France
[1] "OpenStreetMap.France"
$OpenStreetMap.HOT
[1] "OpenStreetMap.HOT"
$OpenStreetMap.BZH
[1] "OpenStreetMap.BZH"
$MapTilesAPI
[1] "MapTilesAPI"
$MapTilesAPI.OSMEnglish
[1] "MapTilesAPI.OSMEnglish"
$MapTilesAPI.OSMFrancais
[1] "MapTilesAPI.OSMFrancais"
$MapTilesAPI.OSMEspagnol
[1] "MapTilesAPI.OSMEspagnol"
$OpenSeaMap
[1] "OpenSeaMap"
$OPNVKarte
[1] "OPNVKarte"
$OpenTopoMap
[1] "OpenTopoMap"
$OpenRailwayMap
[1] "OpenRailwayMap"
$OpenFireMap
[1] "OpenFireMap"
$SafeCast
[1] "SafeCast"
$Stadia
[1] "Stadia"
$Stadia.AlidadeSmooth
[1] "Stadia.AlidadeSmooth"
$Stadia.AlidadeSmoothDark
[1] "Stadia.AlidadeSmoothDark"
$Stadia.OSMBright
[1] "Stadia.OSMBright"
$Stadia.Outdoors
[1] "Stadia.Outdoors"
$Stadia.StamenToner
[1] "Stadia.StamenToner"
$Stadia.StamenTonerBackground
[1] "Stadia.StamenTonerBackground"
$Stadia.StamenTonerLines
[1] "Stadia.StamenTonerLines"
$Stadia.StamenTonerLabels
[1] "Stadia.StamenTonerLabels"
$Stadia.StamenTonerLite
[1] "Stadia.StamenTonerLite"
$Stadia.StamenWatercolor
[1] "Stadia.StamenWatercolor"
$Stadia.StamenTerrain
[1] "Stadia.StamenTerrain"
$Stadia.StamenTerrainBackground
[1] "Stadia.StamenTerrainBackground"
$Stadia.StamenTerrainLabels
[1] "Stadia.StamenTerrainLabels"
$Stadia.StamenTerrainLines
[1] "Stadia.StamenTerrainLines"
$Thunderforest
[1] "Thunderforest"
$Thunderforest.OpenCycleMap
[1] "Thunderforest.OpenCycleMap"
$Thunderforest.Transport
[1] "Thunderforest.Transport"
$Thunderforest.TransportDark
[1] "Thunderforest.TransportDark"
$Thunderforest.SpinalMap
[1] "Thunderforest.SpinalMap"
$Thunderforest.Landscape
[1] "Thunderforest.Landscape"
$Thunderforest.Outdoors
[1] "Thunderforest.Outdoors"
$Thunderforest.Pioneer
[1] "Thunderforest.Pioneer"
$Thunderforest.MobileAtlas
[1] "Thunderforest.MobileAtlas"
$Thunderforest.Neighbourhood
[1] "Thunderforest.Neighbourhood"
$CyclOSM
[1] "CyclOSM"
$Jawg
[1] "Jawg"
$Jawg.Streets
[1] "Jawg.Streets"
$Jawg.Terrain
[1] "Jawg.Terrain"
$Jawg.Sunny
[1] "Jawg.Sunny"
$Jawg.Dark
[1] "Jawg.Dark"
$Jawg.Light
[1] "Jawg.Light"
$Jawg.Matrix
[1] "Jawg.Matrix"
$MapBox
[1] "MapBox"
$MapTiler
[1] "MapTiler"
$MapTiler.Streets
[1] "MapTiler.Streets"
$MapTiler.Basic
[1] "MapTiler.Basic"
$MapTiler.Bright
[1] "MapTiler.Bright"
$MapTiler.Pastel
[1] "MapTiler.Pastel"
$MapTiler.Positron
[1] "MapTiler.Positron"
$MapTiler.Hybrid
[1] "MapTiler.Hybrid"
$MapTiler.Toner
[1] "MapTiler.Toner"
$MapTiler.Topo
[1] "MapTiler.Topo"
$MapTiler.Voyager
[1] "MapTiler.Voyager"
$TomTom
[1] "TomTom"
$TomTom.Basic
[1] "TomTom.Basic"
$TomTom.Hybrid
[1] "TomTom.Hybrid"
$TomTom.Labels
[1] "TomTom.Labels"
$Esri
[1] "Esri"
$Esri.WorldStreetMap
[1] "Esri.WorldStreetMap"
$Esri.DeLorme
[1] "Esri.DeLorme"
$Esri.WorldTopoMap
[1] "Esri.WorldTopoMap"
$Esri.WorldImagery
[1] "Esri.WorldImagery"
$Esri.WorldTerrain
[1] "Esri.WorldTerrain"
$Esri.WorldShadedRelief
[1] "Esri.WorldShadedRelief"
$Esri.WorldPhysical
[1] "Esri.WorldPhysical"
$Esri.OceanBasemap
[1] "Esri.OceanBasemap"
$Esri.NatGeoWorldMap
[1] "Esri.NatGeoWorldMap"
$Esri.WorldGrayCanvas
[1] "Esri.WorldGrayCanvas"
$OpenWeatherMap
[1] "OpenWeatherMap"
$OpenWeatherMap.Clouds
[1] "OpenWeatherMap.Clouds"
$OpenWeatherMap.CloudsClassic
[1] "OpenWeatherMap.CloudsClassic"
$OpenWeatherMap.Precipitation
[1] "OpenWeatherMap.Precipitation"
$OpenWeatherMap.PrecipitationClassic
[1] "OpenWeatherMap.PrecipitationClassic"
$OpenWeatherMap.Rain
[1] "OpenWeatherMap.Rain"
$OpenWeatherMap.RainClassic
[1] "OpenWeatherMap.RainClassic"
$OpenWeatherMap.Pressure
[1] "OpenWeatherMap.Pressure"
$OpenWeatherMap.PressureContour
[1] "OpenWeatherMap.PressureContour"
$OpenWeatherMap.Wind
[1] "OpenWeatherMap.Wind"
$OpenWeatherMap.Temperature
[1] "OpenWeatherMap.Temperature"
$OpenWeatherMap.Snow
[1] "OpenWeatherMap.Snow"
$HERE
[1] "HERE"
$HERE.normalDay
[1] "HERE.normalDay"
$HERE.normalDayCustom
[1] "HERE.normalDayCustom"
$HERE.normalDayGrey
[1] "HERE.normalDayGrey"
$HERE.normalDayMobile
[1] "HERE.normalDayMobile"
$HERE.normalDayGreyMobile
[1] "HERE.normalDayGreyMobile"
$HERE.normalDayTransit
[1] "HERE.normalDayTransit"
$HERE.normalDayTransitMobile
[1] "HERE.normalDayTransitMobile"
$HERE.normalDayTraffic
[1] "HERE.normalDayTraffic"
$HERE.normalNight
[1] "HERE.normalNight"
$HERE.normalNightMobile
[1] "HERE.normalNightMobile"
$HERE.normalNightGrey
[1] "HERE.normalNightGrey"
$HERE.normalNightGreyMobile
[1] "HERE.normalNightGreyMobile"
$HERE.normalNightTransit
[1] "HERE.normalNightTransit"
$HERE.normalNightTransitMobile
[1] "HERE.normalNightTransitMobile"
$HERE.reducedDay
[1] "HERE.reducedDay"
$HERE.reducedNight
[1] "HERE.reducedNight"
$HERE.basicMap
[1] "HERE.basicMap"
$HERE.mapLabels
[1] "HERE.mapLabels"
$HERE.trafficFlow
[1] "HERE.trafficFlow"
$HERE.carnavDayGrey
[1] "HERE.carnavDayGrey"
$HERE.hybridDay
[1] "HERE.hybridDay"
$HERE.hybridDayMobile
[1] "HERE.hybridDayMobile"
$HERE.hybridDayTransit
[1] "HERE.hybridDayTransit"
$HERE.hybridDayGrey
[1] "HERE.hybridDayGrey"
$HERE.hybridDayTraffic
[1] "HERE.hybridDayTraffic"
$HERE.pedestrianDay
[1] "HERE.pedestrianDay"
$HERE.pedestrianNight
[1] "HERE.pedestrianNight"
$HERE.satelliteDay
[1] "HERE.satelliteDay"
$HERE.terrainDay
[1] "HERE.terrainDay"
$HERE.terrainDayMobile
[1] "HERE.terrainDayMobile"
$HEREv3
[1] "HEREv3"
$HEREv3.normalDay
[1] "HEREv3.normalDay"
$HEREv3.normalDayCustom
[1] "HEREv3.normalDayCustom"
$HEREv3.normalDayGrey
[1] "HEREv3.normalDayGrey"
$HEREv3.normalDayMobile
[1] "HEREv3.normalDayMobile"
$HEREv3.normalDayGreyMobile
[1] "HEREv3.normalDayGreyMobile"
$HEREv3.normalDayTransit
[1] "HEREv3.normalDayTransit"
$HEREv3.normalDayTransitMobile
[1] "HEREv3.normalDayTransitMobile"
$HEREv3.normalNight
[1] "HEREv3.normalNight"
$HEREv3.normalNightMobile
[1] "HEREv3.normalNightMobile"
$HEREv3.normalNightGrey
[1] "HEREv3.normalNightGrey"
$HEREv3.normalNightGreyMobile
[1] "HEREv3.normalNightGreyMobile"
$HEREv3.normalNightTransit
[1] "HEREv3.normalNightTransit"
$HEREv3.normalNightTransitMobile
[1] "HEREv3.normalNightTransitMobile"
$HEREv3.reducedDay
[1] "HEREv3.reducedDay"
$HEREv3.reducedNight
[1] "HEREv3.reducedNight"
$HEREv3.basicMap
[1] "HEREv3.basicMap"
$HEREv3.mapLabels
[1] "HEREv3.mapLabels"
$HEREv3.trafficFlow
[1] "HEREv3.trafficFlow"
$HEREv3.carnavDayGrey
[1] "HEREv3.carnavDayGrey"
$HEREv3.hybridDay
[1] "HEREv3.hybridDay"
$HEREv3.hybridDayMobile
[1] "HEREv3.hybridDayMobile"
$HEREv3.hybridDayTransit
[1] "HEREv3.hybridDayTransit"
$HEREv3.hybridDayGrey
[1] "HEREv3.hybridDayGrey"
$HEREv3.pedestrianDay
[1] "HEREv3.pedestrianDay"
$HEREv3.pedestrianNight
[1] "HEREv3.pedestrianNight"
$HEREv3.satelliteDay
[1] "HEREv3.satelliteDay"
$HEREv3.terrainDay
[1] "HEREv3.terrainDay"
$HEREv3.terrainDayMobile
[1] "HEREv3.terrainDayMobile"
$FreeMapSK
[1] "FreeMapSK"
$MtbMap
[1] "MtbMap"
$CartoDB
[1] "CartoDB"
$CartoDB.Positron
[1] "CartoDB.Positron"
$CartoDB.PositronNoLabels
[1] "CartoDB.PositronNoLabels"
$CartoDB.PositronOnlyLabels
[1] "CartoDB.PositronOnlyLabels"
$CartoDB.DarkMatter
[1] "CartoDB.DarkMatter"
$CartoDB.DarkMatterNoLabels
[1] "CartoDB.DarkMatterNoLabels"
$CartoDB.DarkMatterOnlyLabels
[1] "CartoDB.DarkMatterOnlyLabels"
$CartoDB.Voyager
[1] "CartoDB.Voyager"
$CartoDB.VoyagerNoLabels
[1] "CartoDB.VoyagerNoLabels"
$CartoDB.VoyagerOnlyLabels
[1] "CartoDB.VoyagerOnlyLabels"
$CartoDB.VoyagerLabelsUnder
[1] "CartoDB.VoyagerLabelsUnder"
$HikeBike
[1] "HikeBike"
$HikeBike.HikeBike
[1] "HikeBike.HikeBike"
$HikeBike.HillShading
[1] "HikeBike.HillShading"
$BasemapAT
[1] "BasemapAT"
$BasemapAT.basemap
[1] "BasemapAT.basemap"
$BasemapAT.grau
[1] "BasemapAT.grau"
$BasemapAT.overlay
[1] "BasemapAT.overlay"
$BasemapAT.terrain
[1] "BasemapAT.terrain"
$BasemapAT.surface
[1] "BasemapAT.surface"
$BasemapAT.highdpi
[1] "BasemapAT.highdpi"
$BasemapAT.orthofoto
[1] "BasemapAT.orthofoto"
$nlmaps
[1] "nlmaps"
$nlmaps.standaard
[1] "nlmaps.standaard"
$nlmaps.pastel
[1] "nlmaps.pastel"
$nlmaps.grijs
[1] "nlmaps.grijs"
$nlmaps.water
[1] "nlmaps.water"
$nlmaps.luchtfoto
[1] "nlmaps.luchtfoto"
$NASAGIBS
[1] "NASAGIBS"
$NASAGIBS.ModisTerraTrueColorCR
[1] "NASAGIBS.ModisTerraTrueColorCR"
$NASAGIBS.ModisTerraBands367CR
[1] "NASAGIBS.ModisTerraBands367CR"
$NASAGIBS.ViirsEarthAtNight2012
[1] "NASAGIBS.ViirsEarthAtNight2012"
$NASAGIBS.ModisTerraLSTDay
[1] "NASAGIBS.ModisTerraLSTDay"
$NASAGIBS.ModisTerraSnowCover
[1] "NASAGIBS.ModisTerraSnowCover"
$NASAGIBS.ModisTerraAOD
[1] "NASAGIBS.ModisTerraAOD"
$NASAGIBS.ModisTerraChlorophyll
[1] "NASAGIBS.ModisTerraChlorophyll"
$NLS
[1] "NLS"
$JusticeMap
[1] "JusticeMap"
$JusticeMap.income
[1] "JusticeMap.income"
$JusticeMap.americanIndian
[1] "JusticeMap.americanIndian"
$JusticeMap.asian
[1] "JusticeMap.asian"
$JusticeMap.black
[1] "JusticeMap.black"
$JusticeMap.hispanic
[1] "JusticeMap.hispanic"
$JusticeMap.multi
[1] "JusticeMap.multi"
$JusticeMap.nonWhite
[1] "JusticeMap.nonWhite"
$JusticeMap.white
[1] "JusticeMap.white"
$JusticeMap.plurality
[1] "JusticeMap.plurality"
$GeoportailFrance
[1] "GeoportailFrance"
$GeoportailFrance.plan
[1] "GeoportailFrance.plan"
$GeoportailFrance.parcels
[1] "GeoportailFrance.parcels"
$GeoportailFrance.orthos
[1] "GeoportailFrance.orthos"
$OneMapSG
[1] "OneMapSG"
$OneMapSG.Default
[1] "OneMapSG.Default"
$OneMapSG.Night
[1] "OneMapSG.Night"
$OneMapSG.Original
[1] "OneMapSG.Original"
$OneMapSG.Grey
[1] "OneMapSG.Grey"
$OneMapSG.LandLot
[1] "OneMapSG.LandLot"
$USGS
[1] "USGS"
$USGS.USTopo
[1] "USGS.USTopo"
$USGS.USImagery
[1] "USGS.USImagery"
$USGS.USImageryTopo
[1] "USGS.USImageryTopo"
$WaymarkedTrails
[1] "WaymarkedTrails"
$WaymarkedTrails.hiking
[1] "WaymarkedTrails.hiking"
$WaymarkedTrails.cycling
[1] "WaymarkedTrails.cycling"
$WaymarkedTrails.mtb
[1] "WaymarkedTrails.mtb"
$WaymarkedTrails.slopes
[1] "WaymarkedTrails.slopes"
$WaymarkedTrails.riding
[1] "WaymarkedTrails.riding"
$WaymarkedTrails.skating
[1] "WaymarkedTrails.skating"
$OpenAIP
[1] "OpenAIP"
$OpenSnowMap
[1] "OpenSnowMap"
$OpenSnowMap.pistes
[1] "OpenSnowMap.pistes"
$AzureMaps
[1] "AzureMaps"
$AzureMaps.MicrosoftImagery
[1] "AzureMaps.MicrosoftImagery"
$AzureMaps.MicrosoftBaseDarkGrey
[1] "AzureMaps.MicrosoftBaseDarkGrey"
$AzureMaps.MicrosoftBaseRoad
[1] "AzureMaps.MicrosoftBaseRoad"
$AzureMaps.MicrosoftBaseHybridRoad
[1] "AzureMaps.MicrosoftBaseHybridRoad"
$AzureMaps.MicrosoftTerraMain
[1] "AzureMaps.MicrosoftTerraMain"
$AzureMaps.MicrosoftWeatherInfraredMain
[1] "AzureMaps.MicrosoftWeatherInfraredMain"
$AzureMaps.MicrosoftWeatherRadarMain
[1] "AzureMaps.MicrosoftWeatherRadarMain"
$SwissFederalGeoportal
[1] "SwissFederalGeoportal"
$SwissFederalGeoportal.NationalMapColor
[1] "SwissFederalGeoportal.NationalMapColor"
$SwissFederalGeoportal.NationalMapGrey
[1] "SwissFederalGeoportal.NationalMapGrey"
$SwissFederalGeoportal.SWISSIMAGE
[1] "SwissFederalGeoportal.SWISSIMAGE"
R version 4.4.1 (2024-06-14)
Platform: aarch64-apple-darwin20
Running under: macOS 15.1
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.0
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
time zone: Asia/Shanghai
tzcode source: internal
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] readxl_1.4.3 openxlsx_4.2.7.1 htmltools_0.5.8.1
[4] leaflet.extras_2.0.1 geojsonio_0.11.3 leaflet_2.2.2
[7] sf_1.0-18 lubridate_1.9.4 forcats_1.0.0
[10] stringr_1.5.1 dplyr_1.1.4 purrr_1.0.2
[13] readr_2.1.5 tidyr_1.3.1 tibble_3.2.1
[16] ggplot2_3.5.1 tidyverse_2.0.0
loaded via a namespace (and not attached):
[1] gtable_0.3.6 xfun_0.48 htmlwidgets_1.6.4
[4] lattice_0.22-6 tzdb_0.4.0 leaflet.providers_2.0.0
[7] vctrs_0.6.5 tools_4.4.1 crosstalk_1.2.1
[10] generics_0.1.3 curl_6.2.0 proxy_0.4-27
[13] pkgconfig_2.0.3 KernSmooth_2.23-24 RColorBrewer_1.1-3
[16] lifecycle_1.0.4 farver_2.1.2 compiler_4.4.1
[19] munsell_0.5.1 jqr_1.4.0 class_7.3-22
[22] yaml_2.3.10 lazyeval_0.2.2 pillar_1.10.1
[25] jquerylib_0.1.4 classInt_0.4-10 zip_2.3.1
[28] tidyselect_1.2.1 digest_0.6.37 stringi_1.8.4
[31] fastmap_1.2.0 grid_4.4.1 colorspace_2.1-1
[34] cli_3.6.4 magrittr_2.0.3 utf8_1.2.4
[37] crul_1.5.0 e1071_1.7-16 withr_3.0.2
[40] scales_1.3.0 sp_2.1-4 timechange_0.3.0
[43] rmarkdown_2.28 cellranger_1.1.0 hms_1.1.3
[46] evaluate_1.0.1 knitr_1.48 V8_6.0.0
[49] viridisLite_0.4.2 geojson_0.3.5 rlang_1.1.5
[52] Rcpp_1.0.13-1 glue_1.8.0 DBI_1.2.3
[55] httpcode_0.3.0 geojsonsf_2.0.3 rstudioapi_0.17.1
[58] jsonlite_1.8.9 R6_2.6.1 units_0.8-5
https://rstudio.github.io/leaflet/
https://github.com/Lchiffon/leafletCN
https://github.com/longwosion/geojson-map-china
https://xiangyun.rbind.io/2022/02/draw-china-maps/
https://datav.aliyun.com/portal/school/atlas/area_selector
---
title: "map"
subtitle: "with leaflet"
author: "Tony Duan"
execute:
warning: false
error: false
format:
html:
toc: true
toc-location: right
code-fold: show
code-tools: true
number-sections: true
code-block-bg: true
code-block-border-left: "#31BAE9"
code-copy: true
---
# data
```{r}
library(tidyverse)
library(sf)
library(leaflet)
library(geojsonio)
library(leaflet.extras)
```
# display at openstreet map
```{r}
m <- leaflet() %>%
# Add default OpenStreetMap map tiles
addTiles() %>%
# add markers
addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
m
```
# display at google map
```{r}
leaflet() |>
# add base mao
addTiles(urlTemplate = "https://mt1.google.com/vt/lyrs=m&x={x}&y={y}&z={z}") |>
# set view
setView(116.347817690225, 39.997202126977, zoom = 16) |>
# add markers
addMarkers(116.347817690225, 39.997202126977)
```
# Third-Party map
```{r}
#m <- leaflet() %>% setView(lng = -71.0589, lat = 42.3601, zoom = 10)
#m %>% addProviderTiles(providers$Stadia.StamenToner)
```
# add pop up
Popups are small boxes containing arbitrary HTML, that point to a specific point on the map.
```{r}
content <- paste(sep = "<br/>",
"<b><a href='https://www.samurainoodle.com/'>Samurai Noodle</a></b>",
"606 5th Ave. S",
"Seattle, WA 98138"
)
leaflet() %>% addTiles() %>%
setView(-122.327298, 47.597131,zoom = 12) %>%
addPopups(-122.327298, 47.597131, content,
#options = popupOptions(closeButton = FALSE)
options = popupOptions(closeButton = FALSE)
)
```
# add Markers
```{r}
library(htmltools)
df <- read.csv(textConnection(
"Name,Lat,Long
Samurai Noodle,47.597131,-122.327298
Kukai Ramen,47.6154,-122.327157
Tsukushinbo,47.59987,-122.326726"
))
leaflet(df) %>% addTiles() %>%
addMarkers(~Long, ~Lat, popup = ~htmlEscape(Name))
```
# add Labels
A label is a textual or HTML content that can attached to markers and shapes to be always displayed or displayed on mouse over. Unlike popups you don’t need to click a marker/polygon for the label to be shown.
```{r}
library(htmltools)
df <- read.csv(textConnection(
"Name,Lat,Long
Samurai Noodle,47.597131,-122.327298
Kukai Ramen,47.6154,-122.327157
Tsukushinbo,47.59987,-122.326726"))
leaflet(df) %>% addTiles() %>%
addMarkers(~Long, ~Lat, label = ~htmlEscape(Name))
```
# World map
```{r}
#install.packages("https://cran.r-project.org/src/contrib/Archive/maptools/maptools_1.1-8.tar.gz")
```
```{r}
json_data=read_sf("world-administrative-boundaries.geojson")
```
```{r}
map_df <- as.data.frame(json_data)
```
```{r}
#Get my variable
#name<-c("Ghana", "Grenada", "Guyana", "India", "Jamaica", "Kenya", "United States","Canada")
#val<-c(1,2,4,5,5,1000,20000, 100)
#per_gdp_usd<-data.frame(name,val)
```
```{r}
library(openxlsx)
library(readxl)
per_gdp_usd=read_excel('world data.xlsx') %>% mutate(
name=case_when(
name =="United States" ~ "United States of America"
,name =="Russia" ~ "Russian Federation"
,name =="U.K. of Great Britain and Northern Ireland" ~ "United Kingdom"
,name =="U.K. of Great Britain and Northern Ireland" ~ "United Kingdom"
,name == "South Korea"~ "Republic of Korea"
,name =="Lao People's Democratic Republic" ~ "Laos"
,TRUE ~ name
)
)
```
```{r}
#test=full_join(map_df, per_gdp_usd, by="name")
#left =test %>% filter(is.na(iso3)==TRUE)
#right=test %>% filter(is.na(per_gdp_total)==TRUE)
```
```{r}
map_df002 <- merge(map_df, per_gdp_usd, by.x = "name", by.y = "name")
```
```{r}
glimpse(map_df002)
```
```{r}
map_sf002 <- sf::st_as_sf(map_df002, sf_column_name = "geometry")
```
```{r}
pal <- colorNumeric("viridis", NULL)
leaflet(map_sf002) %>%
addTiles() %>%
addPolygons(smoothFactor = 0.3, fillOpacity = 0.5,weight = 1,
fillColor = ~ pal(per_gdp_usd)
,popup = ~ paste0(
"地区:", name, "<br/>",
"<hr/>",
"人均gpd:", per_gdp_usd, "(千 美元)", "<br/>"
)
,label = lapply(paste0(
"地区:", "<b>", map_sf002$name, "</b>", "<br/>",
"人均gpd 美元:", round(map_sf002$per_gdp_usd/1000), "K <br/>",
"总gpd 美元:", round(map_sf002$per_gdp_total/1000000), "M <br/>"
), htmltools::HTML)
)%>% addLegend(
position = "bottomright", title = "人均gpd(美元)",
pal = pal, values = ~per_gdp_usd, opacity = 1.0
)
```
# China one city map
```{r}
json_data <- sf::read_sf("./GeoMapData_CN/citys/440300.json")
#or
json_data=read_sf("https://geo.datav.aliyun.com/areas_v2/bound/440300_full.json")
```
深圳市:
```{r}
pal <- colorNumeric("viridis", NULL)
leaflet(json_data) %>%
addTiles() %>%
addPolygons(smoothFactor = 0.3, fillOpacity = 0.1)
```
# all China each province GPD map
```{r}
json_data=read_sf("https://geo.datav.aliyun.com/areas_v2/bound/100000_full.json")
```
中国 2022 各省 人均gpd usd:
```{r}
library(openxlsx)
library(readxl)
per_gdp_usd=read_excel('china gdp2022.xlsx')
```
```{r}
map_df <- as.data.frame(json_data)
```
```{r}
map_df002 <- merge(map_df, per_gdp_usd, by.x = "name", by.y = "city")
```
```{r}
map_sf002 <- sf::st_as_sf(map_df002, sf_column_name = "geometry")
```
```{r}
pal <- colorNumeric("viridis", NULL)
leaflet(map_sf002) %>%
addTiles() %>%
addPolygons(smoothFactor = 0.3, fillOpacity = 0.5,weight = 1,
fillColor = ~ pal(per_gpd_usd)
,popup = ~ paste0(
"地区:", name, "<br/>",
"<hr/>",
"人均gpd:", per_gpd_usd, "(美万)", "<br/>"
)
,label = lapply(paste0(
"地区:", "<b>", map_sf002$name, "</b>", "<br/>",
"人均gpd 美元:", map_sf002$per_gpd_usd, "<br/>",
"人均gpd 人民币:", map_sf002$per_gpd_rmb, "<br/>",
"人口:", round(map_sf002$total_gpd_rmb/map_sf002$per_gpd_rmb), "<br/>"
), htmltools::HTML)
)%>% addLegend(
position = "bottomright", title = "人均gpd(美元)",
pal = pal, values = ~per_gpd_usd, opacity = 1.0
)
```
GPD data source:国家统计局数据库
# China one province map each city GPD
```{r}
json_data <- sf::read_sf("./GeoMapData_CN/province/440000.json")
```
广东省 2021 人均gpd usd:
```{r}
library(openxlsx)
library(readxl)
per_gdp_usd=read_excel('guangdong city gdp2021.xlsx')
```
```{r}
map_df <- as.data.frame(json_data)
```
```{r}
map_df002 <- merge(map_df, per_gdp_usd, by.x = "name", by.y = "city")
```
```{r}
map_sf002 <- sf::st_as_sf(map_df002, sf_column_name = "geometry")
```
data source:广东统计年鉴2022
```{r}
pal <- colorNumeric("viridis", NULL)
leaflet(map_sf002) %>%
addTiles() %>%
addPolygons(smoothFactor = 0.3, fillOpacity = 0.5,weight = 1,
fillColor = ~ pal(per_gpd_usd)
,popup = ~ paste0(
"城市:", name, "<br/>",
"<hr/>",
"人均gpd:", per_gpd_usd, "(美万)", "<br/>"
)
,label = lapply(paste0(
"城市:", "<b>", map_sf002$name, "</b>", "<br/>",
"人均gpd 美元:", map_sf002$per_gpd_usd, "<br/>",
"人均gpd 人民币:", map_sf002$per_gpd_rmb, "<br/>",
"人口:", round(map_sf002$total_gpd_rmb*10000000/map_sf002$per_gpd_rmb), "<br/>"
), htmltools::HTML)
)%>% addLegend(
position = "bottomright", title = "人均gpd(美元)",
pal = pal, values = ~per_gpd_usd, opacity = 1.0
)
```
https://zh.wikipedia.org/wiki/%E5%B9%BF%E4%B8%9C%E5%90%84%E5%9C%B0%E7%BA%A7%E5%B8%82%E5%9C%B0%E5%8C%BA%E7%94%9F%E4%BA%A7%E6%80%BB%E5%80%BC%E5%88%97%E8%A1%A8
# China province map
```{r}
library(leaflet)
library(sf)
json_data <- sf::read_sf("./GeoMapData_CN/china.json")
#or
json_data=read_sf("https://geo.datav.aliyun.com/areas_v2/bound/100000_full.json")
pal <- colorNumeric("viridis", NULL)
m=leaflet(json_data) %>%
addTiles() %>%
addPolygons(smoothFactor = 0.3, fillOpacity = 0.1)
m
```
add provincial capital
```{r}
China=read_sf("https://geo.datav.aliyun.com/areas_v2/bound/100000_full.json")
```
```{r}
China002=China %>% as_data_frame() %>% mutate(
center2=as.character(center) %>% str_replace('c','')%>% str_replace('[(]','') %>% str_replace('[)]','')
)
```
```{r}
China003=China002%>%separate(center2, c("x", "y"), ", ")
```
```{r}
China_point = China003 %>%
slice(-35)
m %>%
addCircles(data = China_point,
lng = ~as.numeric(x), lat = ~as.numeric(y),color = "red",weight = 10,
fillOpacity =2)
```
# China province and city map
```{r}
map_list <- lapply(
X = list.files("./GeoMapData_CN/province", recursive = T, full.names = T),
FUN = sf::read_sf
)
length(map_list)
```
```{r}
for (i in c(1:length(map_list))){
#print(i)
#print(dim(map_list[[i]]))
#print(ncol(map_list[[i]]))
if(ncol(map_list[[i]])!=10){print(map_list[[i]])}
}
```
```{r}
X = list.files("./GeoMapData_CN/province", recursive = T, full.names = T)
X2=X[-which(X %in% c("./GeoMapData_CN/province/710000.json"
))]
map_list <- lapply(
X = X2,
FUN = sf::read_sf
)
```
```{r}
length(map_list)
```
```{r}
province_map <- Reduce("rbind", map_list)
```
```{r}
# library(leaflet)
# library(sf)
#
# json_data <- province_map
#
#
# pal <- colorNumeric(c("red", "green", "blue"), 1:10)
#
# leaflet(json_data) %>%
# addTiles() %>%
# addPolygons(smoothFactor = 0.3, fillOpacity = 0.1)
```
# China city and district map
```{r}
map_list <- lapply(
X = list.files("./GeoMapData_CN/citys", recursive = T, full.names = T),
FUN = sf::read_sf
)
length(map_list)
```
```{r}
for (i in c(1:length(map_list))){
#print(i)
#print(dim(map_list[[i]]))
#print(ncol(map_list[[i]]))
if(ncol(map_list[[i]])!=10){print(map_list[[i]])}
}
```
```{r}
X = list.files("./GeoMapData_CN/citys", recursive = T, full.names = T)
X2=X[-which(X %in% c("./GeoMapData_CN/citys/620200.json"
,"./GeoMapData_CN/citys/460400.json"
,"./GeoMapData_CN/citys/442100.json"
,"./GeoMapData_CN/citys/442000.json"
,"./GeoMapData_CN/citys/441900.json"
))]
map_list <- lapply(
X = X2,
FUN = sf::read_sf
)
```
```{r}
length(map_list)
```
```{r}
province_map <- Reduce("rbind", map_list)
```
```{r}
# library(leaflet)
# library(sf)
#
# json_data <- province_map
#
#
# pal <- colorNumeric(c("red", "green", "blue"), 1:10)
#
# leaflet(json_data) %>%
# addTiles() %>%
# addPolygons(smoothFactor = 0.3, fillOpacity = 0.1)
```
# show all map providers
```{r, attr.output='.details summary="providers"'}
providers
```
```{r, attr.output='.details summary="sessionInfo()"'}
sessionInfo()
```
# resouce:
https://rstudio.github.io/leaflet/
https://github.com/Lchiffon/leafletCN
https://github.com/longwosion/geojson-map-china
https://xiangyun.rbind.io/2022/02/draw-china-maps/
https://datav.aliyun.com/portal/school/atlas/area_selector