For the countries Indonesia and Canada, create individual plots using equirectangular, orthographic, and Lambert equal area projections, while choosing projection parameters sensible for the area.
# +proj=eqc +lat_ts=0 +lat_0=0 +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +units=m# +proj=ortho +lon_0=0 +lat_0=0# +proj=laea +lon_0=0 +lat_0=0library(sf)# Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.4.0; sf_use_s2() is TRUElibrary(tidyverse)# ── Attaching core tidyverse packages ──────────── tidyverse 2.0.0 ──# ✔ dplyr 1.1.4 ✔ readr 2.1.5# ✔ forcats 1.0.0 ✔ stringr 1.5.1# ✔ ggplot2 3.5.1 ✔ tibble 3.2.1# ✔ lubridate 1.9.4 ✔ tidyr 1.3.1# ✔ purrr 1.0.2# ── Conflicts ────────────────────────────── tidyverse_conflicts() ──# ✖ dplyr::filter() masks stats::filter()# ✖ dplyr::lag() masks stats::lag()# ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errorslibrary(rnaturalearth)ne=ne_countries(returnclass ="sf")ne|>filter(admin=="Canada")->cane|>filter(admin=="Indonesia")->indst_geometry(ca)|>st_centroid()# Geometry set for 1 feature # Geometry type: POINT# Dimension: XY# Bounding box: xmin: -96.39551 ymin: 60.47676 xmax: -96.39551 ymax: 60.47676# Geodetic CRS: WGS 84# POINT (-96.39551 60.47676)ca_eqc=st_crs("+proj=eqc +lat_ts=60.6")st_geometry(ca)|>st_transform(ca_eqc)|>plot(axes=TRUE, graticule =TRUE)
library(tmap)b=st_buffer(nc[1,1], units::set_units(10, km))|>st_cast("LINESTRING")# Warning in st_cast.sf(st_buffer(nc[1, 1], units::set_units(10,# km)), "LINESTRING"): repeating attributes for all sub-geometries# for which they may not be constanttm_shape(nc)+tm_polygons("BIR74", title ="BIR74")+tm_layout(legend.outside=TRUE)+tm_shape(b)+tm_lines(legend.lwd.show =FALSE, col ='red')# # ── tmap v3 code detected ───────────────────────────────────────────# [v3->v4] `tm_polygons()`: migrate the argument(s) related to the# legend of the visual variable `fill` namely 'title' to 'fill.legend# = tm_legend(<HERE>)'# [v3->v4] `tm_lines()`: use `lwd.legend = tm_legend_hide()` instead# of `legend.lwd.show = FALSE`.
Exercise 8.3
Recreate the plot in Figure 8.7 using the viridis colour ramp.
library(stars)# Loading required package: abindlibrary(viridis)# Loading required package: viridisLiter<-read_stars(system.file("tif/L7_ETMs.tif", package ="stars"))tm_shape(r)+tm_raster(palette =viridis(100))# # ── tmap v3 code detected ───────────────────────────────────────────# [v3->v4] `tm_tm_raster()`: migrate the argument(s) related to the# scale of the visual variable `col` namely 'palette' (rename to# 'values') to col.scale = tm_scale(<HERE>).
Exercise 8.4
View the interactive plot in Figure 8.7 using the “view” (interactive) mode of tmap, and explore which interactions are possible;
library(stars)library(viridis)r<-read_stars(system.file("tif/L7_ETMs.tif", package ="stars"))tmap_mode("view")# ℹ tmap mode set to "view".tm_shape(r)+tm_raster(palette =viridis(100))# # ── tmap v3 code detected ───────────────────────────────────────────# [v3->v4] `tm_tm_raster()`: migrate the argument(s) related to the# scale of the visual variable `col` namely 'palette' (rename to# 'values') to col.scale = tm_scale(<HERE>).
Interactions: zoom, pan, linked cursor.
Also explore adding + tm_facets(as.layers=TRUE) and try switching layers on and off.
tmap_mode("view")# ℹ tmap mode set to "view".tm_shape(r)+tm_raster(palette =viridis(100))+tm_facets(as.layers=TRUE)# # ── tmap v3 code detected ───────────────────────────────────────────# [v3->v4] `tm_tm_raster()`: migrate the argument(s) related to the# scale of the visual variable `col` namely 'palette' (rename to# 'values') to col.scale = tm_scale(<HERE>).
Layer switch on left-hand side (layer symbol).
Try also setting a transparency value to 0.5.
tmap_mode("view")# ℹ tmap mode set to "view".tm_shape(r)+tm_raster(palette =viridis(100), alpha =.5)+tm_facets(as.layers=TRUE)# # ── tmap v3 code detected ───────────────────────────────────────────# [v3->v4] `tm_tm_raster()`: migrate the argument(s) related to the# scale of the visual variable `col` namely 'palette' (rename to# 'values') to col.scale = tm_scale(<HERE>).[v3->v4] `tm_raster()`: use `col_alpha` instead of `alpha`.
This shows the base map shining through transparent raster colors (switch 3 of the 4 layers off to see this).