从非highmap-collection地图中制作一个Choropleth [英] Make a choropleth from a non-highmap-collection map
问题描述
我一直在尝试使用highcharter
包中的hcmap
制作一个弦谱图;我从自己的shapefile中获取了多边形,因为它是不在highmap集合列表中的地图.
I've been trying to make a choropleth map with hcmap
from highcharter
package; I obtained the polygons from my own shapefile because it's a map that is not on the list of highmap's collection.
为此,首先,我设法将我的shapefile转换为GeoJson文件,如下所述: https://blog.exploratory.io/creating- geojson-out-of-shapefile-in-r-40bc0005857d
To do so, first I managed to transform my shapefile to a GeoJson file, as described here: https://blog.exploratory.io/creating-geojson-out-of-shapefile-in-r-40bc0005857d
稍后,我设法使用包geosonio
绘制了地图,如下所示:
http://jkunst.com/highcharter/highmaps.html#geojsonio-package
Later I managed to draw the map using the package geosonio
as described here:
http://jkunst.com/highcharter/highmaps.html#geojsonio-package
但是,我不知道如何将具有值的数据框合并到地图中绘制的多边形中.所有可用的示例都将合并为data.frame格式的mapdata,在转换为GeoJson时会丢失.
However, I can't figure out how to merge a dataframe with values into the polygons drawn in my map. All the examples availables are merging to mapdata that is in a data.frame format, which I lose when transforming to GeoJson.
到目前为止,这是我的代码:
Here's my code so far:
library(rgdal)
library(geojsonio)
library(highcharter)
#Get map from shapefile
Mymap <- readOGR(dsn="Mymap", "Mymap", verbose = FALSE) %>%
spTransform(CRS("+proj=longlat +ellps=GRS80 +datum=WGS84"))
#Transform to geoJson
MymapJSON <- geojson_json(Mymap)
#Use geojsonio to make data compatible with hcmap
Myhcmap <- jsonlite::fromJSON(MymapJSON, simplifyVector = FALSE)
Myhcmap<- geojsonio::as.json(Myhcmap)
#Draw map:
highchart(type = "map") %>%
hc_add_series(mapData = Myhcmap, showInLegend = T)
结果:
¿我如何将其他数据放入GeoJson中,以便画出一个圆度?
¿How can I put additional data into the GeoJson so I can draw a choropleth?
推荐答案
前段时间我终于找到了自己的解决方案,这相当简单,但是由于没有充分说明如何向GeoJSON添加数据,因此我将展示在这里:
I finally got to a solution by myself some time ago, it's was fairly simple but since it's not well documented how to add data to the GeoJSON, I will show it here:
#Work with the map until this step:
Myhcmap <- jsonlite::fromJSON(MymapJSON, simplifyVector = FALSE)
#This part was unnecessary:
#Myhcmap<- geojsonio::as.json(Myhcmap)
#Then, write your map like this:
highchart() %>%
hc_add_series_map(Myhcmap, df, value = "value", joinBy = "ID")
位置:
df
是要添加的数据框
value
是您要为地图着色的数据的列名
value
is the column name of the data you want to color your map by
joinBy
是连接键变量
这篇关于从非highmap-collection地图中制作一个Choropleth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!