【问题标题】:Remove Antarctica from gglpot2 map从 ggplot2 地图中删除南极洲
【发布时间】:2018-10-16 11:35:53
【问题描述】:

我正在尝试重现 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')

我想从地图中删除南极洲,这样它就不会占用太多空白空间。我尝试按照另一个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')

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

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

还有其他方法可以去除南极洲吗?


更新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) 中的错误:找不到对象“组”

【问题讨论】:

  • 子集是一种方式,map + scale_y_continuous(limits=c(-60,90)) 是另一种方式。
  • @hrbrmstr 谢谢,规模工作正常!你想让它成为一个答案,以便我可以接受吗?顺便问一下,subsetting 是什么意思?
  • 您的filter() 只是内置subset() 的一个tidyverse 版本。而且,永远,永远不要将coord_cartesian() 与全球地图一起使用。使用适当的投影。 ggplot2::coord_map()ggalt::coord_proj() 的存在是有充分理由的。
  • @hrbrmstr 你能检查我的编辑吗?我尝试使用确实创建没有南极洲的底图的子集,但是当我尝试在其上绘制点时,它通过object 'group' not found 错误

标签: r plot ggplot2 visualization


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我们也可以使用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
    

    【讨论】:

    • 我相当肯定应该考虑使用适当的投影而不是诉诸coord_cartesian()。这是一个非常糟糕的建议。
    • @hrbrmstr 感谢您的建议。如果您可以与coord_mapcoord_proj 分享一个示例,那就太好了,因为我不熟悉它们。如果这满足了 OP 的需求,我也会建议 OP 接受您的帖子作为答案。
    • @hrbrmstr 感谢您的评论。
    猜你喜欢
    • 2022-10-19
    • 1970-01-01
    • 2019-05-29
    • 1970-01-01
    • 2014-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    相关资源
    最近更新 更多