R Legend中的GeoJSON

时间:2018-02-27 21:12:55

标签: r geojson

我尝试根据属性创建每个地理区域的特定颜色的传单图。这是我的geojson文件的测试示例: 我对geojson和传单一般都是新手

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {"class": "blah"},
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -81.7987060546875,
              32.74570253945518
            ],
            [
              -81.6229248046875,
              32.16631295696736
            ],
            [
              -80.958251953125,
              32.4263401615464
            ],
            [
              -81.2713623046875,
              32.791892438123696
            ],
            [
              -81.7437744140625,
              32.97180377635759
            ],
            [
              -81.7987060546875,
              32.74570253945518
            ]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {"class": "blah2"},
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -82.056884765625,
              33.55512901742288
            ],
            [
              -81.4471435546875,
              33.247875947924385
            ],
            [
              -81.40869140625,
              33.80653802509606
            ],
            [
              -82.078857421875,
              33.88865750124075
            ],
            [
              -82.40295410156249,
              33.58716733904656
            ],
            [
              -82.056884765625,
              33.55512901742288
            ]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {"class": "blahh3"},
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -83.485107421875,
              32.930318199070534
            ],
            [
              -83.07861328125,
              31.863562548378965
            ],
            [
              -82.21618652343749,
              32.11049589629439
            ],
            [
              -82.97973632812499,
              33.22030778968541
            ],
            [
              -83.726806640625,
              33.211116472416855
            ],
            [
              -83.485107421875,
              32.930318199070534
            ]
          ]
        ]
      }
    }
  ]
}

我想要做的是根据类属性添加图例,以便每个不同的类与特定颜色相关联。但是,当我尝试绘制并添加图例时,我在绘图上出现错误。以下是我的R代码:

blahTest <- geojson_read("/file/path/...", what = "sp")
fpal <- colorFactor("viridis", blahTest$class)
leaflet(blahTest) %>%
  addTiles() %>%
  addPolygons(stroke = FALSE, smoothFactor = 0.3, fillOpacity = 1,
              color = ~fpal(class)) %>%
  addLegend(colors = ~fpal, opacity = 1.0, labels = ~fpal)

1 个答案:

答案 0 :(得分:1)

对addLegend参数稍作修改即可获得所需的输出:

# read geojson in r
library(rgdal)
blahTest = readOGR("...", "OGRGeoJSON")

library(leaflet)

# create palette
fpal <- colorFactor(
  palette = "viridis",
  domain = blahTest$class
)

# plot
leaflet(blahTest) %>%
  addTiles() %>%
  addPolygons(stroke = FALSE, smoothFactor = 0.3, fillOpacity = 1,
              color = ~pal(class)) %>%
  addLegend("bottomright", 
            pal = fpal, 
            values = ~class,
            title = "Legend",
            opacity = 1,
            labels = ~fpal
)

GeoJson legend