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
with scale_fill_manual
to address each cluster to a specific color.
Here is one example:
#Import shapefiles
require(rgdal)
#Read shaple file with Algeria's province boundaries (download on: http://www.diva-gis.org/gdata)
alg_provinces = readOGR(dsn="C:...\\DZA_adm", layer="DZA_adm1")
require(ggplot2)
#Get rownames from "SpatialPolygonsDataFrame" object, slot "data"
alg_provinces@data$id = rownames(alg_provinces@data)
#Transform object of class "SpatialPolygonsDataFrame" in "data.frame"
alg.points = fortify(alg_provinces,region="id")
require(plyr)
#Join geometries (.shp file) information with information derived from the attribute table (.dbf file), in object of class "data.frame"
alg.df = join(alg.points, alg_provinces@data, by="id")
#This is the part where you will have to adapt your code.
#Here I assigned manually each cluster to its specific province.
#I hope you enjoy the fact I tried to provide my example to match yours.
cluster = data.frame(id=alg_provinces@data$id,
alg_provinces@data$NAME_1,
cluster=c("0","1","2","1","3","2","0","1","0","3",
"3","3","1","1","3","5","1","0","3","0",
"5","0","3","1","2","5","1","5","1","3",
"2","1","0","3","5","1","2","1","3","4",
"0","4","5","0","3","1","0","4"))
#Merge cluster labels with the spatial data
alg = merge(alg.df,cluster,by="id")
#Plot with ggplot using the layers "geom_polygon", "geom_path" and assign the specific cluster color with "Scale_fill_manual"
ggplot(alg) +
geom_polygon(aes(x=long,y=lat,group=NAME_1,fill=cluster)) +
scale_fill_manual(values = c("0"="white","1" = "red","2"="yellow","3"="green","4"="blue","5"="purple")) +
geom_path(aes(x=long,y=lat,group=NAME_1)) +
coord_equal() +
theme_bw() + xlab("Longitude") + ylab("Latitude")
![enter image description here](https://i.stack.imgur.com/c49WY.jpg)
You should be able to use the method you were describing above, but create your vector layer using the 'vector grid' tool under the vector>research tools menu.
This has a checkbox to align the extents and resolution of the vector grid to a raster layer, so should give you a perfect match with your original raster, and also remember to check the option to output as polygons (it's set to lines by default)!
You should then be able to calculate the area of each cell and convert back to a raster as you described above.
Best Answer
Thanks to Curlew, I was able to solve my question. I dropped the color distinctions, to allow for publication on a b/w environment. Here is what I have done:
And here is my result:
Three more questions (not sure if it is appropriate to ask further questions here, though):
is it ok to work with non-logarithmic data?
how do I know what values to add in the legend?
is there a way to plot city names only for the main cities? I know I should include those in the matrix first; after that, how do I do that? And most of all, is that recommendable in such a messy map like this one?