Cannot display ggplot graph graph when restrictions are set using Coord_Map function

I am trying to limit the chart of a map to a specific area. coord_map is preferred until scale_x_continuous and y are equivalent, since the latter messed up the polygons. But for some reason, I find that this does not work. Here is my code (uploads a form file of size 57 KB):

 require(maptools) require(ggplot2) download.file('https://dl.dropboxusercontent.com/u/46043231/UK.zip', "uk.zip", method="internal", mode="wb") unzip('uk.zip') uk = readShapePoly('uk_outline_1000m.shp') print(bbox(uk)) min max x 259.9625 655566.4 y 7211.7025 1218558.9 uk2 = fortify(uk) (p = ggplot(uk2, aes(x=long, y=lat, group=group)) + geom_polygon() + coord_equal()) 

enter image description here

But when coord_map used, the graph disappears:

 p + coord_map(xlim=c(0, 630000), ylim=c(0, 1000000)) 

enter image description here

Any idea what is going on?

+6
source share
1 answer

I would try something like this to test several options.

 library(maptools) library(ggplot2) library(rgdal) library(raster) library(latticeExtra) 

Download and read data

 download.file('https://dl.dropboxusercontent.com/u/46043231/UK.zip', "uk.zip", method="internal", mode="wb") unzip('uk.zip') uk <- readOGR(dsn = getwd(), layer = 'uk_outline_1000m') 

It is assumed that OSGB 1936 / British national grid is used.

More on SpatialReference

 proj4string(uk) <- CRS('+init=epsg:27700') # EPSG 27700 extent(uk) bb.uk <- as(extent(uk), 'SpatialPolygons') # a spatial object proj4string(bb.uk) <- CRS('+init=epsg:27700') 

Record projected shapefile uk bbox. I will write this to map layers to QGIS. This will be my link system.

 writeOGR(as(bb.uk, 'SpatialPolygonsDataFrame'), dsn = getwd(), layer = 'bbuk2_bng', driver = 'ESRI Shapefile') 

The required bounding box. Using predicted coordinates

 bb.uk2 <- as(extent(c(0, 630000), c(0, 1000000)), 'SpatialPolygons') proj4string(bb.uk2) <- CRS('+init=epsg:27700') 

Record projected bbox shapefile

 writeOGR(as(bb.uk2, 'SpatialPolygonsDataFrame'), dsn = getwd(), layer = 'bbuk2user_bng', driver = 'ESRI Shapefile') 

QGis map using British National Grid EPSG: 27700

QGIS map EPSG 27700

Planned Layers

Base plot

 plot(uk, col = 'grey50', axes = T, xlim=c(-50000, 705566.4), ylim=c(-50000, 1325000)) plot(bb.uk, add = T) plot(bb.uk2, border = 'red', add = T) 

base plot epsg 27700

spplot

I took an arbitrary window to expand the area of ​​the graph.

 sp::spplot(uk, zcol = 'NAME_ISO', scales = list(draw = TRUE), xlim=c(-50000, 705566.4), ylim=c(-50000, 1325000), col.regions="grey90") + latticeExtra::layer(sp.polygons(bb.uk, fill = NA, col = 'blue')) + latticeExtra::layer(sp.polygons(bb.uk2, fill = NA, col = 'red')) 

spplot epsg 27700

ggmap with projected layers

 uk.df = fortify(uk) # admin bbuk.df <- fortify(bb.uk) # country bbox extent bbuk2.df <- fortify(bb.uk2) # user bbox extent 

write it down

 p <- ggplot() + geom_polygon(data = uk.df, aes(x=long, y=lat, group=group)) + geom_polygon(data = bbuk.df, aes(x=long, y=lat, group=group), colour = 'blue', fill = NA) + geom_polygon(data = bbuk2.df, aes(x=long, y=lat, group=group), colour = 'red', fill = NA) + coord_equal() # cartesian p 

ggplot epsg27700 - cartesian

build it using the user bounding box

 p + coord_equal(xlim=c(0, 630000), ylim=c(0, 1000000)) 

gplot epsg27700 - cartesian user box

Now ggplot with geographic (non-projected) coordinates

Undesigned WGS84 coordinate system

 p.wgs84 <- CRS("+init=epsg:4326") # WGS84 Long Lat 

Convert Projection Layer to WGS84

 uk.wgs89 <- spTransform(uk, p.wgs84) 

Geographic bbox uk

 bbuk.wgs84 <- as(as(extent(uk.wgs89), 'SpatialPolygons'), 'SpatialPolygonsDataFrame') 

Geographic bbox user degree

 bbuk2.wgs84 <- spTransform(bb.uk2, p.wgs84) bbuk2.wgs84 <- as(bbuk2.wgs84, 'SpatialPolygonsDataFrame') 

Build it with ggplot and cartesian map

 uk.df = fortify(uk.wgs89) # admin bbuk.df <- fortify(bbuk.wgs84) # country bbox extent bbuk2.df <- fortify(bbuk2.wgs84) # user bbox extent 

ggplot epsg4326

The result is not what I would expect. I do not understand why the red square is distorted.

+7
source

All Articles