5

As you can see below, there is a weird displaying problem on the maps I made using ggplots. The same problem seems to happen with any projection.

enter image description here

Here is the code: Only the packages maps and ggplot2 are needed

  mapWorld <- borders("world", colour="gray50", fill="black")
    ggplot() + mapWorld +
      coord_map("mercator") +
      ylim(-90,90)
5
  • Looks like a problem in the data. Maybe in the ordering, maybe not. When I run summary(mapWorld$data) looks like longitude is coded from -179 to 190... I would expect -180 to 180 Commented May 20, 2015 at 21:50
  • I had not thought of that. I will try with other data. Thanks ! Commented May 20, 2015 at 21:53
  • 1
    I can replicate your problem, but the examples at the bottom of ?border work just fine for me. I'm 99% sure it's a data issue rather than a ggplot issue. Commented May 20, 2015 at 21:59
  • examples at the bottom of ?borders work fine for me too. That's weird since some peoples use borders("world", ...) without any mention of this problem Commented May 20, 2015 at 22:09
  • Adding +coord_equal to the plot appears to fix the problem. But I am not posting this as an answer because I am not certain it is a solution per se i.e. Shouldn't the projection specification in coord_map() suffice ??? Commented May 25, 2015 at 11:04

2 Answers 2

2

Apparently the problem is caused by the polygons that cross the 0 coordinate, the place in which the world merges. R dont knows how to close those polygons and projects them all around the world.

This method recreates the polygons and prevents them from crossing the 0 coordinate (xlim and ylim). It works with any kind of projection.

require(ggplot2)
require(PBSmapping)
require(data.table)

mapWorld <- map_data("world")
setnames(mapWorld, c("X","Y","PID","POS","region","subregion"))
worldmap = clipPolys(mapWorld, xlim=xlim,ylim=ylim, keepExtra=TRUE)
ggplot() + geom_polygon(data = mapWorld, aes(X,Y,group=PID))
Sign up to request clarification or add additional context in comments.

1 Comment

This throws me the following error: Error in .clip(polys, xlim, ylim, isPolygons = TRUE, keepExtra) : (list) object cannot be coerced to type 'double'
1

why you need to use?

ggplot() + mapWorld +
  coord_map("mercator") +
  ylim(-90,90)

if u use just

ggplot() + mapWorld

It works perfectly

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.