Open greentheo opened 10 years ago
which btw I'd be happy to add in the polygonal functionality to the leaflet R code.
I'm working on this as well and am having my own problems, hopefully I can piggyback on this issue and solve both our problems.
I downloaded county-level .shp map files at http://wsgw.mass.gov/data/gispub/shape/state/counties.zip then proceeded to imitate the Mexican Crime and Finnish examples in other issues. The terminal is probably where I made my mistakes (I'd like to know more about these functions), here's what I did:
ogr2ogr states.shp COUNTIES_POLYM.shp -t_srs "+proj=longlat +ellps=WGS84 +no_defs +towgs84=0,0,0"
## I really don't know what much of this code means, but the longitude and latitudes seem to be fairly accurate
topojson -o mx_states.json -p states.shp state_code=+OBJECTID,name=COUNTY --id-property COUNTY
## Same with this code. OBJECTID are the ID numbers for the counties (numbers represent the alphabetical rank of each county). It's unclear what I should have put there from the other examples
_I also realize that calling a map of MA counties mx_states and naming the counties by statecode is confusing, but I figured if I can get it right once, then I can worry about changing the names next time
This isn't my real data - which is publicly available, but requires a fair amount of cleaning - but I think it should still work for these purposes. This is my R code:
require(maptools)
require(foreign)
MAmap <- readShapeSpatial("countymaps/COUNTIES_POLYM.shp")
df <- data.frame(County = MAmap$COUNTY, Crude.Rate = seq(1,14))
codes <- read.dbf(file = "~/Desktop/countymaps/states.dbf")[,c(1,3)]
codes$COUNTY <- iconv(codes$COUNTY, "latin1", "utf-8")
codes$OBJECTID <- as.numeric(codes$OBJECTID)
names(codes) <- c("state_code", "name")
df2 <- merge(codes, df, by.x = "name", by.y = "County")
# I tried using plyr's join function to no avail
d1 <- ichoropleth(Crude.Rate ~ name, data = df2, ncuts=6, pal = "YlOrRd", map = 'states')
d1$set(
geographyConfig = list(
dataUrl = "~/Desktop/countymaps/mx_states.json"),
scope = 'states',
setProjection = '#! function( element, options ) {
var projection, path;
projection = d3.geo.mercator()
.center([-72, 42]).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)
d1
I think that -72, 42 should be about center, or at least close enough that the map would show up. But instead I get the appropriate legend below a blank map (no outlines of states or colors or anything).
I hope that if we can figure out how to make an iChoropleth map for the counties of one state, we can figure it out for all of the states.
I would like to ditto the above users. I have tried for the last few days to replicate the Mexican state map using Virginia county data. I completely replicated Mexico, but when I do my Virginia map, I get the blank screen (data legend is visible so I know that it is at some level registering).
Question: considering the part of the code " the map = 'state' ..... scope = 'state' " What should those be if I'm using: 1) county data, 2) my json = vamap.json, 3) my original shp/dbf = vam2.shp/.dbf
Also: does it matter if Mexico_MXstate or US_State_County? I was assuming that it simply registered the shapes rather than the places.
Wonderful work on this package...looking forward to your next revision!
Curious to know if the ichoropleth utility (datamaps) supports county level data. And if so can you give an example?
If not, then using leaflet, is it possible to draw polygons and fill them with a color?
I currently use ggplot and the maps package to draw out the boundaries of counties (and would love to do Township level geo data as well), and could apply the same technique to a leaflet map if I could draw filled polygons.
I suppose it would also open leaflet maps up to making heatmaps pretty easily with R too...