I would like to create a map showing local spatial cluster of a phenomenon, preferably using Local Moran (LISA).
In the reproducible example below, I calculate the local moran's index using spdep
but I would like to know if there is as simple way to map the clustes, prefebly using ggplot2
. Help ?
library(UScensus2000tract)
library(ggplot2)
library(spdep)
# load data
data("oregon.tract")
# plot Census Tract map
plot(oregon.tract)
# create Queens contiguity matrix
spatmatrix <- poly2nb(oregon.tract)
#calculate the local moran of the distribution of black population
lmoran <- localmoran(oregon.tract@data$black, nb2listw(spatmatrix))
Now to make this example more similar to my real dataset, I have some NA
values in my shape file, which represent holes in the polygon, so these areas shouldn't be used in the calculation.
oregon.tract@data$black[3:5] <- NA
Here is a strategy:
This answer was based on this page, suggested by Eli Knaap on twitter, and also borrowed from the answer by @timelyportfolio to this question.
I used the variable
white
instead ofblack
becauseblack
had less explicit results.Concerning NAs,
localmoran()
includes the argumentna.action
, about which the documentation says:I tried:
But run into problems in
lag.listw
and did not have time to look into it. Sorry.I don't think this answer is worthy of a bounty, but perhaps it will get you closer to an answer. Since I don't know anything about
localmoran
, I just guessed at a fill.