填写世界+美国国家地图

问题描述:

我正在尝试创建一个世界和美国各州的地图,由一些分类变量着色。我的想法是画世界地图,然后绘制了美国国家地图上它:填写世界+美国国家地图

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) 

但没有法国的正确着色,以及加拿大的大部分是不是(一些岛屿是):

world

任何想法,我哪里会出错?请注意,如果您从“世界”删除加拿大,一切顺利......

+0

有趣的问题。 “德克萨斯”不是大写还是“阿拉斯加”真的是一个国家? – ddunn801

+0

阿拉斯加州不出现在state_map(夏威夷也没有)。 “region”变量在state_map数据中没有大写,所以为了匹配稍后工作(ifelse语句),“texas”不应该大写。 – Alex

问题是您所使用的国家/地区数据集不适合此目的。它包含了许多重叠的形状,这会造成一些问题与着色,以及国家被标记不良

法国正确的实际上是一种群体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) 

france map

这也是在描述欧洲的几个形状,它隐藏它的着色,即使你直接色组558

map_data("world2")似乎不存在这个问题,但个人使用pective是不同的:

enter image description here

我建议下载更好地记录shape文件,并使用这些映射来代替。