J'ai un ensemble de données comprenant plus de 50 000 données. Cet ensemble de données comprend l'objet, la longitude et la latitude (avec la structure suivante).Colorisation de la carte ggplot en fonction du nombre d'habitants
coord <- data.frame(object = c("a","b","c","d","e","f","h","i","j","k"), LON = c(-73.95 ,-73.97,-73.95,-73.91,-73.91,-73.93,-73.96, -73.90,-73.97,-73.94),LAT =c(40.60,40.68,40.68,40.85,40.83,40.72,40.65, 40.74, 40.69, 40.82))
Je prévois de coloriser différents districts de la carte. J'ai trouvé l'exemple de code ci-dessous.
library(rgeos)
library(maptools)
library(geojsonio)
library(ggplot2)
URL <- "http://services5.arcgis.com/GfwWNkhOj9bNBqoJ/arcgis/rest/services/nycd/FeatureServer/0/query?where=1=1&outFields=*&outSR=4326&f=geojson"
fil <- "nyc_community_districts.geojson"
if (!file.exists(fil)) download.file(URL, fil)
nyc_districts <- geojson_read(fil, what="sp")
nyc_districts_map <- fortify(nyc_districts, region="BoroCD")
mids <- cbind.data.frame(as.data.frame(gCentroid(nyc_districts, byid=TRUE)),id=nyc_districts$BoroCD)
gg <- ggplot()
gg <- gg + geom_map(data=nyc_districts_map, map=nyc_districts_map,
aes(x=long, y=lat, map_id=id),
color="#2b2b2b", size=0.15, fill=NA)
gg <- gg + geom_text(data=mids, aes(x=x, y=y, label=id), size=2)
gg <- gg + coord_map()
gg <- gg + ggthemes::theme_map()
Ensuite, j'ai utilisé ce code pour colorisation district différent
library(viridis)
# make up some fill data
set.seed(1492)
df <- data.frame(LON = c(-73.95770,-73.95445,-73.96215,-73.97739,-73.98136,-73.93974,-73.95061, -73.98609 ,-73.95724,-73.93351,-73.94931),LAT =c(40.81099,40.71415,40.71025,40.68983,40.66715,40.79821,40.66830, 40.68980, 40.71636, 40.76621, 40.80273))
pointsSp <- SpatialPoints(coords = df[,c("LON", "LAT")],
proj4string = CRS(proj4string(nyc_districts)))
nyc_districts$pointCount <- 0
for(i in 1:length(nyc_districts)){
x <- nyc_districts[i,]
nyc_districts$pointCount[i] <- length(x[pointsSp,])
}
choro <- data.frame([email protected]$BoroCD,
fill=sample(100, nrow([email protected])))
gg <- ggplot()
gg <- gg + geom_map(data=nyc_districts_map, map=nyc_districts_map,
aes(x=long, y=lat, map_id=id),
color="#2b2b2b", size=0.15, fill=NA)
gg <- gg + geom_map(data=choro, map=nyc_districts_map,
aes(fill=fill, map_id=district),
color="#2b2b2b", size=0.15)
gg <- gg + geom_point(data=coord, aes(x=LON, y=LAT),
colour= "red", alpha=1,na.rm=T)
gg <- gg + scale_fill_viridis(name="Pick ups\ndistribution") + geom_text(data=mids, aes(x=x, y=y, label=id), size=2)
gg <- gg + coord_map()
gg <- gg + ggthemes::theme_map()
gg <- gg + theme(legend.position=c(0.1,0.5))
gg
Dans le code ci-dessus, avec geom point je peux montrer l'emplacement de coordination différente sur la carte. Mais ma colorisation de district est fausse. pouvez-vous s'il vous plaît me dire comment puis-je coloriser ma carte basée sur "coord" ensemble de données.
@hrbrmstr peut vous aider à me s'il vous plaît à ce sujet. – Behzad
Je vais essayer dès que je suis à la maison. Ici, je ne peux pas accéder au fichier que vous souhaitez télécharger sur le site Web ... – Umberto