0

I'm using rCharts Leaflet maps to display polygons on map on R. Using the Leaflet's geoJson I created some polygons and added them to the map. However, those polygons are filled with a default blue color. I'm trying to give them a different color, but no success. For an example, I used the folloeing JSON, tested it in geojson.io and it came up green, however the R package still plots it in blue, how can I enforce the color?

JSON:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "stroke": "#555555",
        "stroke-width": 2,
        "stroke-opacity": 1,
        "fill": "#00f900",
        "fill-opacity": 0.5
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -74.06982421875,
              40.64730356252251
            ],
            [
              -74.06982421875,
              40.79717741518769
            ],
            [
              -73.80615234375,
              40.79717741518769
            ],
            [
              -73.80615234375,
              40.64730356252251
            ],
            [
              -74.06982421875,
              40.64730356252251
            ]
          ]
        ]
      }
    }
  ]
}

R:

jsonx <- (JSON above)
polys = RJSONIO::fromJSON(jsonX)    
map.center <- c(38,-95)
myMap<-Leaflet$new()
myMap$setView(map.center, 4)
myMap$tileLayer(provider = "Esri.WorldGrayCanvas")
myMap$geoJson(polys)
myMap$set(dom = 'myChart2')
myMap
4

1 回答 1

2

虽然实现很好,但基于rChartsRStudio 的包功能更加全面和健壮。如果您可以改用它,这是一个答案。请注意,无需执行任何操作。 将在您的.leaflethtmlwidgetsleafletfillgeoJSON

# uncomment to install the most recent from github
# devtools::install_github("rstudio/leaflet")
#  or older cran #install.packages("leaflet")
library(leaflet)

gj <- '
{
  "type": "FeatureCollection",
  "features": [
  {
  "type": "Feature",
  "properties": {
  "stroke": "#555555",
  "stroke-width": 2,
  "stroke-opacity": 1,
  "fill": "#00f900",
  "fill-opacity": 0.5
  },
  "geometry": {
  "type": "Polygon",
  "coordinates": [
  [
  [
  -74.06982421875,
  40.64730356252251
  ],
  [
  -74.06982421875,
  40.79717741518769
  ],
  [
  -73.80615234375,
  40.79717741518769
  ],
  [
  -73.80615234375,
  40.64730356252251
  ],
  [
  -74.06982421875,
  40.64730356252251
  ]
  ]
  ]
  }
  }
  ]
  }
'

leaflet() %>%
  addTiles() %>%
  setView( -74.1, 40.7, zoom = 10) %>%
  addGeoJSON( gj )


# to show fill works let's change it with gsub

leaflet() %>%
  addTiles() %>%
  setView( -74.1, 40.7, zoom = 10) %>%
  addGeoJSON(
    gsub(
      x = gj
      ,pattern = '(\\"fill\": \\"#00f900\\",)'
      ,replacement = ""
    )
    # demo addGeoJSON fillColor argument
    ,fillColor = 'green'
  )
于 2015-09-01T18:11:37.500 回答