Reputation: 1437
I am trying to plot a reprojected world map using data from the cshapes
package and sp::spTransform
, but the projection leads to distorted plots. How can I correctly reproject and plot a cshapes
map?
Here is an example that shows that the map plots fine by itself (code adapted from this blog post):
library("cshapes")
library("ggplot2")
library("rgdal")
wmap <- cshp(date=as.Date("2012-06-30"))
wmap_df <- fortify(wmap)
ggplot(wmap_df, aes(long,lat, group=group)) +
geom_polygon() +
labs(title="World map (longlat)") +
coord_equal()
ggsave("~/Desktop/map1.png", height=4, width=7)
And here is the distorted version when I reproject to Robinson:
wmap_robin <- spTransform(wmap, CRS("+proj=robin"))
wmap_df_robin <- fortify(wmap_robin)
ggplot(wmap_df_robin, aes(long,lat, group=group)) +
geom_polygon() +
labs(title="World map (robinson)") +
coord_equal()
ggsave("~/Desktop/map2.png", height=4, width=7)
Some additional info:
cshapes
does. ggplot2
, plotting the map with base graphics shows the same distortions (plot(wmap_robin)
).Upvotes: 3
Views: 747
Reputation: 1437
Update for 2018, using a solution with sf
:
library("cshapes")
library("sf")
cshp(as.Date("2015-01-01")) %>%
st_as_sf() %>%
st_crop(ymin = -90, ymax = 90, xmin=-180, xmax=180) %>%
st_transform(crs = "+proj=robin") %>%
`[`(1) %>% plot()
The output is:
Upvotes: 0
Reputation: 47146
You can use raster::crop
to remove nodes that are just smaller than -180 or larger than 180
library(cshapes)
library(raster)
wmap <- cshp(date=as.Date("2012-06-30"))
w <- crop(wmap, extent(-180, 180,-90,90))
w_robin <- spTransform(w, CRS("+proj=robin"))
plot(w_robin)
Upvotes: 1