【问题标题】:Leaflet on R: How to create layers and colors for each factor level in dataframeR 上的传单:如何为数据框中的每个因子级别创建图层和颜色
【发布时间】:2016-03-10 16:08:10
【问题描述】:

(顺便说一句,还有其他软件包可以满足我的要求吗?)

这是我的数据集:

> head(df)
    id groupID       lat       lon
511  1  277706 -10.89834 -37.05893
377  2  277706 -10.88870 -37.07695
98   3  277705 -10.89050 -37.09406
392  4  277697 -10.92131 -37.10525
6    5  277705 -10.89050 -37.09406
364  6  277697 -10.93730 -37.11600

我想使用传单根据纬度和经度在地图上绘制每一行。此外,每个标记(或弹出窗口或 CircleMarker)都应该根据 groupID 变量具有不同的颜色和图层。

问题是数据每天都在变化,我不知道 groupID 变量会有多少个不同的唯一级别。在此示例数据集中有 12 个级别,但范围可能从 5 到 30 左右不等。documentation 中的示例使用预定义的级别数。

这是我尝试过的:

colorsmap = colors()[1:length(unique(df3$groupID))]
groupColors = colorFactor(palette = "RdYlBu", domain = df3$groupID)

leaflet(data = df3) %>%
  addTiles() %>%
  addCircleMarkers(lng = ~lon, lat = ~lat, color = ~groupColors(groupID),
                   group = ~groupID) %>%
  #addLegend(position = "topright", pal = groupColors, values = ~groupID) %>%
  addLayersControl(~groupID)

它确实提供了一个情节,但是当我只选择一个级别时,其他的并没有像他们应该的那样消失:

图片:

数据集本身:

> dput(df3)
structure(list(id = 1:20, groupID = c(277698L, 277715L, 277704L, 
277706L, 277705L, 277705L, 277719L, 277705L, 277705L, 277709L, 
277706L, 277705L, 277704L, 277706L, 277715L, 277702L, 277719L, 
277706L, 277715L, 277706L), lat = c(-10.8172615660655, -10.8904055150991, 
-10.8887597563482, -10.90203509, -10.9001514, -10.8997748900025, 
-10.8960177351343, -10.8896179908615, -10.8991450456802, -10.9224848475651, 
-10.9000373151094, -10.8905013650562, -10.8889438100208, -10.9001234797436, 
-10.8861897462579, -10.9326053452642, -10.8916601751623, -10.902075281944, 
-10.8822231928033, -10.9079483812524), lon = c(-36.9248145687343, 
-37.0665064455395, -37.0921721937304, -37.05829295, -37.0969278, 
-37.0976847916125, -37.0840372102666, -37.0963566353117, -37.0945971936751, 
-37.0549293249471, -37.066113628594, -37.0940632483155, -37.095505683692, 
-37.0590422449149, -37.0782556623101, -37.0698746017798, -37.0841003949028, 
-37.0593363285999, -37.0724709841895, -37.0817244836096)), .Names = c("id", 
"groupID", "lat", "lon"), row.names = c(20L, 23L, 8L, 36L, 14L, 
13L, 16L, 2L, 11L, 1L, 26L, 6L, 5L, 31L, 22L, 50L, 17L, 34L, 
25L, 42L), class = "data.frame")

【问题讨论】:

    标签: r leaflet


    【解决方案1】:
    library(leaflet)
    
    groups = as.character(unique(df3$groupID))
    
    map = leaflet(df3) %>% addTiles(group = "OpenStreetMap")
    for(g in groups){
      d = df3[df3$groupID == g, ]
      map = map %>% addCircleMarkers(data = d, lng = ~lon, lat = ~lat, 
                                     color = ~groupColors(groupID),
                                     group = g)
    
    }
    map %>% addLayersControl(overlayGroups = groups)
    

    【讨论】:

    • 有没有办法在复选框中对组进行排序?例如复选框 '277698' 先出现,然后是 '277702',.... '277719'(从小到大)?
    • 尝试使用因子并根据需要设置级别。
    【解决方案2】:

    为了完整起见,现在可以使用 ma​​pview 轻松实现,同时使用 zcolburst 参数:

    library(mapview)
    library(sp)
    
    df3 <- structure(list(id = 1:20, 
                          groupID = c(277698L, 277715L, 277704L, 
                                      277706L, 277705L, 277705L, 
                                      277719L, 277705L, 277705L, 277709L, 
                                      277706L, 277705L, 277704L, 277706L, 
                                      277715L, 277702L, 277719L, 
                                      277706L, 277715L, 277706L), 
                          lat = c(-10.8172615660655, -10.8904055150991, 
                                  -10.8887597563482, -10.90203509, -10.9001514, 
                                  -10.8997748900025, -10.8960177351343, 
                                  -10.8896179908615, -10.8991450456802, 
                                  -10.9224848475651, -10.9000373151094, 
                                  -10.8905013650562, -10.8889438100208, 
                                  -10.9001234797436, -10.8861897462579, 
                                  -10.9326053452642, -10.8916601751623, 
                                  -10.902075281944, -10.8822231928033, 
                                  -10.9079483812524), 
                          lon = c(-36.9248145687343, -37.0665064455395, 
                                  -37.0921721937304, -37.05829295, -37.0969278, 
                                  -37.0976847916125, -37.0840372102666, 
                                  -37.0963566353117, -37.0945971936751, 
                                  -37.0549293249471, -37.066113628594, 
                                  -37.0940632483155, -37.095505683692, 
                                  -37.0590422449149, -37.0782556623101, 
                                  -37.0698746017798, -37.0841003949028, 
                                  -37.0593363285999, -37.0724709841895, 
                                  -37.0817244836096)), 
                     .Names = c("id", "groupID", "lat", "lon"), 
                     row.names = c(20L, 23L, 8L, 36L, 14L, 13L, 16L, 2L, 11L, 
                                   1L, 26L, 6L, 5L, 31L, 22L, 50L, 17L, 34L, 25L, 42L), 
                     class = "data.frame")
    
    ## convert df3 to spatialPointsDataFrame for use with mapview
    coordinates(df3) <- ~ lon + lat
    proj4string(df3) <- "+init=epsg:4326"
    
    ## now burst column "groupID"
    mapview(df3, zcol = "groupID", burst = TRUE)
    

    注意:这目前仅在 ma​​pview 的开发版本中可用,可以安装

    devtools::install_github("environmentalinformatics-marburg/mapview", ref = "develop")
    

    【讨论】:

      【解决方案3】:

      我知道这个问题有点老了,并且已经给出了解决方案。但我最近遇到了同样的问题,发现了一个快速肮脏的黑客。如果您仅有效地使用层控件与一个因素的级别,您将有单选按钮一次仅显示一个级别:

      addLayersControl(overlayGroups = data$factor)
      

      但是,如果您只是从整个图层中添加另一个图层控件,您将同时激活所有组(包括因子的每个级别):

      addLayersControl(overlayGroups = c(data$factor, "layer name"))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-10
        • 1970-01-01
        • 1970-01-01
        • 2014-12-11
        • 2016-06-01
        • 2013-11-26
        相关资源
        最近更新 更多