From 19cac354c9b34e87ed72e4e77ca9d68e05d526c7 Mon Sep 17 00:00:00 2001 From: Roger Date: Fri, 2 Aug 2024 17:46:40 +0200 Subject: [PATCH] adapt and condition for tmap4 --- DESCRIPTION | 2 +- NEWS.md | 2 + man/localC.Rd | 311 +++++++++++++++++++++++++++++++++++-------- man/localmoran_bv.Rd | 12 +- man/read.gwt2nb.Rd | 2 +- vignettes/sids.Rmd | 29 +++- 6 files changed, 296 insertions(+), 62 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 6e0e8103..8258b504 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: spdep Version: 1.3-6 -Date: 2024-06-12 +Date: 2024-08-02 Title: Spatial Dependence: Weighting Schemes, Statistics Encoding: UTF-8 Authors@R: c(person("Roger", "Bivand", role = c("cre", "aut"), diff --git a/NEWS.md b/NEWS.md index 3b28c851..6fb46b5a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # Version 1.3-6 (development) +* Condition on forthcoming `tmap` 4 + * #160 handle `n.comp.nb` delay in `print.nb` and elsewhere when the total number of neighbours is large # Version 1.3-5 (2025-06-10) diff --git a/man/localC.Rd b/man/localC.Rd index f2651ca3..daef1608 100644 --- a/man/localC.Rd +++ b/man/localC.Rd @@ -103,7 +103,7 @@ if (run) { attr(C, "pseudo-p")[,6]) } # pseudo-p values probably wrongly folded https://github.com/GeoDaCenter/rgeoda/issues/28 -\dontrun{ +\donttest{ tmap_ok <- FALSE if (require(tmap, quietly=TRUE)) tmap_ok <- TRUE if (run) { @@ -126,16 +126,37 @@ if (run) { if (run) { g$PC1 <- o$x[, "PC1"] brks <- c(min(g$PC1), natural_breaks(k=6, g["PC1"]), max(g$PC1)) - if (tmap_ok) tm_shape(g) + tm_fill("PC1", breaks=brks, midpoint=0) + - tm_borders() # Fig. 1 - else pplot(g["PC1"], breaks=brks) + if (tmap_ok) { + tmap4 <- packageVersion("tmap") >= "3.99" + if (tmap4) { + tm_shape(g) + tm_polygons(fill="PC1", + fill.scale=tm_scale(values="brewer.rd_yl_gn", breaks=brks, + midpoint=0), fill.legend=tm_legend(position=tm_pos_in("left", "bottom"), + frame=FALSE, item.r=0)) + } else { + tm_shape(g) + tm_fill("PC1", breaks=brks, midpoint=0) + + tm_borders() # Fig. 1 + } + } else { + pplot(g["PC1"], breaks=brks) + } } if (run) { g$PC2 <- -1*o$x[, "PC2"] # eigenvalue sign arbitrary brks <- c(min(g$PC2), natural_breaks(k=6, g["PC2"]), max(g$PC2)) - if (tmap_ok) tm_shape(g) + tm_fill("PC2", breaks=brks, midpoint=0) + - tm_borders() # Fig. 2 - else plot(g["PC2"], breaks=brks) + if (tmap_ok) { + if (tmap4) { + tm_shape(g) + tm_polygons(fill="PC2", + fill.scale=tm_scale(values="brewer.rd_yl_gn", breaks=brks, + midpoint=0), fill.legend=tm_legend(position=tm_pos_in("left", "bottom"), + frame=FALSE, item.r=0)) + } else { + tm_shape(g) + tm_fill("PC2", breaks=brks, midpoint=0) + + tm_borders() # Fig. 2 + } + } else { + plot(g["PC2"], breaks=brks) + } } if (run) { w <- queen_weights(g) @@ -145,9 +166,20 @@ if (run) { labels=lisa_labels(lm_PC1)[1:5]) is.na(g$lm_PC1) <- g$lm_PC1 == "Not significant" g$lm_PC1 <- droplevels(g$lm_PC1) - if (tmap_ok) tm_shape(g) + tm_fill("lm_PC1", textNA="Insignificant", - colorNA="gray95") + tm_borders() # Fig. 3 - else plot(g["lm_PC1"]) + if (tmap_ok) { + if (tmap4) { + tm_shape(g) + tm_polygons(fill="lm_PC1", + fill.scale=tm_scale(values="brewer.set3", value.na="gray95", + label.na="Insignificant"), + fill.legend=tm_legend(position=tm_pos_in("left", "bottom"), + frame=FALSE, item.r=0)) + } else { + tm_shape(g) + tm_fill("lm_PC1", textNA="Insignificant", + colorNA="gray95") + tm_borders() # Fig. 3 + } + } else { + plot(g["lm_PC1"]) + } } if (run) { set.seed(1) @@ -156,9 +188,20 @@ if (run) { g$lm_PC1_spdep <- q is.na(g$lm_PC1_spdep) <- lm_PC1_spdep[,6] > 0.02 # note folded p-values g$lm_PC1_spdep <- droplevels(g$lm_PC1_spdep) - if (tmap_ok) tm_shape(g) + tm_fill("lm_PC1_spdep", textNA="Insignificant", - colorNA="gray95") + tm_borders() # rep. Fig. 3 - else plot(g["lm_PC1_spdep"]) + if (tmap_ok) { + if (tmap4) { + tm_shape(g) + tm_polygons(fill="lm_PC1_spdep", + fill.scale=tm_scale(values="brewer.set3", value.na="gray95", + label.na="Insignificant"), + fill.legend=tm_legend(position=tm_pos_in("left", "bottom"), + frame=FALSE, item.r=0)) + } else { + tm_shape(g) + tm_fill("lm_PC1_spdep", textNA="Insignificant", + colorNA="gray95") + tm_borders() # rep. Fig. 3 + } + } else { + plot(g["lm_PC1_spdep"]) + } } if (run) { lg_PC1 <- local_g(w, g["PC1"], significance_cutoff=0.01, @@ -167,15 +210,37 @@ if (run) { labels=lisa_labels(lg_PC1)[0:3]) is.na(g$lg_PC1) <- g$lg_PC1 == "Not significant" g$lg_PC1 <- droplevels(g$lg_PC1) - if (tmap_ok) tm_shape(g) + tm_fill("lg_PC1", textNA="Insignificant", - colorNA="gray95") + tm_borders() # Fig. 4 (wrong) - else plot(g["lg_PC1"]) + if (tmap_ok) { + if (tmap4) { + tm_shape(g) + tm_polygons(fill="lg_PC1", + fill.scale=tm_scale(values="brewer.set3", value.na="gray95", + label.na="Insignificant"), + fill.legend=tm_legend(position=tm_pos_in("left", "bottom"), + frame=FALSE, item.r=0)) + } else { + tm_shape(g) + tm_fill("lg_PC1", textNA="Insignificant", + colorNA="gray95") + tm_borders() # Fig. 4 (wrong) + } + } else { + plot(g["lg_PC1"]) + } g$lg_PC1a <- cut(g$PC1, c(-Inf, mean(g$PC1), Inf), labels=c("Low", "High")) is.na(g$lg_PC1a) <- lisa_pvalues(lg_PC1) >= 0.01 g$lg_PC1a <- droplevels(g$lg_PC1a) - if (tmap_ok) tm_shape(g) + tm_fill("lg_PC1", textNA="Insignificant", - colorNA="gray95") + tm_borders() # Fig. 4 (guess) - else plot(g["lg_PC1"]) + if (tmap_ok) { + if (tmap4) { + tm_shape(g) + tm_polygons(fill="lg_PC1a", + fill.scale=tm_scale(values="brewer.set3", value.na="gray95", + label.na="Insignificant"), + fill.legend=tm_legend(position=tm_pos_in("left", "bottom"), + frame=FALSE, item.r=0)) + } else { + tm_shape(g) + tm_fill("lg_PC1a", textNA="Insignificant", + colorNA="gray95") + tm_borders() # Fig. 4 (guess) + } + } else { + plot(g["lg_PC1"]) + } } if (run) { lc_PC1 <- local_geary(w, g["PC1"], significance_cutoff=0.01, @@ -184,9 +249,20 @@ if (run) { labels=lisa_labels(lc_PC1)[1:5]) is.na(g$lc_PC1) <- g$lc_PC1 == "Not significant" g$lc_PC1 <- droplevels(g$lc_PC1) - if (tmap_ok) tm_shape(g) + tm_fill("lc_PC1", textNA="Insignificant", - colorNA="gray95") + tm_borders() # Fig. 5 - else plot(g["lc_PC1"]) + if (tmap_ok) { + if (tmap4) { + tm_shape(g) + tm_polygons(fill="lc_PC1", + fill.scale=tm_scale(values="brewer.set3", value.na="gray95", + label.na="Insignificant"), + fill.legend=tm_legend(position=tm_pos_in("left", "bottom"), + frame=FALSE, item.r=0)) + } else { + tm_shape(g) + tm_fill("lc_PC1", textNA="Insignificant", + colorNA="gray95") + tm_borders() # Fig. 5 + } + } else { + plot(g["lc_PC1"]) + } } if (run) { set.seed(1) @@ -209,16 +285,38 @@ if (run) { g$lc_PC1_spdep <- attr(lc_PC1_spdep, "cluster") is.na(g$lc_PC1_spdep) <- attr(lc_PC1_spdep, "pseudo-p")[,6] > 0.01 g$lc_PC1_spdep <- droplevels(g$lc_PC1_spdep) - if (tmap_ok) tm_shape(g) + tm_fill("lc_PC1_spdep", textNA="Insignificant", - colorNA="gray95") + tm_borders() # rep. Fig. 5 - else plot(g["lc_PC1_spdep"]) + if (tmap_ok) { + if (tmap4) { + tm_shape(g) + tm_polygons(fill="lc_PC1_spdep", + fill.scale=tm_scale(values="brewer.set3", value.na="gray95", + label.na="Insignificant"), + fill.legend=tm_legend(position=tm_pos_in("left", "bottom"), + frame=FALSE, item.r=0)) + } else { + tm_shape(g) + tm_fill("lc_PC1_spdep", textNA="Insignificant", + colorNA="gray95") + tm_borders() # rep. Fig. 5 + } + } else { + plot(g["lc_PC1_spdep"]) + } } if (run) { g$both_PC1 <- interaction(g$lc_PC1, g$lm_PC1) g$both_PC1 <- droplevels(g$both_PC1) - if (tmap_ok) tm_shape(g) + tm_fill("both_PC1", textNA="Insignificant", - colorNA="gray95") + tm_borders() # Fig. 6 - else plot(g["both_PC1"]) + if (tmap_ok) { + if (tmap4) { + tm_shape(g) + tm_polygons(fill="both_PC1", + fill.scale=tm_scale(values="brewer.set3", value.na="gray95", + label.na="Insignificant"), + fill.legend=tm_legend(position=tm_pos_in("left", "bottom"), + frame=FALSE, item.r=0)) + } else { + tm_shape(g) + tm_fill("both_PC1", textNA="Insignificant", + colorNA="gray95") + tm_borders() # Fig. 6 + } + } else { + plot(g["both_PC1"]) + } } if (run) { lc005_PC1 <- local_geary(w, g["PC1"], significance_cutoff=0.005, @@ -227,17 +325,38 @@ if (run) { labels=lisa_labels(lc005_PC1)[1:5]) is.na(g$lc005_PC1) <- g$lc005_PC1 == "Not significant" g$lc005_PC1 <- droplevels(g$lc005_PC1) - if (tmap_ok) tm_shape(g) + tm_fill("lc005_PC1", textNA="Insignificant", - colorNA="gray95") + tm_borders() # Fig. 7 - else plot(g["lc005_PC1"]) + if (tmap_ok) { + if (tmap4) { + tm_shape(g) + tm_polygons(fill="lc005_PC1", + fill.scale=tm_scale(values="brewer.set3", value.na="gray95", + label.na="Insignificant"), + fill.legend=tm_legend(position=tm_pos_in("left", "bottom"), + frame=FALSE, item.r=0)) + } else { + tm_shape(g) + tm_fill("lc005_PC1", textNA="Insignificant", + colorNA="gray95") + tm_borders() # Fig. 7 + } + } else { + plot(g["lc005_PC1"]) } if (run) { g$lc005_PC1_spdep <- attr(lc_PC1_spdep, "cluster") is.na(g$lc005_PC1_spdep) <- attr(lc_PC1_spdep, "pseudo-p")[,6] > 0.005 g$lc005_PC1_spdep <- droplevels(g$lc005_PC1_spdep) - if (tmap_ok) tm_shape(g) + tm_fill("lc005_PC1_spdep", textNA="Insignificant", - colorNA="gray95") + tm_borders() # rep. Fig. 7 - else plot(g["lc005_PC1_spdep"]) + if (tmap_ok) { + if (tmap4) { + tm_shape(g) + tm_polygons(fill="lc005_PC1_spdep", + fill.scale=tm_scale(values="brewer.set3", value.na="gray95", + label.na="Insignificant"), + fill.legend=tm_legend(position=tm_pos_in("left", "bottom"), + frame=FALSE, item.r=0)) + } else { + tm_shape(g) + tm_fill("lc005_PC1_spdep", textNA="Insignificant", + colorNA="gray95") + tm_borders() # rep. Fig. 7 + } + } else { + plot(g["lc005_PC1_spdep"]) + } } if (run) { lc001_PC1 <- local_geary(w, g["PC1"], significance_cutoff=0.001, @@ -246,16 +365,39 @@ if (run) { labels=lisa_labels(lc001_PC1)[1:5]) is.na(g$lc001_PC1) <- g$lc001_PC1 == "Not significant" g$lc001_PC1 <- droplevels(g$lc001_PC1) - if (tmap_ok) tm_shape(g) + tm_fill("lc001_PC1", textNA="Insignificant", - colorNA="gray95") + tm_borders() # Fig. 8 - else plot(g["lc001_PC1"]) + if (tmap_ok) { + if (tmap4) { + tm_shape(g) + tm_polygons(fill="lc005_PC1", + fill.scale=tm_scale(values="brewer.set3", value.na="gray95", + label.na="Insignificant"), + fill.legend=tm_legend(position=tm_pos_in("left", "bottom"), + frame=FALSE, item.r=0)) + } else { + tm_shape(g) + tm_fill("lc001_PC1", textNA="Insignificant", + colorNA="gray95") + tm_borders() # Fig. 8 + } + } else { + plot(g["lc001_PC1"]) + } +} if (run) { g$lc001_PC1_spdep <- attr(lc_PC1_spdep, "cluster") is.na(g$lc001_PC1_spdep) <- attr(lc_PC1_spdep, "pseudo-p")[,6] > 0.001 g$lc001_PC1_spdep <- droplevels(g$lc001_PC1_spdep) - if (tmap_ok) tm_shape(g) + tm_fill("lc001_PC1_spdep", textNA="Insignificant", - colorNA="gray95") + tm_borders() # rep. Fig. 8 - else plot(g["lc001_PC1_spdep"]) + if (tmap_ok) { + if (tmap4) { + tm_shape(g) + tm_polygons(fill="lc005_PC1_spdep", + fill.scale=tm_scale(values="brewer.set3", value.na="gray95", + label.na="Insignificant"), + fill.legend=tm_legend(position=tm_pos_in("left", "bottom"), + frame=FALSE, item.r=0)) + } else { + tm_shape(g) + tm_fill("lc001_PC1_spdep", textNA="Insignificant", + colorNA="gray95") + tm_borders() # rep. Fig. 8 + } + } else { + plot(g["lc001_PC1_spdep"]) + } } } if (run) { @@ -265,9 +407,20 @@ if (run) { labels=lisa_labels(lc_PC2)[1:5]) is.na(g$lc_PC2) <- g$lc_PC2 == "Not significant" g$lc_PC2 <- droplevels(g$lc_PC2) - if (tmap_ok) tm_shape(g) + tm_fill("lc_PC2", textNA="Insignificant", - colorNA="gray95") + tm_borders() # Fig. 9 - else plot(g["lc_PC2"]) + if (tmap_ok) { + if (tmap4) { + tm_shape(g) + tm_polygons(fill="lc_PC2", + fill.scale=tm_scale(values="brewer.set3", value.na="gray95", + label.na="Insignificant"), + fill.legend=tm_legend(position=tm_pos_in("left", "bottom"), + frame=FALSE, item.r=0)) + } else { + tm_shape(g) + tm_fill("lc_PC2", textNA="Insignificant", + colorNA="gray95") + tm_borders() # Fig. 9 + } + } else { + plot(g["lc_PC2"]) + } } if (run) { lmc_PC <- local_multigeary(w, g[c("PC1","PC2")], significance_cutoff=0.00247, @@ -279,9 +432,20 @@ if (run) { table(interaction((p.adjust(lisa_pvalues(lmc_PC), "fdr") < 0.01), g$lmc_PC)) } if (run) { - if (tmap_ok) tm_shape(g) + tm_fill("lmc_PC", textNA="Insignificant", - colorNA="gray95") + tm_borders() # Fig. 10 - else plot(g["lmc_PC"]) + if (tmap_ok) { + if (tmap4) { + tm_shape(g) + tm_polygons(fill="lmc_PC", + fill.scale=tm_scale(values="brewer.set3", value.na="gray95", + label.na="Insignificant"), + fill.legend=tm_legend(position=tm_pos_in("left", "bottom"), + frame=FALSE, item.r=0)) + } else { + tm_shape(g) + tm_fill("lmc_PC", textNA="Insignificant", + colorNA="gray95") + tm_borders() # Fig. 10 + } + } else { + plot(g["lmc_PC"]) + } } if (run) { set.seed(1) @@ -295,9 +459,20 @@ if (run) { g$lmc_PC_spdep <- attr(lmc_PC_spdep, "cluster") is.na(g$lmc_PC_spdep) <- p.adjust(attr(lmc_PC_spdep, "pseudo-p")[,6], "fdr") > 0.01 g$lmc_PC_spdep <- droplevels(g$lmc_PC_spdep) - if (tmap_ok) tm_shape(g) + tm_fill("lmc_PC_spdep", textNA="Insignificant", - colorNA="gray95") + tm_borders() # rep. Fig. 10 - else plot(g["lmc_PC_spdep"]) + if (tmap_ok) { + if (tmap4) { + tm_shape(g) + tm_polygons(fill="lmc_PC_spdep", + fill.scale=tm_scale(values="brewer.set3", value.na="gray95", + label.na="Insignificant"), + fill.legend=tm_legend(position=tm_pos_in("left", "bottom"), + frame=FALSE, item.r=0)) + } else { + tm_shape(g) + tm_fill("lmc_PC_spdep", textNA="Insignificant", + colorNA="gray95") + tm_borders() # rep. Fig. 10 + } + } else { + plot(g["lmc_PC_spdep"]) + } } if (run) { lmc_vars <- local_multigeary(w, st_drop_geometry(g)[, 1:6], @@ -310,9 +485,20 @@ if (run) { g$lmc_vars)) } if (run) { - if (tmap_ok) tm_shape(g) + tm_fill("lmc_vars", textNA="Insignificant", - colorNA="gray95") + tm_borders() # Fig. 11 - else plot(g["lmc_vars"]) + if (tmap_ok) { + if (tmap4) { + tm_shape(g) + tm_polygons(fill="lmc_vars", + fill.scale=tm_scale(values="brewer.set3", value.na="gray95", + label.na="Insignificant"), + fill.legend=tm_legend(position=tm_pos_in("left", "bottom"), + frame=FALSE, item.r=0)) + } else { + tm_shape(g) + tm_fill("lmc_vars", textNA="Insignificant", + colorNA="gray95") + tm_borders() # Fig. 11 + } + } else { + plot(g["lmc_vars"]) + } } if (run) { set.seed(1) @@ -347,9 +533,20 @@ if (run) { g$lmc_vars_spdep <- attr(lmc_vars_spdep1, "cluster") is.na(g$lmc_vars_spdep) <- p.adjust(attr(lmc_vars_spdep1, "pseudo-p")[,6], "fdr") > 0.01 g$lmc_vars_spdep <- droplevels(g$lmc_vars_spdep) - if (tmap_ok) tm_shape(g) + tm_fill("lmc_vars_spdep", textNA="Insignificant", - colorNA="gray95") + tm_borders() # rep. Fig. 11 - else plot(g["lmc_vars_spdep"]) + if (tmap_ok) { + if (tmap4) { + tm_shape(g) + tm_polygons(fill="lmc_vars_spdep", + fill.scale=tm_scale(values="brewer.set3", value.na="gray95", + label.na="Insignificant"), + fill.legend=tm_legend(position=tm_pos_in("left", "bottom"), + frame=FALSE, item.r=0)) + } else { + tm_shape(g) + tm_fill("lmc_vars_spdep", textNA="Insignificant", + colorNA="gray95") + tm_borders() # rep. Fig. 11 + } + } else { + plot(g["lmc_vars_spdep"]) + } } } @@ -378,9 +575,9 @@ attr(C, "pseudo-p")[,6] } } \references{ -{Anselin, L. (1995), Local Indicators of Spatial Association—LISA. Geographical Analysis, 27: 93-115. \doi{10.1111/j.1538-4632.1995.tb00338.x}} +Anselin, L. (1995), Local Indicators of Spatial Association—LISA. Geographical Analysis, 27: 93-115. \doi{10.1111/j.1538-4632.1995.tb00338.x} -{Anselin, L. (2019), A Local Indicator of Multivariate Spatial Association: Extending Geary's c. Geogr Anal, 51: 133-150. \doi{10.1111/gean.12164}} +Anselin, L. (2019), A Local Indicator of Multivariate Spatial Association: Extending Gearys c. Geogr Anal, 51: 133-150. \doi{10.1111/gean.12164} } \author{ Josiah Parry \email{josiah.parry@gmail.com} and Roger Bivand diff --git a/man/localmoran_bv.Rd b/man/localmoran_bv.Rd index b0eef582..afab28c4 100644 --- a/man/localmoran_bv.Rd +++ b/man/localmoran_bv.Rd @@ -36,7 +36,7 @@ I_i^B= cx_i\Sigma_j{w_{ij}y_j} } } \examples{ -# load columbus data +# load columbus datay columbus <- st_read(system.file("shapes/columbus.gpkg", package="spData")) nb <- poly2nb(columbus) listw <- nb2listw(nb) @@ -44,9 +44,19 @@ set.seed(1) (res <- localmoran_bv(columbus$CRIME, columbus$INC, listw, nsim = 499)) columbus$hs <- hotspot(res, Prname="Pr(folded) Sim", cutoff=0.05, quadrant.type="pysal", p.adjust="none") +\donttest{ if (require("tmap", quietly=TRUE)) { +tmap4 <- packageVersion("tmap") >= "3.99" +if (tmap4) { + tm_shape(columbus) + tm_polygons(fill="hs", + fill.scale=tm_scale(values="brewer.set3"), + fill.legend=tm_legend(position=tm_pos_in("left", "top"), + frame=FALSE, item.r=0), lwd=0.01) +} else { tm_shape(columbus) + tm_fill("hs") } +} +} moran.plot(x=columbus$CRIME, y=columbus$INC, listw=listw) } \references{ diff --git a/man/read.gwt2nb.Rd b/man/read.gwt2nb.Rd index 2ac6ecff..b53c0f13 100644 --- a/man/read.gwt2nb.Rd +++ b/man/read.gwt2nb.Rd @@ -32,7 +32,7 @@ Attempts to honour the region.id argument given when reading GWT files. If the r \code{read.gwt2nb} returns a neighbour "nb" object with the generalised weights stored as a list element called "dlist" of the "GeoDa" attribute. } -\references{Luc Anselin (2003) \emph{GeoDa 0.9 User's Guide}, pp. 80--81, Spatial Analysis Laboratory, Department of Agricultural and Consumer Economics, University of Illinois, Urbana-Champaign, \url{http://geodacenter.github.io/docs/geoda093.pdf}; also \url{http://spatial-econometrics.com/data/contents.html}} +\references{Luc Anselin (2003) \emph{GeoDa 0.9 User's Guide}, pp. 80--81, Spatial Analysis Laboratory, Department of Agricultural and Consumer Economics, University of Illinois, Urbana-Champaign, \url{http://geodacenter.github.io/docs/geoda093.pdf}; also material formerly at spatial-econometrics.com/data/contents.html} \author{Roger Bivand \email{Roger.Bivand@nhh.no}} diff --git a/vignettes/sids.Rmd b/vignettes/sids.Rmd index a2777765..5739c65b 100644 --- a/vignettes/sids.Rmd +++ b/vignettes/sids.Rmd @@ -252,8 +252,13 @@ is_tmap ```{r choymap, echo=TRUE, eval=is_tmap} library(tmap) +tmap4 <- packageVersion("tmap") >= "3.99" +if (tmap4) { + tm_shape(nc) + tm_polygons(fill=c("low", "high"), fill.scale = tm_scale(values="brewer.set1"), fill.legend = tm_legend("p-values", frame=FALSE, item.r = 0), fill.free=FALSE, lwd=0.01) + tm_layout(panel.labels=c("low", "high")) +} else { tm_shape(nc) + tm_fill(c("low", "high"), palette="Set1", title="p-values") + tm_facets(free.scales=FALSE) + tm_layout(panel.labels=c("low", "high")) +} ``` For more complicated thematic maps, it may be helpful to use ColorBrewer @@ -277,7 +282,11 @@ nc$pmap <- pmap$pmap ```{r, eval=is_tmap, echo=TRUE} brks <- c(0,0.001,0.01,0.025,0.05,0.95,0.975,0.99,0.999,1) +if (tmap4) { + tm_shape(nc) + tm_polygons(fill="pmap", fill.scale = tm_scale(values="brewer.rd_bu", midpoint=0.5, breaks=brks), fill.legend = tm_legend(frame=FALSE, item.r = 0, position = tm_pos_out("right", "center")), lwd=0.01) + tm_layout(component.autoscale=FALSE) +} else { tm_shape(nc) + tm_fill("pmap", breaks=brks, midpoint=0.5, palette="RdBu") + tm_layout(legend.outside=TRUE) +} ``` @@ -308,7 +317,11 @@ nc$stdres <- rstandard(res) ```{r, eval=is_tmap, echo=TRUE} brks <- c(-4, -3, -2, -1.5, -1, -0.5, 0.5, 1, 1.5, 2, 3, 4) -tm_shape(nc) + tm_fill("stdres", breaks=brks, midpoint=0, palette="RdBu") + tm_layout(legend.outside=TRUE) +if (tmap4) { + tm_shape(nc) + tm_polygons(fill="stdres", fill.scale = tm_scale(values="brewer.rd_bu", midpoint=0.5, breaks=brks), fill.legend = tm_legend(frame=FALSE, item.r = 0, position = tm_pos_out("right", "center")), lwd=0.01) + tm_layout(component.autoscale=FALSE) +} else { + tm_shape(nc) + tm_fill("stdres", breaks=brks, midpoint=0, palette="RdBu") + tm_layout(legend.outside=TRUE) +} ``` The dispersion is equal to `r summary(res)$dispersion`, much greater than unity; we calculate @@ -350,8 +363,11 @@ nc$EB_loc <- res$est ```{r, eval=is_tmap} brks <- c(0, 0.25, 0.5, 0.75, 1, 2, 3, 4, 5) nc_miss <- st_centroid(st_geometry(nc[card(ncCC89) == 0,]), of_largest_polygon) +if (tmap4) { + tm_shape(nc) + tm_polygons(fill="stdres", fill.scale = tm_scale(values="brewer.rd_bu", midpoint=0.5, breaks=brks), fill.legend = tm_legend(frame=FALSE, item.r = 0, position = tm_pos_out("right", "center")), lwd=0.01) + tm_layout(component.autoscale=FALSE) + tm_shape(nc_miss) + tm_symbols(shape=8, size=0.5) +} else { tm_shape(nc) + tm_fill("EB_loc", breaks=brks, midpoint=1, palette="RdBu") + tm_layout(legend.outside=TRUE) + tm_shape(nc_miss) + tm_symbols(shape=8, size=0.5) - +} ``` The results are shown in Figure \ref{EBlocal}. Like other relevant @@ -384,7 +400,11 @@ nboth <- length(table(unclass(nc$both))) ``` ```{r, eval=is_tmap} +if (tmap4) { + tm_shape(nc) + tm_polygons(fill="both", fill.scale=tm_scale(values="brewer.set1"), fill.legend = tm_legend("rough\nrectangles", frame=FALSE, item.r = 0, position = tm_pos_out("right", "center")), lwd=0.01) + tm_layout(component.autoscale=FALSE) +} else { tm_shape(nc) + tm_fill("both", palette="Set1", title="rough\nrectangles") + tm_layout(legend.outside=TRUE) +} ``` Cressie constructs a transformed SIDS rates variable, 1974–78, for his @@ -445,8 +465,13 @@ nc$mp_resid <- nc$ft.SID74 - nc$pred ``` ```{r, eval=is_tmap} +if (tmap4) { + out1 <- tm_shape(nc) + tm_polygons(fill=c("ft.SID74", "pred"), fill.scale=tm_scale(values="brewer.yl_or_br"), fill.legend=tm_legend(position=tm_pos_out("right", "center"), frame=FALSE, item.r = 0), fill.free=FALSE, lwd=0.01) + tm_layout(panel.labels=c("Observed", "Median polish prediction")) + out2 <- tm_shape(nc) + tm_polygons(fill="mp_resid", fill.scale=tm_scale(values="brewer.rd_yl_gn", midpoint=0), fill.legend=tm_legend(position=tm_pos_out("right", "center"), frame=FALSE, item.r = 0), lwd=0.01) +} else { out1 <- tm_shape(nc) + tm_fill(c("ft.SID74", "pred")) + tm_facets(free.scales=FALSE) + tm_layout(panel.labels=c("Observed", "Median polish prediction")) out2 <- tm_shape(nc) + tm_fill("mp_resid", midpoint=0) + tm_layout(legend.outside=TRUE) +} tmap_arrange(out1, out2, ncol=1) ```