从gglpot2地图中删除南极洲

时间:2018-05-06 14:53:57

标签: r plot ggplot2 visualization

我正在尝试重现this tutorial如何绘制类似散点图的地图。以下是完整代码和输出:

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

我想从地图中删除Antartica,以便它不会占用太多空白空间。我尝试按照以下方式跟踪另一个similar Stackoverflow question的解决方案:

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

但是当我尝试显示地图时,我收到以下错误:

  

粘贴错误(区域,组):未找到对象“区域”

有没有其他方法可以删除Antartica?

更新subset尝试失败

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)

错误:

  

eval(expr,envir,enclos)中的错误:找不到对象'group'

2 个答案:

答案 0 :(得分:3)

根据hrbmstr的建议,这是一个使用正确投影和sf包(使用ggplot2开发版本中的geom_sf)的解决方案。注意我们使用coord_sf来设置限制。

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

答案 1 :(得分:1)

我们也可以使用coord_cartesian(ylim = c(-50, 90))来设置y限制。

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