我使用ggplot(geom_map
)制作了地图。我的代码看起来很像这样:
gg <- ggplot()
gg <- gg +
geom_map(data=county, map=county,
aes(long, lat, map_id=region),
color="grey", fill=NA, size=0.15)
gg <- gg +
geom_map(data=state, map=state,
aes(long, lat, map_id=region),
color="black", fill=NA, size=0.5) +
geom_label_repel(data = states,
aes(long, lat, label = region),
size = 2)
如何将美国的全部资本添加到其中,也许map.cities
?
答案 0 :(得分:2)
您可以使用maps
库中名为us.cities
的数据,该数据表示其中的大写字母。您可以在geom_label
中使用ggplot
,但是您发现geom_label_repel
中的ggrepel
更干净。
library(ggplot2)
library(maps)
library(ggrepel)
data(us.cities)
capitals <- subset(us.cities, capital == 2)
capitals_notAKHI <- capitals[!(capitals$country.etc %in% "AK" | capitals$country.etc %in% "HI"), ] #exclude Alaska and Hawaii
capitals_notAKHI$city <- sub(' [^ ]*$','',capitals_notAKHI$name) # split out city for the label
ggplot(capitals_notAKHI, aes(long, lat)) +
borders("state") +
geom_point() +
coord_quickmap() +
geom_label_repel(aes(label = city), size = 2)