Skip to content

Instantly share code, notes, and snippets.

@hrbrmstr
Created December 24, 2014 17:54
Show Gist options
  • Save hrbrmstr/7ba57441dffac9a9126b to your computer and use it in GitHub Desktop.
Save hrbrmstr/7ba57441dffac9a9126b to your computer and use it in GitHub Desktop.
foralex
library(rgdal)
library(rgeos)
library(maptools)
library(mapproj)
us <- readOGR("http://bl.ocks.org/mbostock/raw/4090846/us.json", "states")
proj4string(us) <- CRS("+proj=merc")
map <- fortify(us)
gg <- ggplot()
gg <- gg + geom_map(data=map, map=map,
aes(x=long, y=lat, map_id=id, group=group),
fill="#ffffff", color="#0e0e0e", size=0.15)
gg <- gg + xlim(-124.848974, -66.885444)
gg <- gg + ylim(24.396308, 49.384358)
gg <- gg + coord_map(projection="albers", lat=39, lat1=45)
gg <- gg + labs(x=NULL, y=NULL, title=NULL)
gg <- gg + theme(legend.position="bottom")
gg <- gg + theme(legend.key=element_blank())
gg <- gg + theme(plot.title=element_text(size=16))
gg
@hrbrmstr
Copy link
Author

Most of the TopoJSON files I've seen with D3 maps seem to have no projection associated with them. This reads it in from the URL (I picked the "states" feature simply since it's faster to work with :-), assigns a projection to it then does the ggplot stuff. It's rly important to use the proper bounding box (try removing the xlim/ylim and see what happens :-) since Albers really does do a transform on the projection. I left the background and gridlines in just so so you can see what it does to the projection.

@abresler
Copy link

Still getting the "Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, :
Cannot open file" -- I will try reinstalling the package

@hrbrmstr
Copy link
Author

DOH! Memory: triggered. You need the absolute latest gdal library installed and then re-install rgdal.

@abresler
Copy link

Still no luck :(

library(rgdal)
Loading required package: sp
rgdal: version: 0.9-1, (SVN revision 518)
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 1.9.2, released 2012/10/08
Path to GDAL shared files: /Users/alexbresler/Library/R/3.1/library/rgdal/gdal
Loaded PROJ.4 runtime: Rel. 4.8.0, 6 March 2012, [PJ_VERSION: 480]
Path to PROJ.4 shared files: /Users/alexbresler/Library/R/3.1/library/rgdal/proj
library(rgeos)
rgeos version: 0.3-8, (SVN revision 460)
GEOS runtime version: 3.3.3-CAPI-1.7.4
Polygon checking: TRUE

library(maptools)
Checking rgeos availability: TRUE
library(mapproj)
Loading required package: maps
us <- readOGR("http://bl.ocks.org/mbostock/raw/4090846/us.json", "states")
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, :
Cannot open file

@timelyportfolio
Copy link

Did you ever get this figured out? I'm getting the same error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment