有多种绘制地理数据的方法,一个好的和简单的开始是ggplot2
结合maps
提供地理多边形映射世界国家的包使用。
您可以使用world
地图数据集并将其与您的数据合并,例如基于国家名称,以获得地理坐标。我还没有完全理解您想要绘制的内容,但您的基本情况可能是:
数据
structure(list(Country = c("India", "China", "Mexico", "United States",
"United States", "United States", "United States", "United States"
), Country_code = c("IN", "CN", "MX", "US", "US", "US", "US",
"US"), Id = c(40, 3781, 40, 1000, 3781, 40, 65, 815), Unique_person = c(2,
1, 1, 14, 566, 43, 908, 78)), class = c("spec_tbl_df", "tbl_df",
"tbl", "data.frame"), row.names = c(NA, -8L), spec = structure(list(
cols = list(Country = structure(list(), class = c("collector_character",
"collector")), Country_code = structure(list(), class = c("collector_character",
"collector")), Id = structure(list(), class = c("collector_double",
"collector")), Unique_person = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))
代码
library(ggplot2)
library(maps)
data$Country[data$Country == 'United States'] <- "USA" # names are different for USA in datasets
data <- merge(data,
map_data("world"), by.x = "Country", by.y = "region") %>%
arrange(Country, order)
ggplot() +
# world map
geom_polygon(data = map_data("world"),
aes(x=long, y = lat, group = group),
fill = "grey50") +
# custom map
geom_polygon(data = data,
aes(x=long, y = lat, group = group, fill = Id))