Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 372
Vasilis Avatar asked Nov 06 '25 02:11

Vasilis


2 Answers

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

like image 69
sebdalgarno Avatar answered Nov 08 '25 16:11

sebdalgarno


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

like image 33
www Avatar answered Nov 08 '25 16:11

www



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!