| Title: | Moran, LISA and Getis-Ord Maps for Teaching Spatial Statistics |
|---|---|
| Description: | Functions written for Penn State spatial statistics courses. Each function takes an 'sf' dataset, a column name, a neighbour list and spatial weights, and makes one finished plot: a Moran scatterplot, a LISA (Local Moran's I) map, Getis-Ord Gi and Gi* hot spot maps, or a four-map summary figure. The maps are built with 'tmap' and are sized to fit R Markdown and Quarto reports. |
| Authors: | Helen Greatrex [aut, cre] |
| Maintainer: | Helen Greatrex <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.3.0 |
| Built: | 2026-11-23 08:08:30 UTC |
| Source: | https://github.com/psu-spatial/sfdepmap |
Makes one figure with four maps and one caption underneath:
A. your data as a choropleth map (five groups, Jenks natural breaks),
B. LISA clusters, C. Gi* hot and cold spots, D. Gi hot and cold spots.
In R Markdown, a chunk with fig.width=9, fig.height=11 gives it enough room.
autocorrelation_summary( data, variable, label, neighbours, weights, title = variable, data_source = NULL, mode = "plot", basemap = FALSE, significance = 0.05, nsim = 499, seed = NULL, explanation = TRUE, global_stats = TRUE, units = "Value", adjust_p = FALSE )autocorrelation_summary( data, variable, label, neighbours, weights, title = variable, data_source = NULL, mode = "plot", basemap = FALSE, significance = 0.05, nsim = 499, seed = NULL, explanation = TRUE, global_stats = TRUE, units = "Value", adjust_p = FALSE )
data |
Your sf object, for example |
variable |
The NAME of the column you want to study, in "quotes". |
label |
The NAME of a column that labels each row, in "quotes" (for example |
neighbours |
The neighbours you made, for example with |
weights |
The weights you made with |
title |
A description of your variable, used on the plots. |
data_source |
A line of text saying where your data came from, shown under the map.
Leave it out (or use |
mode |
|
basemap |
|
significance |
The p-value cut-off. The default is 0.05. |
nsim |
How many random shuffles are used for the p-values. The default is 499. |
seed |
The random seed. Leave it out and R picks a new one each time. Give a whole number to get the same map every time. |
explanation |
|
global_stats |
|
units |
What your numbers are, for example |
adjust_p |
|
A figure. Type its name to see it.
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) nc <- sf::st_transform(nc, 32019) # a flat (projected) map of North Carolina NeighbourMatrix <- sfdep::st_contiguity(nc) WeightsMatrix <- sfdep::st_weights(NeighbourMatrix, style = "W", allow_zero = TRUE) Summary <- autocorrelation_summary(nc, "BIR74", "NAME", NeighbourMatrix, WeightsMatrix, title = "Births in 1974", units = "Births", seed = 364)nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) nc <- sf::st_transform(nc, 32019) # a flat (projected) map of North Carolina NeighbourMatrix <- sfdep::st_contiguity(nc) WeightsMatrix <- sfdep::st_weights(NeighbourMatrix, style = "W", allow_zero = TRUE) Summary <- autocorrelation_summary(nc, "BIR74", "NAME", NeighbourMatrix, WeightsMatrix, title = "Births in 1974", units = "Births", seed = 364)
Both give every area a z-score and a p-value. A positive z-score with a
small p-value is a hot spot (high values clustered together) and a negative
z-score with a small p-value is a cold spot. The p-values come from random
shuffles of the data, using sfdep::local_g_perm() and
sfdep::local_gstar_perm(). Areas are put into 7 groups: 90%, 95% and 99%
confidence mean p <= 0.10, 0.05 and 0.01.
getisstar_map( data, variable, label, neighbours, weights, title = variable, data_source = NULL, mode = "plot", palette = 1, basemap = FALSE, nsim = 499, seed = NULL, adjust_p = FALSE, silent = FALSE, return_data = FALSE ) getis_map( data, variable, label, neighbours, weights, title = variable, data_source = NULL, mode = "plot", palette = 1, basemap = FALSE, nsim = 499, seed = NULL, adjust_p = FALSE, silent = FALSE, return_data = FALSE )getisstar_map( data, variable, label, neighbours, weights, title = variable, data_source = NULL, mode = "plot", palette = 1, basemap = FALSE, nsim = 499, seed = NULL, adjust_p = FALSE, silent = FALSE, return_data = FALSE ) getis_map( data, variable, label, neighbours, weights, title = variable, data_source = NULL, mode = "plot", palette = 1, basemap = FALSE, nsim = 499, seed = NULL, adjust_p = FALSE, silent = FALSE, return_data = FALSE )
data |
Your sf object, for example |
variable |
The NAME of the column you want to study, in "quotes". |
label |
The NAME of a column that labels each row, in "quotes" (for example |
neighbours |
The neighbours you made, for example with |
weights |
The weights you made with |
title |
A description of your variable, used on the plots. |
data_source |
A line of text saying where your data came from, shown under the map.
Leave it out (or use |
mode |
|
palette |
Which colours to use: a palette number (1 to 5), a palette name
( |
basemap |
|
nsim |
How many random shuffles are used for the p-values. The default is 499. |
seed |
The random seed. Leave it out and R picks a new one each time. Give a whole number to get the same map every time. |
adjust_p |
|
silent |
|
return_data |
|
getisstar_map() makes a Gi* ("G-i-star") map: each area is counted
TOGETHER WITH its neighbours. getis_map() makes a Gi map: each area's own
value is LEFT OUT and only its neighbours are used. The map changes
slightly each time unless you give a seed. The seed that was used is
always written under the map.
A map. Type its name to see it.
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) nc <- sf::st_transform(nc, 32019) # a flat (projected) map of North Carolina NeighbourMatrix <- sfdep::st_contiguity(nc) WeightsMatrix <- sfdep::st_weights(NeighbourMatrix, style = "W", allow_zero = TRUE) GETISSTARmap <- getisstar_map(nc, "BIR74", "NAME", NeighbourMatrix, WeightsMatrix, title = "Births in 1974", seed = 364) GETISmap <- getis_map(nc, "BIR74", "NAME", NeighbourMatrix, WeightsMatrix, title = "Births in 1974", seed = 364, adjust_p = TRUE)nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) nc <- sf::st_transform(nc, 32019) # a flat (projected) map of North Carolina NeighbourMatrix <- sfdep::st_contiguity(nc) WeightsMatrix <- sfdep::st_weights(NeighbourMatrix, style = "W", allow_zero = TRUE) GETISSTARmap <- getisstar_map(nc, "BIR74", "NAME", NeighbourMatrix, WeightsMatrix, title = "Births in 1974", seed = 364) GETISmap <- getis_map(nc, "BIR74", "NAME", NeighbourMatrix, WeightsMatrix, title = "Births in 1974", seed = 364, adjust_p = TRUE)
Puts each area into a group (High-High, High-Low, Low-High, Low-Low or
not significant), prints a table of how many areas are in each group, and
makes a finished map. The p-values come from random shuffles of the data,
so the map changes slightly each time unless you give a seed. The seed
that was used is always written under the map.
lisa_map( data, variable, label, neighbours, weights, title = variable, data_source = NULL, mode = "plot", significance = 0.05, nsim = 499, global_stats = FALSE, palette = 1, basemap = FALSE, seed = NULL, silent = FALSE, return_data = FALSE )lisa_map( data, variable, label, neighbours, weights, title = variable, data_source = NULL, mode = "plot", significance = 0.05, nsim = 499, global_stats = FALSE, palette = 1, basemap = FALSE, seed = NULL, silent = FALSE, return_data = FALSE )
data |
Your sf object, for example |
variable |
The NAME of the column you want to study, in "quotes". |
label |
The NAME of a column that labels each row, in "quotes" (for example |
neighbours |
The neighbours you made, for example with |
weights |
The weights you made with |
title |
A description of your variable, used on the plots. |
data_source |
A line of text saying where your data came from, shown under the map.
Leave it out (or use |
mode |
|
significance |
The p-value cut-off. The default is 0.05. |
nsim |
How many random shuffles are used for the p-values. The default is 499. |
global_stats |
|
palette |
Which colours to use: a palette number (1 to 6), a palette name
( |
basemap |
|
seed |
The random seed. Leave it out and R picks a new one each time. Give a whole number to get the same map every time. |
silent |
|
return_data |
|
A map. Type its name to see it. In R Markdown, a chunk with
fig.width=7, fig.height=6 gives it enough room.
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) nc <- sf::st_transform(nc, 32019) # a flat (projected) map of North Carolina NeighbourMatrix <- sfdep::st_contiguity(nc) WeightsMatrix <- sfdep::st_weights(NeighbourMatrix, style = "W", allow_zero = TRUE) LISAmap <- lisa_map(nc, "BIR74", "NAME", NeighbourMatrix, WeightsMatrix, title = "Births in 1974", seed = 364)nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) nc <- sf::st_transform(nc, 32019) # a flat (projected) map of North Carolina NeighbourMatrix <- sfdep::st_contiguity(nc) WeightsMatrix <- sfdep::st_weights(NeighbourMatrix, style = "W", allow_zero = TRUE) LISAmap <- lisa_map(nc, "BIR74", "NAME", NeighbourMatrix, WeightsMatrix, title = "Births in 1974", seed = 364)
The same plot as moran_scatterplot(), with every point coloured by its LISA
group (High-High, High-Low, Low-High, Low-Low). Points that are not significant
at your chosen p-value are grey. The groups and p-values are the ones
lisa_map() uses, so with the same significance, nsim and seed the plot
matches the map. The seed that was used is always written under the plot.
lisa_scatterplot( data, variable, label, neighbours, weights, title = variable, standardise = FALSE, annotate = FALSE, palette = 1, significance = 0.05, nsim = 499, seed = NULL )lisa_scatterplot( data, variable, label, neighbours, weights, title = variable, standardise = FALSE, annotate = FALSE, palette = 1, significance = 0.05, nsim = 499, seed = NULL )
data |
Your sf object, for example |
variable |
The NAME of the column you want to study, in "quotes". |
label |
The NAME of a column that labels each row, in "quotes" (for example |
neighbours |
The neighbours you made, for example with |
weights |
The weights you made with |
title |
A description of your variable, used on the plots. |
standardise |
|
annotate |
|
palette |
The colours for the four LISA groups. These are the same palettes as
|
significance |
The p-value cut-off. The default is 0.05. |
nsim |
How many random shuffles are used for the p-values. The default is 499. |
seed |
The random seed. Leave it out and R picks a new one each time. |
A point's LISA group can differ from the quadrant it sits in when it is very close to one of the dashed lines, because the LISA calculation measures the neighbours' average against the overall mean and the dashed line shows the mean of the neighbours' averages.
The plot is drawn. The numbers behind it are returned invisibly, with a
lisa_group column.
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) nc <- sf::st_transform(nc, 32019) # a flat (projected) map of North Carolina NeighbourMatrix <- sfdep::st_contiguity(nc) WeightsMatrix <- sfdep::st_weights(NeighbourMatrix, style = "W", allow_zero = TRUE) lisa_scatterplot(nc, "BIR74", "NAME", NeighbourMatrix, WeightsMatrix, title = "Births in 1974", seed = 364)nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) nc <- sf::st_transform(nc, 32019) # a flat (projected) map of North Carolina NeighbourMatrix <- sfdep::st_contiguity(nc) WeightsMatrix <- sfdep::st_weights(NeighbourMatrix, style = "W", allow_zero = TRUE) lisa_scatterplot(nc, "BIR74", "NAME", NeighbourMatrix, WeightsMatrix, title = "Births in 1974", seed = 364)
Plots the value in each area (x axis) against the average value in that area's neighbours, the "spatial lag" (y axis). The most unusual points are labelled using your label column.
moran_scatterplot( data, variable, label, neighbours, weights, title = variable, standardise = FALSE, annotate = FALSE, palette = 1 )moran_scatterplot( data, variable, label, neighbours, weights, title = variable, standardise = FALSE, annotate = FALSE, palette = 1 )
data |
Your sf object, for example |
variable |
The NAME of the column you want to study, in "quotes". |
label |
The NAME of a column that labels each row, in "quotes" (for example |
neighbours |
The neighbours you made, for example with |
weights |
The weights you made with |
title |
A description of your variable, used on the plots. |
standardise |
|
annotate |
|
palette |
The colours used by |
Areas with no neighbours, or with a missing value, are left off the plot, and a
message says which ones. With row-standardised weights (style = "W"), the slope of
the line is Moran's I for the areas on the plot.
The plot is drawn. The numbers behind it are returned invisibly: the x value,
the spatial lag (wx), whether the point was labelled (is_inf) and the label.
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) nc <- sf::st_transform(nc, 32019) # a flat (projected) map of North Carolina NeighbourMatrix <- sfdep::st_contiguity(nc) WeightsMatrix <- sfdep::st_weights(NeighbourMatrix, style = "W", allow_zero = TRUE) moran_scatterplot(nc, "BIR74", "NAME", NeighbourMatrix, WeightsMatrix, title = "Births in 1974") moran_scatterplot(nc, "BIR74", "NAME", NeighbourMatrix, WeightsMatrix, title = "Births in 1974", standardise = TRUE, annotate = TRUE)nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) nc <- sf::st_transform(nc, 32019) # a flat (projected) map of North Carolina NeighbourMatrix <- sfdep::st_contiguity(nc) WeightsMatrix <- sfdep::st_weights(NeighbourMatrix, style = "W", allow_zero = TRUE) moran_scatterplot(nc, "BIR74", "NAME", NeighbourMatrix, WeightsMatrix, title = "Births in 1974") moran_scatterplot(nc, "BIR74", "NAME", NeighbourMatrix, WeightsMatrix, title = "Births in 1974", standardise = TRUE, annotate = TRUE)
These run for you when you type the name of a map, or when a map is printed in R Markdown or Quarto. You do not need to call them yourself.
## S3 method for class 'autocorrelation_map' print(x, ...) ## S3 method for class 'autocorrelation_map' knit_print(x, ...)## S3 method for class 'autocorrelation_map' print(x, ...) ## S3 method for class 'autocorrelation_map' knit_print(x, ...)
x |
A map made by |
... |
Not used. |
The map, invisibly.