```r

knitr::opts_chunk$set(echo = TRUE, fig.width = 9)

# Instructions

Open the tmap repository as RStudio project.
Set Knit Directory (via drop-down menu of the Knit button in RStudio) to Project Directory.

```r
library(devtools)
load_all()
data(World,metro,land)

World$pop_class = cut(World$pop_est, breaks = c(0, 10, 100, 1000, Inf) * 1e6, labels = c("Small", "Medium", "Large", "Extra Large"))                       
World$HPI_class = cut(World$HPI, breaks = seq(10, 50, by = 10))
World$well_being_class = cut(World$well_being, breaks = seq(2, 8, by = 2))
World$footprint_class = cut(World$footprint, breaks = seq(0, 16, by = 4))

metro$pop2020_class = cut(metro$pop2020, breaks = c(.5, 1.5, 2.5, 5, 15, 40) * 1e6)



Africa = World[World$continent == "Africa", ]
tmapV =  ifelse(packageVersion("tmap") >= "3.4", "4", "3")
txt = function(x) print(grid::grid.text(x, gp = gpar(cex = 3)))
v3 = function(e) {
    if (tmapV == "3") {
        print(e)
    } else {
        txt("Only for tmap 3")
    }
    invisible(NULL)
}
v4 = function(e) {
    if (tmapV == "4") {
        print(e)
    } else {
        txt("Only for tmap 4")
    }
    invisible(NULL)
}
txt(paste("Loaded tmap version", tmapV))
# set device to landscape
tmap_options(legend.width = 10, legend.frame = TRUE)
tm_shape(World) + tm_polygons("HPI")
tm_shape(World) + tm_polygons("HPI", fill.legend = tm_legend(position = tm_pos_auto_out(pos.h= "left")))
tm_shape(World) + tm_polygons("HPI", fill.legend = tm_legend(position = tm_pos_auto_out(pos.h= "right", pos.v = "bottom")))
tm_shape(World) + tm_polygons("HPI", fill.legend = tm_legend(position = tm_pos_auto_out(pos.h= "right", pos.v = "bottom", align.h = "left", align.v = "right")))
tm_shape(World) + tm_polygons("HPI", fill.legend = tm_legend(position = tm_pos_auto_out(pos.h= "right", pos.v = "bottom", align.h = "center", align.v = "center")))
tm_shape(World) + tm_polygons("HPI", fill.legend = tm_legend(position = tm_pos_auto_out(pos.h= "right", pos.v = "bottom", align.h = "right", align.v = "bottom")))
tm_shape(World) + tm_polygons("HPI")
tm_shape(World) + tm_polygons("HPI", fill.legend = tm_legend(position = tm_pos_out(cell.h= "left", cell.v = "top")))
tm_shape(World) + tm_polygons("HPI", fill.legend = tm_legend(position = tm_pos_out(cell.h= "left", cell.v = "center")))
tm_shape(World) + tm_polygons("HPI", fill.legend = tm_legend(position = tm_pos_out(cell.h= "center", cell.v = "bottom")))
tm_shape(World) + tm_polygons("HPI")
tm_shape(World) + tm_polygons("HPI", fill.legend = tm_legend(position = tm_pos_in(pos.h= "left", pos.v = "top")))
tm_shape(World) + tm_polygons("HPI", fill.legend = tm_legend(position = tm_pos_in(pos.h= "left", pos.v = "center")))
tm_shape(World) + tm_polygons("HPI", fill.legend = tm_legend(position = tm_pos_in(pos.h= "center", pos.v = "bottom")))
tm_shape(World) + tm_polygons("HPI", col = "economy")
tm_shape(World) + tm_polygons("HPI", col = "economy") + tm_layout(legend.position = tm_pos_out("center", "center", pos.h = "center", pos.v = "center"))
tm_shape(World) + tm_polygons("HPI", col = "economy") + tm_layout(legend.position = tm_pos_out("center", "center", pos.h = "center", pos.v = "center", align.h = "right", align.v = "bottom"))
tm_shape(World) + tm_polygons("HPI", col = "economy", col.legend = tm_legend(position = tm_pos_in(align.h = "right"))) + tm_layout(legend.position = tm_pos_out("center", "center", pos.h = "center", pos.v = "center"))
tm_shape(World) + tm_polygons("HPI", col = "economy", col.legend = tm_legend(position = tm_pos_in(align.h = "right"))) + tm_layout(legend.position = tm_pos_out("center", "center", pos.h = "center", pos.v = "center"))
tm_shape(World) + tm_polygons("HPI", col = "economy")
tm_shape(World) + tm_polygons("HPI", col = "economy") + tm_title("Test1234") + tm_layout(legend.orientation = "landscape", legend.stack = "vertical")
tm_shape(World) + tm_polygons("HPI", col = "economy", fill.legend = tm_legend(position = tm_pos_auto_out("right", "bottom")), col.legend = tm_legend(position = tm_pos_auto_out("right", "bottom", pos.v = "bottom"))) + tm_title("Test1234")

tm_shape(World) + tm_polygons("HPI", col = "economy", lty = "economy", 
                              fill.legend = tm_legend(position = tm_pos_auto_out("right", "bottom")), 
                              lty.legend = tm_legend(position = tm_pos_auto_out("right", "bottom")), 
                              col.legend = tm_legend(position = tm_pos_auto_out("right", "bottom", pos.v = "bottom"))) + 
    tm_title("Test1234")
tm_shape(World) + tm_polygons("HPI", col = "economy") + tm_layout(legend.position = tm_pos_auto_in())
tm_shape(World) + tm_polygons("HPI", col = "economy") + tm_layout(legend.position = tm_pos_auto_in()) + tm_title("test")
tm_shape(World) + tm_polygons("HPI", col = "economy") + tm_layout(legend.position = tm_pos_auto_in()) + tm_title("test", position = tm_pos_in("left", "bottom"))
tm_shape(World) + tm_polygons("HPI", col = "economy", lwd = "life_exp",
                              fill.legend = tm_legend(position = tm_pos_in("left", "top")),
                              col.legend = tm_legend(position = tm_pos_in("left", "bottom")),
                              lwd.legend = tm_legend(position = tm_pos_in("right", "TOP"))) + 
    tm_title("test", position = tm_pos_in("right", "BOTTOM"), frame = TRUE) +
    tm_title("test2", position = tm_pos_in("right", "TOP"), frame = TRUE) +
    tm_title("test3", position = tm_pos_in(0.4, 0.4), frame = TRUE) +
    tm_title("test4", position = tm_pos_in(0.4, 0.4), frame = TRUE) +
    tm_title("test5", position = tm_pos_in(0.7, 0.7), frame = TRUE)


tm_shape(World) + tm_polygons("HPI", col = "economy", lwd = "life_exp",
                              fill.legend = tm_legend(position = tm_pos_in("left", "top")),
                              col.legend = tm_legend(position = tm_pos_in("LEFT", "bottom")),
                              lwd.legend = tm_legend(position = tm_pos_in("right", "TOP"))) + 
    tm_title("test", position = tm_pos_in("right", "BOTTOM"), frame = TRUE) +
    tm_title("test2", position = tm_pos_in("right", "TOP"), frame = TRUE) +
    tm_title("test3", position = tm_pos_in(0.4, 0.4), frame = TRUE) +
    tm_title("test4", position = tm_pos_in(0.4, 0.4), frame = TRUE) +
    tm_title("test5", position = tm_pos_in(0.0, 0.0, just.h = "left", just.v = "bottom"), frame = TRUE, bg.color = "red") +
    tm_layout(scale = 2)


r-tmap/tmap documentation built on June 23, 2024, 9:58 a.m.