rCharts GeoJSON - 更改多边形的填充颜色
rCharts GeoJSON - Change fill color of polygons
我正在使用 rCharts Leaflet 地图在 R 上的地图上显示多边形。
使用 Leaflet 的 geoJson 我创建了一些多边形并将它们添加到地图中。但是,这些多边形填充有默认的蓝色。我试图给他们不同的颜色,但没有成功。
例如,我使用了下面的 JSON,在 geojson.io 中对其进行了测试,结果显示为绿色,但是 R 包仍将其绘制为蓝色,我如何强制执行颜色?
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
]
]
]
}
}
]
}
回复:
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
虽然 rCharts
的实现很好,但 RStudio 的 leaflet
包基于 htmlwidgets
的功能更加全面和强大。如果您可以改用它,这里有一个答案。请注意,无需执行任何操作。 leaflet
将在您的 geoJSON
中选择 fill
。
# 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'
)
我正在使用 rCharts Leaflet 地图在 R 上的地图上显示多边形。 使用 Leaflet 的 geoJson 我创建了一些多边形并将它们添加到地图中。但是,这些多边形填充有默认的蓝色。我试图给他们不同的颜色,但没有成功。 例如,我使用了下面的 JSON,在 geojson.io 中对其进行了测试,结果显示为绿色,但是 R 包仍将其绘制为蓝色,我如何强制执行颜色?
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
]
]
]
}
}
]
}
回复:
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
虽然 rCharts
的实现很好,但 RStudio 的 leaflet
包基于 htmlwidgets
的功能更加全面和强大。如果您可以改用它,这里有一个答案。请注意,无需执行任何操作。 leaflet
将在您的 geoJSON
中选择 fill
。
# 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'
)