I want to present data on a map of our country.
I have used R for showing the clusters on a country! Here the steps I took:
setwd('D:/r/cluster2')
channel <- odbcConnectExcel('cluster.xls')
data <- sqlFetch(channel, 'clust9')
y9 <- data.frame(inf=data$infest, faible=data$faible, moyen=data$moyen, fort=data$fort, lon=data$Lon, lat=data$Lat)
library(fossil)
d = earth.dist(y9)
km <- kmeans(d,centers=5)
hc <- hclust(d)
clust <- cutree(hc, k=5 )
set.seed(123)
plot(hc)
y9$clust <- cutree(hc,k=5)
map.AL <- readOGR(dsn="D:/r/cluster/shp", layer="ALG_boundaries")
map.df <- fortify(map.AL)
ggplot(map.df)+ geom_path(aes(x=long, y=lat, group=group))+
geom_point(data=y9, aes(x=long, y=lat, color=factor(clust)), size=4)+
scale_color_discrete("Cluster") + coord_fixed()
On the map, it's showing just points for the regions that have the same characteristics. However, what I want to show is all the region colored not just a point.
Best Answer
You can create a new column into the spatial data "data.frame" and then, assign to each province its respective cluster label. After that, use in ggplot the layers
geom_polygon
withscale_fill_manual
to address each cluster to a specific color.Here is one example: