R语言之数据可视化---地图可视化leaflet包(下)

leaflet1.png

包的github链接地址:https://github.com/rstudio/leaflet
基于leaflet的中文扩展包出自chiffon大大github地址:https://github.com/Lchiffon/leafletCN

一.安装方式:

可见作者关于安装的文章

二.使用方法:

上篇中主要讲述了leaflet包的一些使用方法,但对于中国的R玩家来讲还不是很方便,这篇文章来给大家介绍一下chiffon大大写的一个leafletCN包。

  • regionNames:返回某个地图的区域名

regionNames("苏州")
[1] "常熟市" "虎丘区" "昆山市" "太仓市" "吴江区" "相城区" "张家港市"
[8] "吴中区" "姑苏区"

  • demomap 传入地图名绘制示例地图(只能传入城市名)

demomap("苏州")


R语言之数据可视化---地图可视化leaflet包(下)_第1张图片
leaflet.png
  • geojsonMap 将一个分层颜色图绘制在一个实时地图上

dat = data.frame(name = regionNames("china"),value = runif(34))
value的值来控制颜色的深浅
dat$value2 = cut(dat$value, c(0, 0.25, 0.5, 1))
geojsonMap(dat,"china",namevar = ~name, valuevar = ~value2,palette="Reds",
colorMethod="factor")

  • amap 在leaflet地图上叠加高德地图,结合leaflet和baidumap使用

leaflet() %>%
amap() %>%
addMarkers(lng=120.77093, lat=31.596020, popup="willnight")

  • 小实例:暗黑风格(帅帅的)(实例url:http://xwj.565tech.com/jianshu/leaflet22.html )
    library(baidumap)
    data1=getCoordinate(c("同济科技广场","常熟理工东南校区","常熟理工东湖校区"),formatted = T)
    data1<-as.data.frame(data1)
    myIcon = makeAwesomeIcon(icon = "home", library = "glyphicon",
    markerColor = "blue", iconColor = "white", spin = FALSE,
    extraClasses = NULL)
    dat1<-data.frame(lon=data1$longtitude,lat=data1$latitude)
    leaflet(dat1) %>%
    setView(lng=121.48024, lat=31.23631, zoom=5) %>%
    addProviderTiles("Thunderforest.SpinalMap") %>%
    addAwesomeMarkers(icon=myIcon)
leaflet1.png
  • 又一个小实例:(实例url:http://xwj.565tech.com/jianshu/leaflet24.html )
    library(baidumap)
    data1=getCoordinate(c("苏州","上海","常州"),formatted = T)
    data1<-as.data.frame(data1)
    dat1<-data.frame(lon=data1$longtitude,lat=data1$latitude)
    iconList = awesomeIconList(
    "home" = makeAwesomeIcon(icon = "home",markerColor = "skyblue"),
    "bank" = makeAwesomeIcon(icon = "plus-sign",markerColor = "orange"),
    "coffee" = makeAwesomeIcon(icon = "book")
    )
    dat1$type<-c("home", "bank" ,"coffee" )
    leaflet(dat1) %>% addProviderTiles("CartoDB.Positron") %>% addAwesomeMarkers(icon = ~iconList[type],popup="willnight")
R语言之数据可视化---地图可视化leaflet包(下)_第2张图片
leaflet3.png

好了leaflet包就介绍到这里,基本上配合baidumap包拿到坐标数据,再配合leaflet的主题设置,就可以绘制出很多漂亮的地图,以后的文章会介绍其他可视化的包。

部分参考chiffon大大:https://github.com/Lchiffon/leafletCN

你可能感兴趣的:(R语言之数据可视化---地图可视化leaflet包(下))