rMaps ichoropleth with custom map/shp
Asked Answered
M

2

46

In R, I would like help in trying to replicate the tutorial here for my own custom SHP (Shapefile) file or map to be an interactive choropleth map...

The map is of the Small Areas of Northern Ireland. Which can be found here.

Below are the steps I have taken so far...

I think the issue is the setting of the geographyConfig data...

Any help would be much appreciated....

# 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)

Loading rMaps.html does not produce the relevant map, as it only shows the cuts at the bottom but not the map.

Microgram answered 11/4, 2015 at 21:1 Comment(1)
Loading rMaps.html does not produce the relevant map but only the bottom section because it triggers JavaScript errors as can be seen in the JavaScript Console in Chrome, or the Web Inspector in Safari. I have no idea what causes these JavaScript errors.Cutwater
H
1

Various people on recordnotfound.com asked the author to reply to this thread (on recordnotfound.com). I got in touch with the owner of the rMaps project, Ramnath. He suggested that there is a new project that provides an enhanced feature set: http://github.com/rstudio/leaflet

Here are more details about the leaflet project: https://recordnotfound.com/leaflet-rstudio-35205

Hydrostat answered 11/2, 2016 at 4:44 Comment(2)
Add more details to your answerCochabamba
Leaflet is awesome. This is a good place to start looking into leaflet #29261185Nonappearance
I
0

Have you seen http://www.r-bloggers.com/rmaps-mexico-map/? I was having a similar problem and found that for small scale areas your need to manipulate scale argument in the bit of code below. It does work.

    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)
Incommunicable answered 24/6, 2016 at 0:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.