根据先前几篇内容的框架,今天介绍leaflet在线地图的第三篇,以线条元素构造的路径图。
library(leaflet) library(dplyr) data<-read.table("D:/R/File/subwayline.txt",header=TRUE,stringsAsFactors = FALSE) data$Type<-as.factor(data$Type) data1<-filter(data,Type=="NO1") data2<-filter(data,Type=="NO5")
以上我用百度地图的坐标拾取平台采点拾取的背景地铁一号线和五号线的车站地理位置信息,(因为没有现成的数据,只能手工采点,可能不是很准确)
今天要介绍的leaflet类型是线条,也即addPolylines函数,其基本语法如下:
addPolylines(map, lng = NULL,lat = NULL, #指定数据源并做经纬度声明
layerId = NULL,
group = NULL, #图层分组(制作动态可见性交互时使用)
stroke = TRUE, #是否呈现路径线
color = "#03F",weight = 5,opacity = 0.5, #关于线条的颜色、宽度、透明度
fill = FALSE, fillColor = color, fillOpacity = 0.2,
dashArray = NULL, smoothFactor = 1, noClip = FALSE, #线型参数,平滑度等
popup = NULL, options = pathOptions(), data = getMapData(map)) #弹窗、选项控制
以下构造色盘:
pal <- colorFactor(topo.colors(2),data$Type)
作图函数:使用默认点标识
leaflet()%>% addTiles()%>% addPolylines(data=data1,~lon,~lat,color="blue")%>% addPolylines(data=data2,~lon,~lat,color="green")%>% addMarkers(data=data,~lon, ~lat,popup = ~address)
使用可自定义的圆点标识车站位置
leaflet()%>% addTiles()%>% addPolylines(data=data1,~lon,~lat,color="blue")%>% addPolylines(data=data2,~lon,~lat,color="green")%>% addCircles(data=data,~lon, ~lat,popup = ~address,color="red")
leaflet()%>% addTiles()%>% addPolylines(data=data1,~lon,~lat,color="blue")%>% addPolylines(data=data2,~lon,~lat,color="green")%>% addCircleMarkers(data=data,~lon, ~lat,popup = ~address,color=~Type)
以下是几种比较常用的地图背景素材
addProviderTiles("Esri.WorldStreetMap")
addProviderTiles("Esri.WorldImagery")
addProviderTiles("Esri.WorldTerrain")
addProviderTiles("Esri.NatGeoWorldMap")
leaflet()%>% addProviderTiles("Esri.WorldStreetMap")%>% addPolylines(data=data1,~lon,~lat,color="blue")%>% addPolylines(data=data2,~lon,~lat,color="green")%>% addCircles(data=data,~lon, ~lat,popup = ~address,color=~Type)
leaflet()%>% addProviderTiles("Esri.WorldImagery")%>% addPolylines(data=data1,~lon,~lat,color="blue")%>% addPolylines(data=data2,~lon,~lat,color="green")%>% addCircles(data=data,~lon, ~lat,popup = ~address,color=~Type)
leaflet()%>% addProviderTiles("Esri.WorldGrayCanvas") %>% addPolylines(data=data1,~lon,~lat,color="blue")%>% addPolylines(data=data2,~lon,~lat,color="green")%>% addCircles(data=data,~lon, ~lat,popup = ~address,color=~Type)
leaflet()%>% addProviderTiles("Esri.NatGeoWorldMap") %>% addPolylines(data=data1,~lon,~lat,color="blue")%>% addPolylines(data=data2,~lon,~lat,color="green")%>% addCircles(data=data,~lon, ~lat,popup = ~address,color=~Type)
是不是觉得花样不够多呀,那好吧,以下这些,全部都送给你,那好不谢!
###############################
# OpenStreetMap.Mapnik
# OpenStreetMap.BlackAndWhite
# OpenStreetMap.DE
# OpenStreetMap.France
# OpenStreetMap.HOT
# OpenTopoMap
# Thunderforest.OpenCycleMap
# Thunderforest.Transport
# Thunderforest.TransportDark
# Thunderforest.SpinalMap
# Thunderforest.Landscape
# Thunderforest.Outdoors
# Thunderforest.Pioneer
# OpenMapSurfer.Roads
# OpenMapSurfer.Grayscale
# Hydda.Full
# Stamen.Toner
# Stamen.TonerBackground
# Stamen.TonerLite
# Stamen.Watercolor
# Stamen.Terrain
# Stamen.TerrainBackground
# Stamen.TopOSMRelief
# Esri.WorldStreetMap
# Esri.DeLorme
# Esri.WorldTopoMap
# Esri.WorldImagery
# Esri.WorldTerrain
# Esri.WorldShadedRelief
# Esri.WorldPhysical
# Esri.OceanBasemap
# Esri.NatGeoWorldMap
# Esri.WorldGrayCanvas
# MtbMap
# CartoDB.Positron
# CartoDB.PositronNoLabels
# CartoDB.PositronOnlyLabels
# CartoDB.DarkMatter
# CartoDB.DarkMatterNoLabels
# CartoDB.DarkMatterOnlyLabels
# HikeBike.HikeBike
# HikeBike.HillShading
# NASAGIBS.ModisTerraTrueColorCR
# NASAGIBS.ModisTerraBands367CR
# NASAGIBS.ViirsEarthAtNight2012
# NASAGIBS.ModisTerraLSTDay
# NASAGIBS.ModisTerraSnowCover
# NASAGIBS.ModisTerraAOD
# NASAGIBS.ModisTerraChlorophyll
#################################################
细数也有好几十个,够你玩一阵子了,使用方法仅仅是通过设置图层函数进行调用,然后就可以愉快的在图层上面进行可视化操作了。
addProviderTiles("地图类型")