简体   繁体   English

R 空间 - 边界图

[英]R Spatial - Boundaries Map

How can I define the boundaries of a country, so rivers outside the country won't appear on the map?如何定义一个国家的边界​​,使该国以外的河流不会出现在地图上? The image link below will clarify what I mean:下面的图片链接将阐明我的意思:

US Rivers美国河流

在此处输入图片说明

library(tidyverse) # ggplot2, dplyr, tidyr, readr, purrr, tibble
library(magrittr) # pipes
library(rnaturalearth) # Rivers
library(urbnmapr)

states <- urbnmapr::states
states <- fortify(states)

rivers10 <- ne_download(scale = "medium", type = 'rivers_lake_centerlines', category = 'physical') #, returnclass = "sf"

rivers10 <- fortify(rivers10)

rivers10 <- rivers10 %>%
  filter(long >= min(states$long)) %>%
  filter(long <= max(states$long)) %>%
  filter(lat >= min(states$lat)) %>%
  filter(lat <= max(states$lat))


ggplot() +
  geom_polygon(data = urbnmapr::states, mapping = aes(x = long, y = lat, group = group),
               fill = "#CDCDCD", color = "#25221E") +
  coord_map(projection = "albers", lat0 = 39, lat1 = 45) +
  geom_path(data = rivers10,
            aes(long, lat, group = group), size = 1, color = '#000077') +
  theme_minimal()

This is easier if you get the data as spatial objects.如果您将数据作为空间对象获取,这会更容易。 Then you can manipulate them to intersect the rivers with the US boundary.然后你可以操纵它们使河流与美国边界相交。

library(tidyverse) # ggplot2, dplyr, tidyr, readr, purrr, tibble
library(rnaturalearth) # Rivers
library(sf)
library(urbnmapr)

states = get_urbn_map('states', sf=TRUE)

rivers10 <- ne_download(scale = "medium", type = 'rivers_lake_centerlines', 
  category = 'physical', returnclass = "sf")

# Outline of the US
us = st_union(states)

# Transform rivers to the same projection as states and clip to US
rivers10 <- rivers10 %>%
  st_transform(st_crs(states)) %>% 
  st_intersection(us)

ggplot() +
  geom_sf(data=states, fill = "#CDCDCD", color = "#25221E") +
  geom_sf(data=rivers10, color='#000077') +
  theme_minimal()

Created on 2019-12-26 by the reprex package (v0.3.0)reprex 包(v0.3.0) 于 2019 年 12 月 26 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM