我试图在一张世界地图上可视化一张从维基百科上刮来的Polity系列数据集中的表格。
在去掉一些变量之后,我希望通过颜色编码来可视化国家及其政权类型(编码为Polity数据集IV类)。在网页上有一个世界地图可视化作为我复制的基础。
我查阅了来自这个网站的文档,似乎我需要将我的数据集与打包的地理数据结合起来。不过,我不知道该怎么做。
输入:
library(rvest)
library(dplyr)
polity <- read_html("https://en.wikipedia.org/wiki/Polity_data_series")
table <- polity %>% html_table(fill=T)
second_table <- table[[2]]
second_table <- second_table[c(1,5)]
polity <- second_table
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
library(ggplot2)
world <- ne_countries(scale = "medium", returnclass = "sf")
ggplot(data = world) +
geom_sf() +
xlab("Longitude") + ylab("Latitude") +
ggtitle("World map", subtitle = paste0("(", length(unique(world$NAME)), " countries)"))发布于 2021-01-19 09:03:08
这个能做你想要的吗?
world2 <- left_join(world, polity,
by = c("name" = "Country"))
ggplot(data = world2) +
geom_sf(aes(fill = `Polity datasets IV category`)) +
xlab("Longitude") +
ylab("Latitude")

https://stackoverflow.com/questions/65784283
复制相似问题