【问题标题】:Legends on R Leaflet MapsR传单地图上的图例
【发布时间】:2015-04-16 19:53:31
【问题描述】:

虽然我意识到使用 Rstudio leaflet package 在地图上添加图例仍在进行中,但我一直在尝试将图例 事后 添加到 R 生成的 HTML 中。

library("leaflet")
set.seed(100)
pdf <- data.frame(Latitude = runif(100, -90,90), Longitude = runif(100, -180,180), 
                  col=rep(c("red", "blue"), 50 ))

#just red
leaflet(pdf) %>% addTiles()  %>%
  addCircleMarkers(lat = ~ Latitude, lng = ~ Longitude, color= ~col)

我一直在尝试修改来自http://leafletjs.com/examples/choropleth.html 的代码,并通过在 R 中运行上述代码并将其转换为 HTML 来找出将其添加到输出的位置。

所以就像将以下内容放在 html 的正文中:

<script>
var legend = L.control({position: 'bottomright'});

legend.onAdd = function (map) {

    var div = L.DomUtil.create('div', 'info legend'),
        grades = [red, blue],
        labels = [];

    // loop through our density intervals and generate a label with a colored square for each label
    for (var i = 0; i < grades.length; i++) {
        div.innerHTML +=
            '<i style="background:' + getColor(grades[i] + 1) + '"></i> ' +
            grades[i] + (grades[i + 1] ? '&ndash;' + grades[i + 1] + '<br>' : '+');
    }

    return div;
};

legend.addTo(map);
<script>

但是,这似乎不起作用。什么都没有弹出。也不清楚我会如何使用除“红色”和“蓝色”以外的名称来表示成绩。我还添加了 CSS,如 choropleth 示例中所示,但没有骰子。

有没有人这样做 - 手动将图例添加到他们的 R 输出(例如,在发布后从 Rpubs 获取源)以添加图例?

【问题讨论】:

  • 注意这个包是由 RStudio 的人写的,但它只是普通的 R。
  • 是的 - 我说 Rstudio 传单包是为了将它与其他生成传单代码的 R 包区分开来。

标签: r leaflet


【解决方案1】:

虽然这不是我想要的,但似乎 addLegend 函数正在开发中,并且位于包的一个分支中。在此处查看一些文档和示例:

http://smartinsightsfromdata.github.io//2015/04/25/choropleths.html

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-21
  • 2021-01-19
相关资源
最近更新 更多