好的,所以我在科罗拉多州的R女佣地图。我希望能够选择县。我的数据给了我县的名称,但我不知道如何将它应用到我的ggmap。
这是根据我的多边形数据给出的所有县的名称
Colorado@data[["NAME_2"]]
[1] "Adams" "Alamosa" "Arapahoe" "Archuleta" "Baca"
[6] "Bent" "Boulder" "Broomfield" "Chaffee" "Cheyenne"
[11] "Clear Creek" "Conejos" "Costilla" "Crowley" "Custer"
[16] "Delta" "Denver" "Dolores" "Douglas" "Eagle"
[21] "El Paso" "Elbert" "Fremont" "Garfield" "Gilpin"
[26] "Grand" "Gunnison" "Hinsdale" "Huerfano" "Jackson"
[31] "Jefferson" "Kiowa" "Kit Carson" "La Plata" "Lake"
[36] "Larimer" "Las Animas" "Lincoln" "Logan" "Mesa"
[41] "Mineral" "Moffat" "Montezuma" "Montrose" "Morgan"
[46] "Otero" "Ouray" "Park" "Phillips" "Pitkin"
[51] "Prowers" "Pueblo" "Rio Blanco" "Rio Grande" "Routt"
[56] "Saguache" "San Juan" "San Miguel" "Sedgwick" "Summit"
[61] "Teller" "Washington" "Weld" "Yuma"
我希望能够选择其中任何一个县并将其涂成一些颜色。
我也有路径来获取我的数据。我一直在环顾四周,似乎无法将它们融合在一起。任何帮助,将不胜感激
代码
library(raster)
library(ggplot2)
library(rgdal)
#calling our state
states <- c('Colorado')
#getting our countys and states
Co <- getData("GADM",country="USA",level=2)
Colorado <- Co[Co$NAME_1 %in% states,]
# getting map
bm <- ggmap(get_map(location = c(-105.56, 39),
maptype = "hybrid", zoom = 7))
#overlaying our polygon onto ggmap
gg <- bm + geom_polygon(data = Colorado, aes(y=lat,x=long, group=group),
alpha = 0, color = "red" )
gg + geom_path() + coord_map()
答案 0 :(得分:1)
您可以对数据|> HttpBuilder.withStringBody "text/plain" """{ "sortBy": "coolness", "take": 10 }"""
进行分组,以提取特定县的信息,例如“埃尔帕索”:
Colorado
使用子集数据创建填充多边形。
subset(Colorado, Colorado$NAME_2 == "El Paso")
然后你可以将它包装在一个函数中,# Select county and fill color
county <- "El Paso"
county_fill_color <- "pink"
# overlaying our polygon onto ggmap
gg <- bm + geom_polygon(data = Colorado, aes(y=lat, x=long, group=group), alpha = 0, color = "red" )
gg <- gg + geom_path() + coord_map()
# overlay selected county polygon with fill and alpha parameters
gg + geom_polygon(data = subset(Colorado, Colorado$NAME_2 == county), aes(y=lat, x=long, group=group), alpha = 0.75, fill = county_fill_color)
和county
是函数的参数。