ePoQ
ePoQ

Reputation: 463

Map in ggplot2 visualization/displaying bug?

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)

Upvotes: 5

Views: 1033

Answers (2)

ePoQ
ePoQ

Reputation: 463

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))

Upvotes: 2

Erathia
Erathia

Reputation: 45

why you need to use?

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

if u use just

ggplot() + mapWorld

It works perfectly

Upvotes: 1

Related Questions