Cloropleth 使用 ggplot

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

我一直在尝试绘制一个 cloropleth 地图。我取得了一些进展,但我现在被困住了。 这个想法是绘制巴西地图,并根据 2021 年暴力故意死亡人数对每个州进行颜色编码。

我正在使用以下数据集:

State <- c("SP", "SC", "DF", "MG", "RS", "MS", "PR", "AC", "PI","TO", "MT", "RO", "GO", "RJ", "ES", "MA", "PB", "AL", "RN", "PA", "SE", "PE", "RR", "CE", "AM", "BA", "AP")
VID <- c(7.9, 10.1, 11.2, 11.4, 15.9, 20.7, 20.8, 21.2, 23.8, 24.3, 24.9, 25.0, 26.1, 27.2, 28.2, 28.3, 28.6, 31.8, 32.4, 32.8, 33.9, 34.8, 35.5, 37.0, 39.1, 44.9, 53.8)
VIDBrazil <- data.frame(State, VID)

来自巴西的 geoJSON 数据文件我从这个Brasil JSON 文件中获取

我尝试使用的代码基于here,特别是herehereherehere

的资源

我使用的代码如下:

State <- c("SP", "SC", "DF", "MG", "RS", "MS", "PR", "AC", "PI","TO", "MT", "RO", "GO", "RJ", "ES", "MA", "PB", "AL", "RN", "PA", "SE", "PE", "RR", "CE", "AM", "BA", "AP")
VID <- c(7.9, 10.1, 11.2, 11.4, 15.9, 20.7, 20.8, 21.2, 23.8, 24.3, 24.9, 25.0, 26.1, 27.2, 28.2, 28.3, 28.6, 31.8, 32.4, 32.8, 33.9, 34.8, 35.5, 37.0, 39.1, 44.9, 53.8)
VIDBrazil <- data.frame(State, VID)

spdf_Brasil <- geojson_read("C:/Users/Giovanni/Desktop/R/brazil_geo.json", what = "sp") #Import JSON file

spdf_Brasil_A<- tidy(spdf_Brasil) #Convert to data frame

ggplot() + #plot the map
  geom_polygon(data = spdf_Brasil_A, aes( x = long, y = lat, group = group), fill="#69b3a2", color="white") +
  theme_void() +
  coord_map()

spdf_Brasil_MVI <- spdf_Brasil_A %>% #add the values of deaths to use for color coding the map
  left_join(. , VIDBrazil, by=c("id"="State"))


ggplot() + #ploting with color coding
  geom_polygon(data = spdf_Brasil_MVI, aes( x = long, y = lat, group = group, fill= VID), color="green") + 
  theme_void() +
  scale_fill_gradient(name = "Mortes por 100 mil habitantes", low = "#fdc0b2", high = "#9a1f03", limits = c(0, 60.0)) +
  coord_map()

Obtained map

我错过了什么?我应该怎么做才能对地图进行颜色编码?

感谢您的协助!

r ggplot2 maps visualization
© www.soinside.com 2019 - 2024. All rights reserved.