0
votes

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)

enter image description here

1

1 Answers

1
votes

Nowadays is probably easier to use the sfpackage to plot the type of maps you want. You can see some examples here https://r-spatial.github.io/sf/articles/sf5.html

I adapted an example from there, which shows how to use ggplot2 and its facet_wrap function to create a map for each level of a given variable.

Some steps shown here may not be necessary if you already have, for example, a variable with certain amount of levels which you which to facet.

library(sf)
library(ggplot2)
library(tidyr)
library(dplyr)
library(classInt)
library(viridis)

# Read example shapefile from sf package
nc <- st_read(system.file("shape/nc.shp", package="sf"))

# subset columns of interest as well as geometry column
# create BIR in which the variables BIR74, BIR79, NWBIR79
# become different levels of it
nc2 <- nc %>% select(BIR74, BIR79, NWBIR79, geometry) %>% gather(VAR, BIR, -geometry)

# HEre i just wanted to create 5 categories for the BIR variable
ints <- classIntervals(nc2$BIR, n = 5, style = "jenks")
nc2 <- nc2 %>% mutate(BIR_cat = cut(BIR, ints$brks, dig.lab=10)) 

# I just changed the levels's labels to match the output you are looking for
nc2 <- nc2 %>% mutate(values = ifelse(BIR_cat == "(3,1946]", "1", 
                                ifelse(BIR_cat == "(1946,4706]", "2", 
                                 ifelse(BIR_cat == "(4706,9087]", "3",
                                  ifelse(BIR_cat == "(9087,16184]", "4",
                                    ifelse(BIR_cat == "(16184,30757]", "5", NA))))))

# Map the spatial data
ggplot() +
 geom_sf(data = nc2, aes(fill = values)) +
 facet_wrap(~VAR, ncol = 1) +
 scale_fill_viridis(discrete=TRUE) 

enter image description here