1

我正在尝试将美国城市添加到我的 R 地图中。

当我添加世界城市时:

ggplot(data = usa) +
  geom_polygon(aes(x = longitude, y = latitude, group = group, fill = id)) +
  coord_quickmap() + 
  guides(fill = FALSE) +
  theme(axis.text = element_blank()) +
  theme(axis.ticks = element_blank()) +
  theme(axis.title = element_blank()) +
  xlim(-169, -67) +
  ylim(17,72)+
  scale_fill_discrete()+
  geom_point(data=world.cities, aes(x=long, y = lat, size= capital))

或加拿大城市:

ggplot(data = usa) +
  geom_polygon(aes(x = longitude, y = latitude, group = group, fill = id)) +
  coord_quickmap() + 
  guides(fill = FALSE) +
  theme(axis.text = element_blank()) +
  theme(axis.ticks = element_blank()) +
  theme(axis.title = element_blank()) +
  xlim(-169, -67) +
  ylim(17,72)+
  scale_fill_discrete()+
  geom_point(data=canada.cities, aes(x=long, y = lat, size= capital))

它工作得很好。但是,当我尝试将它替换us.cities为应该是maps包的一部分(https://cran.r-project.org/web/packages/maps/maps.pdf)时,它会给出错误:

ggplot2 不知道如何处理类字符的数据。

关于可能是什么问题的任何想法?

编辑:更多代码:

library(maps)
library(fiftystater)

usa <- data("fifty_states")
us.cities <- data(us.cities)
usa.cities <- world.cities %>% filter(country.etc=="USA")
map <- map_data("world")
hawaii <- read.csv("hawaii.csv", stringsAsFactors = FALSE)
alaska <- read.csv("alaska.csv", stringsAsFactors = FALSE)
forty8states <- fifty_states %>% filter(id != "hawaii") %>% filter(id    !="alaska") %>% select(long, lat, id, group)

alaska <- read.csv("alaska.csv", stringsAsFactors = FALSE)
hawaii <- read.csv("hawaii.csv", stringsAsFactors = FALSE)

usa <- rbind(forty8states, alaska, hawaii)
usa <- usa %>% mutate(longitude = round(long, digits = 2)) 
usa <- usa %>% mutate(latitude = round(lat, digits = 2))

hawaii 和 alaska csv 是来自world数据框中的 hawaii 和 alaska 数据maps,经过一些编辑,因此它们的格式与其余部分相同。

4

0 回答 0