在 r 中映射时避免疯狂的线条

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

您好,我正在尝试在 r 中创建一张显示平均基尼系数的欧洲地图。我很确定我的数据已全部整理完毕,但我仍然收到一堆疯狂的线条和漏洞。我制作地图的方式有错误吗?

我确保所有国家/地区名称都在两个数据集中对齐,因此我很确定问题出在映射上。我尝试将群体美学更改为子群体,但这只是摆脱了渐变颜色。我还尝试按照此处的建议限制 x 和 y 坐标在 ggplot2 中绘制地图时避免水平线和疯狂的形状但这并没有解决问题。我还尝试使用 geom_polypath 这有帮助,但没有完全解决问题。

这是生成的图像:Crazy map

wiid_filtered <- wiid %>%
  filter(year >= 2000 & country != "Russia")

wiid_filtered <- wiid_filtered %>%
  filter(region_un == "Europe")

# Calculate the average Gini score for each country
wiid_avg <- wiid_filtered %>%
  group_by(country) %>%
  summarise(avg_gini = mean(gini, na.rm = TRUE))

# Load world map data
world_map <- map_data("world")

# Filter world map data for Europe
europe_map <- subset(world_map, region %in% c("Albania", "Andorra", "Austria", "Belarus", "Belgium", "Bosnia and Herzegovina",
                                                      "Bulgaria", "Croatia", "Czech Republic", "Denmark", "Estonia", "Finland", "France",
                                                      "Germany", "Greece", "Hungary", "Iceland", "Ireland", "Italy", "Kosovo", 
                                                      "Latvia", "Lithuania", "Luxembourg", "Malta", "Moldova", "Montenegro", 
                                                      "Netherlands", "North Macedonia", "Norway", "Poland", "Portugal", "Romania", 
                                                      "San Marino", "Serbia", "Serbia and Montenegro", "Slovakia", "Slovenia", 
                                                      "Spain", "Sweden", "Switzerland", "Ukraine", "UK"))

wiid_avg <- wiid_avg %>% 
  mutate(country = if_else(country == "United Kingdom", "UK", country))

wiid_avg <- wiid_avg %>% 
  mutate(country = if_else(country == "Czechia", "Czech Republic", country))


map_data <- merge(europe_map, wiid_avg, by.x = "region", by.y = "country", all.x = TRUE)

# Plot the choropleth map
ggplot(map_data, aes(x = long, y = lat, group = group, fill = avg_gini)) +
  geom_polypath(color = "black", size = 0.2) +
  coord_cartesian(xlim = c(-30, 50), ylim = c(30, 90)) +
  scale_fill_gradient(name = "Average Gini Score",
                      low = "lightblue", high = "darkblue",
                      guide = "legend") +
  labs(title = "Average Gini Score in Europe since 2000",
       x = "", y = "") +
  theme_minimal() +
  theme(legend.position = "bottom")
r ggplot2 maps ggmap
1个回答
0
投票

您遇到的问题是由于

ggplot2
地图数据的结构造成的。我对
geom_polypath()
一无所知,但我确实知道,如果您正在处理空间数据,那么将它们创建为简单特征 (sf) 对象“几乎总是”更容易。 这是因为

ggplot2

有一些非常方便的功能来管理 sf 对象,例如

geom_sf()
coord_sf()
等。使用
geom_sf()
绘制 sf 对象还有一个优点,即坐标会自动从 sf 对象导出。
为此,以下是如何使用 

sf

包解决您的问题。您尚未提供 GINI 数据,因此,如果您在加入数据时遇到问题,请使用相关数据更新您的问题,我将更新此答案。

library(sf)
library(dplyr)
library(ggplot2)

# Load world map data
world_map <- map_data("world")

# Filter world map data for Europe
europe_map <- subset(world_map, region %in% c("Albania", "Andorra", "Austria", "Belarus", "Belgium", "Bosnia and Herzegovina",
                                              "Bulgaria", "Croatia", "Czech Republic", "Denmark", "Estonia", "Finland", "France",
                                              "Germany", "Greece", "Hungary", "Iceland", "Ireland", "Italy", "Kosovo", 
                                              "Latvia", "Lithuania", "Luxembourg", "Malta", "Moldova", "Montenegro", 
                                              "Netherlands", "North Macedonia", "Norway", "Poland", "Portugal", "Romania", 
                                              "San Marino", "Serbia", "Serbia and Montenegro", "Slovakia", "Slovenia", 
                                              "Spain", "Sweden", "Switzerland", "Ukraine", "UK"))

# Define lon/lat, group data, combine by group, define as polygons, and set CRS
europe_map <- europe_map %>%
  st_as_sf(coords = c("long","lat")) %>%
  group_by(region, group) %>%
  summarise(geometry = st_combine(geometry), .groups = "drop") %>% 
  st_cast("POLYGON") %>%
  st_set_crs(4326) %>%
  ungroup()

ggplot(europe_map) +
  geom_sf(color = "black", linewidth = 0.2) +
  coord_sf(xlim = c(-30, 50), ylim = c(30, 90)) +
  # scale_fill_gradient(name = "Average Gini Score",
  #                     low = "lightblue", high = "darkblue",
  #                     guide = "legend") +
  labs(title = "Average Gini Score in Europe since 2000",
       x = "", y = "") +
  theme_minimal() +
  theme(legend.position = "bottom")

© www.soinside.com 2019 - 2024. All rights reserved.