在R中使用Leaflet的多个`addCircleMarkers`图层? [英] Multiple `addCircleMarkers` layers using Leaflet in R?

查看:491
本文介绍了在R中使用Leaflet的多个`addCircleMarkers`图层?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R中使用Leaflet时,我认为绘制图层(ala ggplot)将是有效的:

When using Leaflet in R, I assumed that plotting layers (ala ggplot) would be effective:

 m <- leaflet() %>%
      addTiles() %>%
      addCircleMarkers(lat=subset(DF, outcome=='W')$lat, lng=subset(DF, outcome=='W')$lon, color= "red") %>%
      addCircleMarkers(lat=subset(DF, outcome=='L')$lat, lng=subset(DF, outcome=='L')$lon, color= "blue") 

我以为这会给我两个不同的彩色圆圈标记,红色标记表示具有"W"结果的记录,蓝色标记则是具有"L"结果的记录.

I had assumed that this would give me two different colored circle markers, red markers for those records that had 'W' outcomes, and blue markers for records that had 'L' outcomes.

相反,我根本没有任何地图.

Instead, I don't get any map at all.

如何使用R中的Leaflet依次传送多个addCircleMarkers?

How can I pipe multiple addCircleMarkers in sequence using Leaflet in R?

推荐答案

管道很简单.以下代码对我有用.

Pipelining is straight forward. The following code works for me.

leaflet() %>% 
  addTiles() %>% 
  addCircleMarkers(lng = 9, lat = 47, color = 'red') %>% 
  addCircleMarkers(lng = 8.5, lat = 47.5, color = 'blue')

您的示例代码也可以在示例数据框架中正常工作

Also your example code works fine with a sample data frame:

DF <- data.frame(lat = c(47,48), lon = c(8,9), outcome = c("W", "L"))
leaflet() %>%
  addTiles() %>%
  addCircleMarkers(
    lat=subset(DF, outcome=='W')$lat, lng=subset(DF,outcome=='W')$lon, 
    color= "red") %>%
  addCircleMarkers(
    lat=subset(DF, outcome=='L')$lat, lng=subset(DF, outcome=='L')$lon, 
    color= "blue")

这给出了下面的图

这篇关于在R中使用Leaflet的多个`addCircleMarkers`图层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆