如何使五大湖与 R 中的海洋颜色相同?
How to make the Great Lakes the same color as the ocean in R?
我刚开始使用 R 制作地图。我正在尝试制作北美地图(以美国为中心),并希望五大湖的颜色与海洋颜色相同。我当前的代码默认使它们与 countries/states 具有相同的颜色。关于如何改变颜色的任何想法?也许是不同的底图?
当前代码:
library(cowplot)
library(googleway)
library(ggplot2)
library(ggrepel)
library(ggspatial)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
world <- ne_countries(scale = "medium", returnclass = "sf")
usa <- st_as_sf(maps::map("state", fill=TRUE, plot =FALSE),
crs = 4269)
ggplot(data = world) +
geom_sf(color = "black", fill = "gray") +
geom_sf(data = usa, color = "black", fill = "gray") +
xlab("Longitude") + ylab("Latitude") +
coord_sf(xlim = c(-123, -69), ylim = c(25, 49), expand = TRUE) +
annotation_scale(location = "br", width_hint = 0.5, text_cex = 1) +
annotation_north_arrow(location = "br", which_north = "true",
pad_x = unit(0.15, "in"), pad_y = unit(0.3, "in"),
style = north_arrow_fancy_orienteering) +
theme_bw() +
theme(plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
text = element_text(size = 16),
axis.text.x = element_text(size = 14, color = "black"),
axis.text.y = element_text(size = 14, color = "black"),
panel.grid.major = element_line(color = gray(0.5), linetype = "dashed", size = 0.5),
panel.background = element_rect(fill = "aliceblue"))
好的,我做了一些挖掘,发现 rnaturalearth 有全世界湖泊的几何形状。 ne_download(type = 'lakes')
.
library(ggplot2)
library(ggspatial)
library(sf)
library(rnaturalearth)
科幻世界
world <- rnaturalearth::ne_countries(scale = "medium",
returnclass = "sf")
北美作为 sf
n_america <- world %>%
filter(adm0_a3 %in% c("MEX", "CAN", "USA"))
作为 sf 的湖泊
lakes <- rnaturalearth::ne_download(scale = 110,
type = 'lakes',
category = 'physical') %>%
sf::st_as_sf(lakes110, crs = 4269)
美国各州作为 sf
usa_states <- st_as_sf(maps::map("state",
fill=TRUE,
plot =FALSE),
crs = 4269)
或者,美国表示为 sf(因此您不需要 {maps}
)
devtools::install_github("ropensci/rnaturalearthhires")
usa_states <- rnaturalearth::ne_states(country = "United States of America") %>%
sf::st_as_sf(crs = 4269)
使用 ggplot2 绘图
ggplot() +
geom_sf(data = n_america,
mapping = aes(geometry = geometry),
color = "black",
fill = "gray") +
geom_sf(data = usa_states,
mapping = aes(geometry = geom),
color = "black",
fill = "gray") +
geom_sf(data = lakes,
mapping = aes(geometry = geometry),
color = "black",
fill = "lightblue") +
coord_sf(ylim = c(23, 49),
xlim = c(-123, -69),
expand = TRUE) +
annotation_scale(location = "br",
width_hint = 0.25,
text_cex = 1) +
annotation_north_arrow(location = "br",
which_north = "true",
pad_x = unit(0.15, "in"),
pad_y = unit(0.3, "in"),
style = north_arrow_fancy_orienteering) +
labs(x = "Longitude",
y = "Latitude") +
theme_bw() +
theme(axis.text.x = element_text(size = 12, color = "black"),
axis.text.y = element_text(size = 12, color = "black"),
panel.grid.major = element_line(color = gray(0.5), linetype = "dashed", size = 0.5),
panel.background = element_rect(fill = "lightblue"))
随心所欲地改变海洋和湖泊的颜色。
我刚开始使用 R 制作地图。我正在尝试制作北美地图(以美国为中心),并希望五大湖的颜色与海洋颜色相同。我当前的代码默认使它们与 countries/states 具有相同的颜色。关于如何改变颜色的任何想法?也许是不同的底图?
当前代码:
library(cowplot)
library(googleway)
library(ggplot2)
library(ggrepel)
library(ggspatial)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
world <- ne_countries(scale = "medium", returnclass = "sf")
usa <- st_as_sf(maps::map("state", fill=TRUE, plot =FALSE),
crs = 4269)
ggplot(data = world) +
geom_sf(color = "black", fill = "gray") +
geom_sf(data = usa, color = "black", fill = "gray") +
xlab("Longitude") + ylab("Latitude") +
coord_sf(xlim = c(-123, -69), ylim = c(25, 49), expand = TRUE) +
annotation_scale(location = "br", width_hint = 0.5, text_cex = 1) +
annotation_north_arrow(location = "br", which_north = "true",
pad_x = unit(0.15, "in"), pad_y = unit(0.3, "in"),
style = north_arrow_fancy_orienteering) +
theme_bw() +
theme(plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
text = element_text(size = 16),
axis.text.x = element_text(size = 14, color = "black"),
axis.text.y = element_text(size = 14, color = "black"),
panel.grid.major = element_line(color = gray(0.5), linetype = "dashed", size = 0.5),
panel.background = element_rect(fill = "aliceblue"))
好的,我做了一些挖掘,发现 rnaturalearth 有全世界湖泊的几何形状。 ne_download(type = 'lakes')
.
library(ggplot2)
library(ggspatial)
library(sf)
library(rnaturalearth)
科幻世界
world <- rnaturalearth::ne_countries(scale = "medium",
returnclass = "sf")
北美作为 sf
n_america <- world %>%
filter(adm0_a3 %in% c("MEX", "CAN", "USA"))
作为 sf 的湖泊
lakes <- rnaturalearth::ne_download(scale = 110,
type = 'lakes',
category = 'physical') %>%
sf::st_as_sf(lakes110, crs = 4269)
美国各州作为 sf
usa_states <- st_as_sf(maps::map("state",
fill=TRUE,
plot =FALSE),
crs = 4269)
或者,美国表示为 sf(因此您不需要 {maps}
)
devtools::install_github("ropensci/rnaturalearthhires")
usa_states <- rnaturalearth::ne_states(country = "United States of America") %>%
sf::st_as_sf(crs = 4269)
使用 ggplot2 绘图
ggplot() +
geom_sf(data = n_america,
mapping = aes(geometry = geometry),
color = "black",
fill = "gray") +
geom_sf(data = usa_states,
mapping = aes(geometry = geom),
color = "black",
fill = "gray") +
geom_sf(data = lakes,
mapping = aes(geometry = geometry),
color = "black",
fill = "lightblue") +
coord_sf(ylim = c(23, 49),
xlim = c(-123, -69),
expand = TRUE) +
annotation_scale(location = "br",
width_hint = 0.25,
text_cex = 1) +
annotation_north_arrow(location = "br",
which_north = "true",
pad_x = unit(0.15, "in"),
pad_y = unit(0.3, "in"),
style = north_arrow_fancy_orienteering) +
labs(x = "Longitude",
y = "Latitude") +
theme_bw() +
theme(axis.text.x = element_text(size = 12, color = "black"),
axis.text.y = element_text(size = 12, color = "black"),
panel.grid.major = element_line(color = gray(0.5), linetype = "dashed", size = 0.5),
panel.background = element_rect(fill = "lightblue"))
随心所欲地改变海洋和湖泊的颜色。