Giter Site home page Giter Site logo

leafletcn's Introduction

leafletCN

CRAN Downloads Build status

leafletCN是一个基于leaflet的**扩展包, 里面保存了一些适用于**的区域划分数据以及一些有帮助的函数, 地理区划数据来源于github的geojson-map-china项目. 数据细分到县级市.

安装

## 稳定版
install.packages("leafletCN")
## 开发版
devtools::install_github("lchiffon/leafletCN")

常用的函数

  • regionNames 返回某个地图的区域名
  • demomap 传入地图名绘制示例地图
  • geojsonMap 将一个分层颜色图绘制在一个实时地图上

其他辅助leaflet包使用的函数

  • amap 在leaflet地图上叠加高德地图
  • read.geoShape 读取一个geojson的对象,保存成spdataframe,以方便leaflet调用
  • leafletGeo用地图名以及一个数据框创建一个sp的对象

基本使用

regionNames

传入需要查看的城市名, 显示这个城市支持的区域信息, 比如查看成都:

regionNames("成都")
[1] "成华区"   "崇州市"   "大邑县"   "都江堰市" "金牛区"  
[6] "金堂县"   "锦江区"   "龙泉驿区" "彭州市"   "蒲江县"  
[11] "青白江区" "青羊区"   "双流县"   "温江区"   "武侯区"  
[16] "新都区"   "新津县"   "邛崃市"   "郫县"    

如果不传入对象, 会自动返回300多个支持的名字列表,包括各个城市,省,以及三个特殊的名字:

  1. world世界地图
  2. china**分省份地图
  3. city**分城市地图

demomap

传入城市名,显示这个城市的示例地图

demomap("**")

<iframe src="examples/demo1.html"></iframe>

geojsonmap

将一个数据框显示在需要展示的地图上. 在函数中做了一些有趣的设置, leafletCN会自动匹配传入的前两个字符来寻找合适的位置进行绘制, 所以基本不需要纠结是写'上海市'还是'上海'了

图做出来可以在上面点点点...

dat = data.frame(name = regionNames("china"),
                 value = runif(34))
geojsonMap(dat,"china")

<iframe src="examples/demo2.html"></iframe>
geojsonmap 的参数
  • 还没开始写噗哈哈, 只写了帮助文档, 求PR

辅助函数

amap

叠加一个高德地图, 使用:

leaflet() %>%
  amap() %>%  
  addMarkers(lng=116.3125774825, lat=39.9707249401, popup="The birthplace of COS")

read.geoShape

read.geoShape这个函数可以把一个geojson格式的数据读取为一个SpatialPolygonsDataFrame对象, 方便sp或者leaflet包中的调用.

if(require(sp)){
  filePath = system.file("geojson/china.json",package = "leafletCN")
  map = read.geoShape(filePath)
  plot(map)
}

leafletGeo

leafletGeo这个函数可以把一个数据框和一个地图组合在一起, 方便用leaflet调用, 其中名字的 变量为name, 数值的变量为value~

if(require(leaflet)){
  dat = data.frame(regionNames("china"),
                                runif(34))
  map = leafletGeo("china", dat)

   pal <- colorNumeric(
     palette = "Blues",
     domain = map$value)

  leaflet(map) %>% addTiles() %>%
     addPolygons(stroke = TRUE,
     smoothFactor = 1,
     fillOpacity = 0.7,
     weight = 1,
     color = ~pal(value),
     popup = ~htmltools::htmlEscape(popup)
     ) %>%
   addLegend("bottomright", pal = pal, values = ~value,
                        title = "legendTitle",
                 labFormat = leaflet::labelFormat(prefix = ""),
                 opacity = 1)
}

例子

十行代码完成空气质量的可视化

leafletcn's People

Contributors

hetong007 avatar lchiffon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

leafletcn's Issues

Title on two lines in addTitle()

Hi, thanks for your work on this package. addTitle really helped me as I don't really know HTML/CSS.

One thing though, if the title is any longer than a few words, the title breaks out into two lines. How can I prevent that from happening? I've found some HTML stuff on StackOverflow but I don't know how to incorporate it into leaflet.

谢谢您!我会汉语,(一点点。。。) 因为我度汉语四年。

更新leafletCN

您好,非常感谢您的leafletCN.

因为我用leafletCN画国内地图时,发现json数据可能比较旧,比如有些城市可能已经改名、像北京和天津这种直辖市早已撤县设区。所以我对leafletCN的数据进行了更新,用[这里的数据(https://github.com/wenccro/chinaMapJsonData)替换了原来的地图数据。

另外还伴随了一些小改动:

  • 修改了leafletcn.map.names与更新后地图数据一致
  • **地图中加入南海诸岛
  • 把自己处理数据的流程写在了data-raw文件夹下,并添加至.buildignore

我修改后的仓库在, 目前通过R CMD check没有warning。

如果您觉得可以的话,没什么问题,我可以提交个PR

画**地图或者省份地图时,能不能只显示它自己不要多余部分?

大为,你好,问个小问题:在画**地图或者省份地图时,能不能只显示它自己不要多余部分?我好像没找到这个参数,原理的话我觉得是将地图数据与要显示的数据先连接一下,然后把没连接上的都去掉就可以了,类似下面这样,然后画出来就可以了

mapdata = merge(地图数据,数据框数据,by='id',all.x = T)
mapdata2 =mapdata[ ia.na(value),]

平时在leaflet上都是这么操作的,但是每次画**地图都要去找一遍地图数据比较麻烦,leafletCN自带数据很方便,希望有空看到回答下~

Examples for 'leafletCN::geojsonMap' is wrong?

dat = data.frame(name = regionNames("china"),
value = runif(34))
geojsonMap(dat,"china")

Error in grepl(paste0(.triList[[5]], .triList[[7]][1]), x) :
regular expression is invalid
In addition: Warning message:
In grepl(paste0(.triList[[5]], .triList[[7]][1]), x) :
unable to translate 'ʡ' to a wide string

Raw Data Check

Function demomap() crashed when i was tried to display the cities' boundary in both "海南省" and “吉林省”.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.