I'm currently on the cusp of getting done what I want to get done - but having a slight problem with referring to my data from within R's sp
SpatialPolygonDataFrame
object. The issue is how ggplot2
deals with spatial polygons, and factors that define or are mapped to them. I have my polygons, and in the @data
element, I have the relevant data I'm interested in mapping/visualising, in a SpatialPolygonDataFrame called plotData. The following code;
p = ggplot(plotData, aes(x = long, y = lat, group = id))
p + geom_polygon(fill=plotData@data$total,color='black')
yields an error about the number of elements within the aesthetic call:
Error: Aesthetics must be either length 1 or the same as the data (1651526): colour, fill
I've tried a number of different ways of accessing these data - but am not able to actually figure out how to colour it based on anything within the @data element. The difference is something to do with how ggplot2
treats polygons - (my data has only 2208 unique elements, and 2208 unique polys, not 1.6 million), but I can't seem to identify how this is put together.
tmap
package or convert your sp class object to sf class with the sf package.