I want to create a facet with nine (in this example only three) maps in one plot. I have managed to plot almost ideally one map when I subset from the original shapefile. However, when I try to plot them all at once it is not possible.
The plot needs to have the same legend (discrete numbers as values 1, 2, 3, 4, 5) even when some of the maps only has values from 1 to 4.
In addition, when for one of the polygons there is missing data, it should be plotted in gray, with legend NA value.
An example of the output from the code below is at the bottom. An example data is available here.
path <- '~path'
muniCluster <- rgdal::readOGR(dsn=path, layer="data")
class(muniCluster)
[1] "SpatialPolygonsDataFrame"
attr(,"package")
[1] "sp"
ilum <- subset(muniCluster, CLUSTER == "CLUS_ILUM")
ilum$VALUES <- as.integer(ilum$VALUES)
ilum_df <- fortify(ilum)
ilum_tidy <- tidy(ilum)
class(ilum_df)
class(ilum_tidy)
# Recategorizes data as required for plotting
ilum$id <- row.names(ilum)
ilum_tidy <- left_join(ilum_tidy, ilum@data)
ilum_tidy$VALUES <- as.factor(ilum_tidy$VALUES)
ilum_map_v2 <- ggplot(ilum_tidy, aes(x = long, y = lat, group = group, fill = VALUES)) +
geom_polygon(color = "black", size = 0.1) +
labs(title = "Light cluster") +
scale_fill_viridis(discrete=TRUE)
ilum_map_final_v2 <- ilum_map_v2 + coord_map()
print(ilum_map_final_v2)

