Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does coord_map produce a weird output?

Tags:

r

ggplot2

I'm trying to draw a world map using ggplot. My code is in my gist file. The output is correct when I don't use coord_map but very strange when I use coord_map :

ggplot(data = test, aes(fill = x)) +
geom_map(aes(map_id = id), map =world.ggmap, color = "white") +
expand_limits(x = world.ggmap$long, y = world.ggmap$lat) +
theme_tufte() + 
coord_map()

map with <code>coord_map</code>

ggplot(data = test, aes(fill = x)) +
geom_map(aes(map_id = id), map =world.ggmap, color = "white") +
expand_limits(x = world.ggmap$long, y = world.ggmap$lat) +
theme_tufte()

enter image description here

I've got the same error when I use data from the maps package :

library(ggplot2)
library(maps)
world <- map_data("world")
ggplot() + 
geom_map( data=world, aes(x=long, y=lat, group = group, map_id = region),colour="white", fill="grey10", map = world ) + 
coord_map()

enter image description here

Does anyone has an answer ?

like image 450
PAC Avatar asked May 14 '14 10:05

PAC


3 Answers

I had a similar problem before, due to longitude values outside the range [-180,180]. In your example the data do not have this problem but my trick seems to work also here. In my case I just used 'xlim' to exclude the problematic data.

This solution seems to work in your case also (I used the code from your gist):

map+coord_map(xlim=c(-180,180))

It produces the following map:

enter image description here

There is still a problem with Antarctica, you can also consider clipping it too if you don't need this area:

map+coord_map(xlim=c(-180,180), ylim=c(-60, 90))

enter image description here

like image 87
alko989 Avatar answered Nov 15 '22 02:11

alko989


Another solution is to use wrld_simpl from maptools instead, but it retains issues with Antarctica.

require(maptools)
require(ggmap)

md <- map_data("world2")
data(wrld_simpl)
ggplot(wrld_simpl, aes(group = group, x = long, y = lat)) +
  geom_map() +
  coord_map()

Imgur

like image 45
Johan Larsson Avatar answered Nov 15 '22 01:11

Johan Larsson


I'm sure that is quite late but the same problem is still happening in ggplot.

If you're trying to zoom-in use the following approach.

ggplot()+...+xlim(c(-100, -25))+ ylim(c(-60, 20))

Good luck!

like image 2
CRP Avatar answered Nov 15 '22 01:11

CRP