在地图上为特定国家着色

问题描述 投票:0回答:1

嘿嘿!我正在尝试为这张地图上的特定国家(Fance)着色,但我不知道该怎么做。请帮我!谢谢!

library(rnaturalearth)
library(sf)
library(ggplot2)
world <- ne_countries(scale = 50, returnclass = 'sf')
ggplot(world) + geom_sf(aes(fill = continent), color = 'black')+ coord_sf(crs = st_crs(3035),
           xlim = c(2800000, 6200000), 
           ylim = c(1500000, 4000000)) +
scale_fill_manual(values = c('grey', NA, 'grey','grey', NA, NA, NA, NA), guide = 'none', na.value = 'white') + theme(panel.background = element_rect(),panel.grid.major = element_line(linewidth = 0.1))
r colors maps country
1个回答
0
投票

您可以对国家/地区进行颜色编码 (a) 通过将其添加为单独的图层,或 (b) 通过创建指示变量。另请参阅此答案

library(rnaturalearth)
library(sf)
library(ggplot2)

# Load world data
world <- ne_countries(scale = 50, returnclass = 'sf')

(一)

# Filter data for France
france <- world[world$name == "France", ]

# Plot the map with two layers
ggplot() + 
  geom_sf(data = world, aes(fill = continent), color = 'black') + 
  geom_sf(data = france, fill = 'blue', color = 'black') + 
  coord_sf(crs = st_crs(3035), xlim = c(2800000, 6200000), ylim = c(1500000, 4000000)) + 
  theme(panel.background = element_rect(), panel.grid.major = element_line(linewidth = 0.1))

(二)

# Create a new column to specify the color for France
world$color <- ifelse(world$name == "France", "France", "Other")

# Plot the map
ggplot(world) + 
  geom_sf(aes(fill = color), color = 'black') + 
  scale_fill_manual(values = c("France" = "blue", "Other" = "grey")) + 
  coord_sf(crs = st_crs(3035), xlim = c(2800000, 6200000), ylim = c(1500000, 4000000)) + 
  theme(panel.background = element_rect(), panel.grid.major = element_line(linewidth = 0.1))
© www.soinside.com 2019 - 2024. All rights reserved.