Pacific-centric Robinson projection with ggplot in R

跟風遠走 提交于 2019-12-08 06:07:22

问题


I'm trying to create a pacific-centric Robinson projection in R with ggplot...

converting a shapefile to a Robinson CRS using spTransform and setting longitude to 150 degrees doesn't work, and fortify errors out.

like this:

world_robin <- spTransform(world2, CRS("+proj=robin +lon_0=150 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
plot(world_robin, col = "khaki", bg = "azure2")

And there doesn't appear to be a way to do it later in the process when setting a projection for ggmap: ie,

coord_map("mollweide", orientation=c(90, 0, 150)) 

this appears to be the best, but it's not quite there.


回答1:


You'll need GDAL / rgdal for this and it's 180 vs 150 but you should be able to extrapolate. GIS SO FTW!

library(ggplot2)
library(ggthemes)
library(sp)
library(rgdal)

# assumes you are in the ne_110m... directory
# split the world and stitch it back together again

system("ogr2ogr world_part1.shp ne_110m_admin_0_countries.shp -clipsrc -180 -90 0 90")
system("ogr2ogr world_part2.shp ne_110m_admin_0_countries.shp -clipsrc 0 -90 180 90")
system('ogr2ogr world_part1_shifted.shp world_part1.shp -dialect sqlite -sql "SELECT ShiftCoords(geometry,360,0), admin FROM world_part1"')
system("ogr2ogr world_0_360_raw.shp world_part2.shp")
system("ogr2ogr -update -append world_0_360_raw.shp world_part1_shifted.shp -nln world_0_360_raw")

world <- readOGR("ne_110m_admin_0_countries/world_0_360_raw.shp", "world_0_360_raw")
world_robin <- spTransform(world, CRS("+proj=robin +lon_0=180 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

world_dat <- fortify(world_robin)

gg <- ggplot()
gg <- gg + geom_map(data=world_dat, map=world_dat,
                    aes(x=long, y=lat, map_id=id),
                    fill="khaki", color="black", size=0.25)
gg <- gg + coord_equal()
gg <- gg + theme_map()
gg <- gg + theme(plot.background=element_rect(fill="azure2"))
gg

You can use gdalUtils for this as well, but it just calls the system binaries and you have to specify the clipping polygons with well known strings (so the system call is a few characters shorter for the line).

FYI: Doing this means you have to shift and project all points/shapes before plotting with ggplot2.



来源:https://stackoverflow.com/questions/32591368/pacific-centric-robinson-projection-with-ggplot-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!