【问题标题】:rMaps ichoropleth with custom map/shp使用自定义地图/shp 映射 choropleth
【发布时间】:2015-06-17 10:41:07
【问题描述】:

在 R 中,我希望帮助我尝试将教程 here 复制为我自己的自定义 SHP(Shapefile)文件或地图,使其成为交互式 choropleth 地图...

该地图是北爱尔兰小区域的地图。可以找到here。

以下是我到目前为止所采取的步骤...

我认为问题在于geographyConfig 数据的设置...

任何帮助将不胜感激......

# Download and unzip the data
system('wget http://www.nisra.gov.uk/archive/geography/digital_products/SA2011_Esri_Shapefile.zip')
system('unzip SA2011_Esri_Shapefile.zip')

# Load libraries
library(rgdal)
library(rgeos)
library(rMaps)

shp.file <- 'SA2011.shp'

# Convert projection
system(paste0('ogr2ogr tmp.shp ',
               shp.file,
              ' -t_srs "+proj=longlat +ellps=WGS84 +no_defs +towgs84=0,0,0"'))

# Read in the data
xx <- readOGR(dsn=getwd(),layer='tmp')
mm <- xx@data
head(mm)
n <- nrow(mm)
dat.val <- mm$Hectares

# Add extra year data
mm <- mm[rep(seq(n),3),]
mm$Hectares <- c(dat.val,rev(dat.val),dat.val/2)
mm$year <- rep(c(2000:2002),each=n)
colnames(mm)[1] <- 'ID'
id.var <- 'SA2011'


# Convert to json
system(paste0('topojson -o tmp.json -s 1e-7 -q 1e5 tmp.shp -p ID=',
              id.var,
              ' --id-property ',
              id.var))

d1 <- ichoropleth(Hectares ~ ID, data = mm, ncuts = 9, pal = 'YlOrRd', 
                  animate = 'year',  map = 'states'
)
d1$set(
  geographyConfig = list(
    dataUrl = "tmp.json"
  ),
  scope = 'states',
  setProjection = '#! function( element, options ) {
  var projection, path;
  projection = d3.geo.mercator()
  .center([-7, 55]).scale(element.offsetWidth)
  .translate([element.offsetWidth / 2, element.offsetHeight / 2]);

  path = d3.geo.path().projection( projection );
  return {path: path, projection: projection};
  } !#'
)
d1$save('rMaps.html', cdn = TRUE)

加载rMaps.html 不会生成相关地图,因为它只显示底部的切口而不显示地图。

【问题讨论】:

  • 加载 rMaps.html 不会生成相关地图,而只会生成底部,因为它会触发 JavaScript 错误,这可以在 Chrome 中的 JavaScript 控制台或 Safari 中的 Web Inspector 中看到。我不知道是什么导致了这些 JavaScript 错误。

标签: r d3.js rcharts


【解决方案1】:

你见过http://www.r-bloggers.com/rmaps-mexico-map/吗? 我遇到了类似的问题,发现对于小规模区域,您需要在下面的代码中操作 scale 参数。它确实有效。

    d1$set(
  geographyConfig = list(
   dataUrl = "tmp.json"
  ),
 scope = 'tmp',
 setProjection = '#! function( element, options ) {
   var projection, path;
   projection = d3.geo.mercator()
    .center([-5.832087, 54.605035]).scale(element.offsetWidth)
    .translate([element.offsetWidth / 2, element.offsetHeight / 2]);

   path = d3.geo.path().projection( projection );
   return {path: path, projection: projection};
  } !#'
)
d1$save('rMaps.html', cdn = TRUE)

【讨论】:

    【解决方案2】:

    recordnotfound.com 上的各种人要求作者回复此线程(在 recordnotfound.com 上)。我与 rMaps 项目的所有者 Ramnath 取得了联系。他建议有一个提供增强功能集的新项目:http://github.com/rstudio/leaflet

    这里有更多关于传单项目的详细信息:https://recordnotfound.com/leaflet-rstudio-35205

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多