我愿绘制一份显示国家边界、各县边界和地形背景的堪萨斯州地图。 为此,我使用以下图书馆:<代码>ggmap和>>mapdata
。
library(ggmap)
library(ggthemes)
library(dplyr)
library(mapdata)
ks_state_map_df <- map_data("state", region="kansas")
ks_counties_map_df <- map_data("county") %>% filter(region=="kansas")
ks_cities_map_df <- us.cities %>% filter(country.etc=="KS")
ks_cities_map_df$name <- gsub(" KS","", ks_cities_map_df$name)
KS_area <- c(left=-102.051744, right=-94.588413, bottom=36.993016, top=40.003162)
KS_map <- get_stadiamap(bbox=KS_area, maptype= outdoors )
ggmap(KS_map) +
theme_map() +
geom_polygon(data=ks_state_map_df, mapping=aes(x=long, y=lat, group=group), color="black", fill=NA) +
geom_polygon(data=ks_counties_map_df, mapping=aes(x=long, y=lat), color="gray", fill=NA) +
geom_point(data=ks_cities_map_df, mapping=aes(x=long, y=lat)) +
geom_text(data=ks_cities_map_df, mapping=aes(x=long, y=lat, label=name), hjust=0, nudge_x=0.1)
However, it seems that my above code attempt does not render the polygons correctly (something to do with the group attribute, I believe). How do I need to fix my code?
Edit 1:
The terrain map areas should be increased to: KS_area <- c(left=-102.2, right=-94.4, bottom=37.2, top=40.2)