Vasilis
Vasilis

Reputation: 2832

Remove Antarctica from gglpot2 map

I'm trying to reproduce this tutorial on how to plot a scatterplot-like map. Below is the full code and the output:

library(readr)
library(dplyr)
library(DT)

datatable(rladies, rownames = FALSE,
          options = list(pageLength = 5))
url_csv <- 'https://raw.githubusercontent.com/d4tagirl/R-Ladies-growth-maps/master/rladies.csv'
rladies <- read_csv(url(url_csv)) %>% 
  select(-1)

library(ggplot2)
library(maps)
library(ggthemes)

world <- ggplot() +
  borders("world", colour = "gray85", fill = "gray80") +
  theme_map() 

map <- world +
  geom_point(aes(x = lon, y = lat, size = followers),
             data = rladies, 
             colour = 'purple', alpha = .5) +
  scale_size_continuous(range = c(1, 8), 
                        breaks = c(250, 500, 750, 1000)) +
  labs(size = 'Followers')

enter image description here

I want to remove Antartica from the map so that it doesn't take so much empty space. I tried to follow the solution from another similar Stackoverflow question as follows:

world <- map_data("world") %>% 
     filter(region != "Antarctica") %>% 
     ggplot(aes(long, lat, group = paste(region, group))) + 
     geom_polygon() + 
     coord_fixed()

map <- world +
  geom_point(aes(x = lon, y = lat, size = followers),
             data = rladies, 
             colour = 'purple', alpha = .5) +
  scale_size_continuous(range = c(1, 8), 
                        breaks = c(250, 500, 750, 1000)) +
  labs(size = 'Followers')

But when I try to display the map I get the following error:

Error in paste(region, group) : object 'region' not found

Is there any other way to remove Antartica?


UPDATE: Failed subset attempt

countries <- map_data("world")
map_df <- subset(countries, region != "Antarctica")
map_base <- ggplot(data = map_df, mapping = aes(x = long, y = lat, group = group)) + coord_fixed(1.3) + geom_polygon(color = "black", fill = "gray")
# The base map is created successfully but I cannot plot points on it
map_base + geom_point(aes(x = lon, y = lat, size = followers), data = rladies, colour = 'purple', alpha = .5)

Error:

Error in eval(expr, envir, enclos) : object 'group' not found

Upvotes: 3

Views: 3507

Answers (3)

Dave Jarvis
Dave Jarvis

Reputation: 31171

Here's another way, which uses ggplot(), a filter, and a single line of code to remove Antarctica by referencing its 3-letter code:

library( tidyverse )
library( cowplot )
library( sf )
library( lwgeom )
library( rworldmap )

base_map <- getMap( resolution = "low" )
world_sf <- st_as_sf( base_map )

# Remove Penguinville
world_sf <- world_sf %>% filter( SOV_A3 != "ATA" )

# The rest of the answer uses the Winkel tripel projection.
crs_wintri <- "+proj=wintri +datum=WGS84 +no_defs +over"
world_wintri <- st_transform_proj(world_sf, crs = crs_wintri)

lats <- c(90:-90, -90:90, 90)
longs <- c(rep(c(180, -180), each = 181), 180)

wintri_outline <- 
  list(cbind(longs, lats)) %>%
  st_polygon() %>%
  st_sfc(
    crs = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
  ) %>% 
  st_sf() %>%
  st_transform_proj(crs = crs_wintri)

# Layout the graticule and transform it to Winkel tripel.
grat_wintri <- 
  st_graticule(lat = c(-89.9, seq(-80, 80, 20), 89.9)) %>%
  st_transform_proj(crs = crs_wintri)

ggplot() +
  geom_sf(data = wintri_outline, fill = "#56B4E950", color = NA) +
  geom_sf(data = grat_wintri, color = "gray30", size = 0.25/.pt) +
  geom_sf(data = world_wintri, color = "black", size = 0.5/.pt) +
  geom_sf(data = wintri_outline, fill = NA, color = "grey30", size = 0.5/.pt) +
  coord_sf(datum = NULL) +
  theme_map()

Produces:

World Map without Antarctica

Upvotes: 0

sebdalgarno
sebdalgarno

Reputation: 3197

Following on hrbmstr's advice, here is a solution using a proper projection and the sf package (with geom_sf from the development version of ggplot2). Note we use coord_sf to set the limits.

library(sf)

world <- map_data("world")
world.sf <- sf::st_as_sf(world, coords = c("long", "lat"), crs = 4326) %>% 
  group_by(group) %>% 
  summarize(do_union = FALSE) %>%
  st_cast("POLYGON") %>% 
  ungroup()

world <- ggplot() +
  geom_sf(data = world.sf, colour = "gray85", fill = "gray80") + 
  coord_sf(ylim = c(-50, 90), datum = NA) +
  theme(panel.background = element_rect(fill = 'white'))

 world +
  geom_point(aes(x = lon, y = lat, size = followers),
             data = rladies, 
             colour = 'purple', alpha = .5) +
  scale_size_continuous(range = c(1, 8), 
                        breaks = c(250, 500, 750, 1000)) +
  labs(size = 'Followers', x = NULL, y = NULL)

enter image description here

Upvotes: 4

www
www

Reputation: 39154

We can also use coord_cartesian(ylim = c(-50, 90)) to set the y limits.

library(ggplot2)
library(maps)
library(ggthemes)

world <- ggplot() +
  borders("world", colour = "gray85", fill = "gray80") +
  theme_map() +
  coord_cartesian(ylim = c(-50, 90)) 


map <- world +
  geom_point(aes(x = lon, y = lat, size = followers),
             data = rladies, 
             colour = 'purple', alpha = .5) +
  scale_size_continuous(range = c(1, 8), 
                        breaks = c(250, 500, 750, 1000)) +
  labs(size = 'Followers')

map

enter image description here

Upvotes: 2

Related Questions