R 填充一个世界+;美国州地图

R 填充一个世界+;美国州地图,r,ggplot2,R,Ggplot2,我正试图创建一张世界和美国各州的地图,上面有一些分类变量。我的想法是绘制世界地图,然后在上面绘制美国州地图: library(maps) library(ggplot2) library(dplyr) states_map <- map_data("state") world_map <- map_data("world") world_map <- world_map %>% filter(region != "Antarctica") states <

我正试图创建一张世界和美国各州的地图,上面有一些分类变量。我的想法是绘制世界地图,然后在上面绘制美国州地图:

library(maps)
library(ggplot2)
library(dplyr)

states_map <- map_data("state")
world_map <- map_data("world")

world_map <- world_map %>%
  filter(region != "Antarctica")

states <- c("texas")

world <- c("Alaska",
          "Canada",
          "France")

world_map$region <- ifelse(world_map$subregion == "Alaska", "Alaska", world_map$region)

world_map$status <- ifelse(world_map$region %in% world, TRUE, FALSE)
states_map$status <- ifelse(states_map$region %in% states, TRUE, FALSE)

ggplot() + 
  geom_map(aes(map_id = region, fill = status), 
           map = world_map, 
           data = world_map, 
           color = "black") + 
  geom_map(aes(map_id = region, fill = status), 
           map = states_map, 
           data = states_map, 
           color = "black") + 
  expand_limits(x = world_map$long, y = world_map$lat)
库(地图)
图书馆(GG2)
图书馆(dplyr)

states\u map问题在于您使用的国家/地区数据集不适合此用途。它包含许多重叠的形状,这会导致着色问题,而且国家标签也很差

法国本土实际上是558集团,在其区域或子区域列中没有数据:

ggplot() + 
  geom_map(aes(map_id = region, fill = status), 
           map = world_map[world_map$group == 558,], 
           data = world_map[world_map$group == 558,], 
           color = "black") + 
  expand_limits(x = world_map$long, y = world_map$lat)

这也是在一些描述欧洲的形状下,即使你直接给558组上色,也会隐藏它的颜色

map\u data(“world2”)
似乎没有这个问题,但使用的透视图不同:


我建议下载更好的文档化形状文件,并使用它们来映射。

有趣的问题。“德克萨斯”不大写或者“阿拉斯加”真的是一个州,这有关系吗?阿拉斯加没有出现在州地图上(夏威夷也没有)。“region”变量在state_map数据中没有大写,因此为了稍后进行匹配(ifelse语句),“texas”不应该大写。