geom_map - 名称(地图)不正确

geom_map - names(map) not TRUE

我在尝试制作地图时一直收到此错误...

Error in geom_map(data = all_states, map = all_states, mapping = aes(map_id = State, : all(c("x", "y", "id") %in% names(map)) is not TRUE

到目前为止我的代码...

all_states = read.csv(file = "https://public.opendatasoft.com/explore/dataset/us-zip-code-latitude-and-longitude/download/?format=csv&timezone=America/New_York&use_labels_for_header=true",
                      header = TRUE,
                      sep = ";")

all_states$State = state.name[match(all_states$State, state.abb)]
all_states = na.omit(all_states)

ggplot(data = all_states, aes(map_id = State)) + 
  geom_map(data = all_states,
           map = all_states,
           mapping = aes(map_id=State,x=Longitude,y=Latitude)) +
  coord_fixed()

我做错了什么?

2个问题。

  • 您没有下载正确的地图。 geom_map 需要用于创建多边形的数据,但您的数据包含城市的坐标
  • geom_map 对数据框中的列名非常奇特和限制

解决方案

  • 获取正确的地图(例如,只需使用美国的地图包)
  • 重命名列

我还删除了一两行和 'fortified' 数据框,因为通常建议在将其用于地图之前这样做。

library(tidyverse)
all_states = read.csv(file = "https://public.opendatasoft.com/explore/dataset/us-zip-code-latitude-and-longitude/download/?format=csv&timezone=America/New_York&use_labels_for_header=true", header = TRUE, sep = ";")

all_states = na.omit(all_states) %>% 
  mutate(region = State, long=Longitude, lat = Latitude) %>%fortify

US <- map_data('usa')
#> 
#> Attaching package: 'maps'

#>     map
ggplot()+
  geom_map(data = US, map = US, mapping = aes( map_id = region, x = long, y = lat), fill = 'white') +
    # now this is the US background
  geom_point(data = filter(all_states, ! region %in% c('HI','AK','AS')), aes(x = long, y = lat), size = .01, color = 'black')
    # and this is YOUR data. Use geom_point for it!!! 
    #I have removed Alaska, Hawaii and a third little bit which I ignorantly don't know. 'AS'. 

#> Warning: Ignoring unknown aesthetics: x, y

reprex package (v0.2.1)

于 2019-08-02 创建