使用R创建传单地图

 传单R包

嘿!

Today I would like to introduce you to the leaflet package for R. This package makes it easy to integrate and control 传单 地图 in R. In other words: You can create beautiful 传单 地图 with your 数据 from inside R, without any knowledge of 的JavaScript , HTML, CSS, etc…听起来很棒,对吧?这是这样的…
首先让’s prepare some 数据 :

library(raster)
#get boundaries
usa <- getData("GADM", country="USA", level=2)
#add random  数据 
usa$randomData <- rnorm(n=nrow(usa), 150, 30)

The fastest way to get to some arbitrary 数据 , is to load the 栅格 package and download administrative boundaries of a country of your choice using the getData() function. Note that country boundaries come without any additional 数据 (like population, income, etc..). Since we later want to visualise some 数据 on our map using some colors and pop-ups, I added a new column to the SpatialPolygonsDataFrame and filled it with random 数据 using the rnorm function.

创建地图

创建包含传单地图的HTML页面的最简单方法是使用RMarkdown。为此,请转到新文件-> RMarkdown ->HTML。最终的RMarkdown文档如下所示:

---
title: " 传单  with R 101"
output: html_document
---

```{r, echo=F, warning=F, message=F}
#load leaflet package for R
library(leaflet)
library(raster)

#arbitrary  数据  (code from above)
usa <- getData("GADM", country="USA", level=2)
usa$randomData <- rnorm(n=nrow(usa), 150, 30)

#create a color palette to fill the polygons
pal <- colorQuantile("Greens", NULL, n = 5)

#create a pop up (onClick)
polygon_popup <- paste0("<strong>Name: </strong>", usa$NAME_1, "<br>",
                        "<strong>Indicator: </strong>", round(usa$randomData,2))

#create leaflet map
  map = leaflet() %>% 
      addProviderTiles("CartoDB.Positron") %>% 
      setView(-98.35, 39.7,
              zoom = 4) %>% 
      addPolygons(data = usa, 
                  fillColor= ~pal(randomData),
                  fillOpacity = 0.4, 
                  weight = 2, 
                  color = "white",
                  popup = polygon_popup)
map
```

如您所见,代码很短而且很直观:

leaflet() initialises a new leaflet map, tiles/basemape can be added using the addTiles() or addProviderTiles() functions. In my example I included a basemap provided by CartoDB called “Positron”. Once you set the view and the zoom level using the setView() function, you can overlay your 数据 with the addPolygons() function. And that’s it! Click on KnitHTML and view the final result in your browser:

 传单R包

I hope you enjoyed this rather short introduction into the very powerful leaflet package for R. If you are intrested in more detailed tutorials or code examples, please leave a comment below or visit this official R的传单– Introduction.

干杯

马丁

 

关于作者

马丁出生于捷克共和国,就读于维也纳自然资源与生命科学大学。他目前在GeoVille(一家位于奥地利的地球观测公司)工作,专门从事土地监测。他的主要兴趣是:开源应用程序,例如R,(地理空间)统计和数据管理,Web映射和可视化。他喜欢旅行,寻宝,摄影和运动。

5条留言

您可以在这篇文章中发表评论。


  • 马丁,您也有一个很棒的博客,其中有很好的文章。喜欢的传单映射。谢谢你的分享。我一定会跟随你的。我也像你一样在读硕士学位。

    Pramod Mishra 6年前 回复


  • 马丁,这非常有帮助。您知道我们如何在弹出窗口中插入超链接。我有一列带有超链接标记的列,并希望将其放在弹出窗口中。例如,如果您能将姓名链接到某个网站?感谢您的帮助。

    鲁兹 5年前 回复


    • 不幸的是我可以’在注释中插入任何代码。
      但是,如果您给我发送电子邮件: martin@gis-blog.com ,我将向您发送您所需的代码段。

      给马丁加油

      马丁 5年前 回复


  • 嗨,马丁,

    我正在尝试使用简单的传单地图生成HTML页面,但该地图无法呈现。您能否告诉我是否需要执行其他步骤?我使用的代码如下:


    标题: “Leaflet Map”
    输出:
    html_document:
    独立的:否

    “`{r results=’asis’, echo = FALSE}
    图书馆(传单)
    rand_lng =函数(n = 10)rnorm(n,-93.65,.01)
    rand_lat =函数(n = 10)rnorm(n,42.0285,.01)
    m = leaflet()%>% addTiles() %>%addCircles(rand_lng(50),rand_lat(50),radius = runif(50,10,200))
    m

    千伏 5年前 回复


    • 我尝试了您的代码(稍作调整– I edited the “在{}大括号前面,效果很好。
      您收到什么错误消息?


      标题:“传单地图”
      输出:
      html_document:
      独立的:否

      ““ {r results =” 阿西斯”,echo = FALSE}
      图书馆(传单)
      rand_lng =函数(n = 10)rnorm(n,-93.65,.01)
      rand_lat =函数(n = 10)rnorm(n,42.0285,.01)
      m = leaflet()%>%addTiles()%>%addCircles(rand_lng(50),rand_lat(50),radius = runif(50,10,200))
      m

      马丁 5年前 回复


发表回复

*