14

I want to project this map in robinson projection:

library(ggmap)
world <- map_data("world")
ggplot() + geom_path(data = world, 
                              aes(long, lat, group = group))

enter image description here

And I would like to change the projection to "Robinson" (following advice from answer to my previous question: What projection does the global climate region map from Wikipedia use?

I had a hard time finding a default implementation of this projection, I worked out the following for using the proj4 library:

library(proj4)
robinson <- project(cbind(world$long, world$lat), 
                    proj = "+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs")

I have tried a number of approaches, including:

# using ggmap::get.map()
get_map("world", projection = mapprojection(robinson))
# using ggplot2::coord_map
coord_map(projection = robinson)
# and sp::coordinates:
library(sp)
coordinates(world) <- ~ lat + long
gridded(world) <- TRUE # returns error
proj4string(world) <- CRS(robinson)

but none of these work. Is it a typo, or I missing something fundamental about this method?

0

2 Answers 2

12

It might be tricky to handle Robinson from within ggplot2.

AFAIK ggplot2 coord_map solution you explored will use projection information as defined in mapproject package. There are few available there but unfortunately Robinson is not one of them and I'm not sure if you can add your own.

Also - the world data you are using (from ggmap package I presume) is already a data frame class. So you will not be able to reproject it easily (?).

My suggestion would be start from scratch using shape file and handle geographical data before passing them to ggplot2. My cursory solution using Natural Earth data would follow this steps:

library(ggplot2)
library(grid)

# get data
download.file(url="http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/110m/cultural/ne_110m_admin_0_countries.zip", "ne_110m_admin_0_countries.zip", "auto")
unzip("ne_110m_admin_0_countries.zip")
file.remove("ne_110m_admin_0_countries.zip")

# read shape file using rgdal library
library(rgdal)
ogrInfo(".", "ne_110m_admin_0_countries")
world <- readOGR(".", "ne_110m_admin_0_countries")
summary(world)  
plot(world, col = "grey")  

readOGR uses information about the projection from prj file and summary now tells me now that world is now

Object of class SpatialPolygonsDataFrame
Coordinates:
   min       max
x -180 180.00000
y  -90  83.64513
Is projected: FALSE 
proj4string :
[+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0]

And looks like that:

enter image description here

Let's transform to Robinson:

worldRobinson <- spTransform(world, CRS("+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
summary(worldRobinson)  
plot(worldRobinson, col = "grey")  

Summary is now:

Object of class SpatialPolygonsDataFrame
Coordinates:
        min      max
x -16810131 16810131
y  -8625154  8343004
Is projected: TRUE 
proj4string :
[+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs +towgs84=0,0,0]

And it looks like that:

enter image description here

From here you should be able to continue with ggplot (fortify might be needed).

8

You can now do this directly with the ggalt package:

library(ggplot2)
library(ggalt)
library(ggthemes)

wrld <- map_data("world")

gg <- ggplot()
gg <- gg + geom_map(data=wrld, map=wrld,
                    aes(x=long, y=lat, map_id=region),
                    color="#2b2b2b", size=0.15, fill=NA)
gg <- gg + coord_proj("+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs")
gg <- gg + theme_map()
gg

enter image description here

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.