ggplot2 polygon world map centred with limits give

2019-07-22 20:17发布

问题:

Using the below code I generate a map centred on Washington DC. solution based on kohske's solution here. I have changed geom_path to geom_polygon as I want to colour the countries.

All is well and good until I want add the scale_x_continuous (commented so you can see that there's no problem when it is left out), this makes Antarctica and China look very strange, i guess because the polygons have been trimmed?

Is there a known workaround for this? Help is appreciated.

library(maps)
library(maptools)
library(ggplot2)

home_country_longitude <- -77.03

mp1 <- fortify(map(fill=TRUE, plot=FALSE))
mp2 <- mp1

mp2$long <- mp2$long + 360
mp2$group <- mp2$group + max(mp2$group) + 1

mp <- rbind(mp1, mp2)

if(home_country_longitude < 0){
  mp$long <- mp$long - (360 + home_country_longitude)
} else {
  mp$long <- mp$long + home_country_longitude
}

ggplot() + 
  geom_polygon(aes(x = long, y = lat, group = group), data = mp) +
  #scale_x_continuous(limits = c(-180, 180)) +
  theme(panel.background = element_rect(fill = "#090D2A"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank())

回答1:

Use coord_map, like this. Full explanation here.

ggplot() + 
  geom_polygon(aes(x = long, y = lat, group = group), data = mp) +
  coord_map(xlim = c(-180, 180)) +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank())



标签: r ggplot2