library(tidyverse)
library(sf)
library(tmap)
Accidents <- st_read("Montevideo_Data/Accidentes2006-2010/accidentes2006-2010.shp")
Reading layer `accidentes2006-2010' from data source
`C:\Users\Orlan\Dropbox\Teaching\SpatialAnalysis\Tutorials\03_MapMaking\Montevideo_Data\Accidentes2006-2010\accidentes2006-2010.shp'
using driver `ESRI Shapefile'
Simple feature collection with 41121 features and 7 fields
Geometry type: POINT
Dimension: XY
Bounding box: xmin: 558003.5 ymin: 6134508 xmax: 588206.1 ymax: 6157857
CRS: NA
Accidents = st_set_crs(Accidents, 32721)
Stations <- st_read("Montevideo_Data/afe_estaciones/afe_estaciones.shp")
Reading layer `afe_estaciones' from data source
`C:\Users\Orlan\Dropbox\Teaching\SpatialAnalysis\Tutorials\03_MapMaking\Montevideo_Data\afe_estaciones\afe_estaciones.shp'
using driver `ESRI Shapefile'
Simple feature collection with 105 features and 13 fields
Geometry type: POINT
Dimension: XY
Bounding box: xmin: -58.07525 ymin: -34.89368 xmax: -53.38024 ymax: -30.90302
Geodetic CRS: WGS 84
Stations <- st_set_crs(Stations, 32721)
Warning: st_crs<- : replacing crs does not reproject data; use st_transform for that
Uruguay <- st_read("Montevideo_Data/Vectoriales_2011/ine_depto.shp")
Reading layer `ine_depto' from data source
`C:\Users\Orlan\Dropbox\Teaching\SpatialAnalysis\Tutorials\03_MapMaking\Montevideo_Data\Vectoriales_2011\ine_depto.shp'
using driver `ESRI Shapefile'
Simple feature collection with 20 features and 5 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: 366582.2 ymin: 6127919 xmax: 858252.1 ymax: 6671738
CRS: NA
Uruguay <- st_set_crs(Uruguay, 32721)
Bicicircuitos <- st_read(
"Montevideo_Data/CyclingMontevideoData/v_bi_bicicircuitos/v_bi_bicicircuitos.shp")
Reading layer `v_bi_bicicircuitos' from data source
`C:\Users\Orlan\Dropbox\Teaching\SpatialAnalysis\Tutorials\03_MapMaking\Montevideo_Data\CyclingMontevideoData\v_bi_bicicircuitos\v_bi_bicicircuitos.shp'
using driver `ESRI Shapefile'
Simple feature collection with 248 features and 3 fields
Geometry type: LINESTRING
Dimension: XY
Bounding box: xmin: 571736.8 ymin: 6134478 xmax: 586683.5 ymax: 6147830
Projected CRS: WGS 84 / UTM zone 21S
tm_shape(Uruguay) +
tm_polygons()
tm_shape(Uruguay) +
tm_fill()
tm_shape(Uruguay) +
tm_fill() +
tm_layout(bg.color = "lightblue")
tm_shape(Uruguay) +
tm_borders(col = "black", lwd = 0.5, lty = "dashed")
Montevideo <- Uruguay %>% filter(NOMBRE == "MONTEVIDEO")
tm_shape(Montevideo) +
tm_polygons() +
tm_shape(Accidents) + tm_bubbles()
tm_shape(Montevideo) +
tm_polygons() +
tm_shape(Accidents) + tm_bubbles() +
tm_layout(frame = FALSE)
MyMap <- tm_shape(Uruguay) +
tm_polygons(col = "deepskyblue1")
MyMap
st_bbox(Uruguay)
xmin ymin xmax ymax
366582.2 6127918.5 858252.1 6671738.4
st_bbox(Montevideo)
xmin ymin xmax ymax
551982.7 6133498.9 589227.2 6159810.3
tm_shape(Uruguay, bbox = st_bbox(Montevideo)) +
tm_polygons(col = "deepskyblue1")
nc = st_read(system.file("shape/nc.shp", package="sf"))
Reading layer `nc' from data source
`C:\Users\Orlan\AppData\Local\R\win-library\4.2\sf\shape\nc.shp'
using driver `ESRI Shapefile'
Simple feature collection with 100 features and 14 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
Geodetic CRS: NAD27
tm_shape(nc) + tm_polygons(col = "BIR79")
tm_shape(nc) + tm_fill(col = "BIR79")
tm_shape(nc) + tm_fill(col = "BIR79") + tm_borders()
tm_shape(nc) + tm_polygons(col = "BIR79")
tm_shape(nc) + tm_polygons(col = "BIR79") +
tm_compass() + tm_scale_bar()
tm_shape(nc) + tm_polygons(col="BIR79") +
tm_layout(legend.outside = TRUE, frame = FALSE)
tm_shape(nc) + tm_polygons(col="BIR79") +
tm_layout(legend.position = c(0.2, 0.03),
scale = 0.8,
legend.frame = T)
tm_shape(nc) + tm_polygons(col = "BIR79", title = "Births 1979 - 84") +
tm_layout(legend.position = c(0.01, 0.65),
scale = 0.65,
legend.frame = T)
tm_shape(nc) + tm_polygons(col = "BIR79", title = "Births 1979 - 84") +
tm_layout(title = "THI IS MY TITLE",
legend.position = c(0.01, 0.05),
scale = 0.9,
legend.frame = T) +
tm_add_legend(type = c("fill"),
labels = c("Some Area", "Other Area"),
col = c("#66c2a5", "#e5c494"),
title = "Other Areas") +
tm_add_legend(type = "symbol",
labels = c("A point", "Other Point"),
col = c("#e78ac3", "grey"))
But coming back to our map, it needs a compass so your audience know where noth and east are.
tm_shape(nc) + tm_polygons(col = "BIR79", title = "Births 1979 - 84") +
tm_layout(legend.position = c(0.01, 0.65),
scale = 0.65,
legend.frame = T) +
tm_compass()
NA
tm_shape(nc) + tm_polygons(col = "BIR79", title = "Births 1979 - 84") +
tm_layout(legend.position = c(0.01, 0.65),
scale = 0.65,
legend.frame = T) +
tm_compass(size = 5, type = "8star")
NA
tm_shape(nc) + tm_polygons(col = "BIR79", title = "Births 1979 - 84") +
tm_layout(legend.position = c(0.01, 0.65),
scale = 0.65,
legend.frame = T) +
tm_compass(size = 5, type = "rose", lwd = 3, show.labels = 0)
tm_shape(nc) + tm_polygons(col = "BIR79", title = "Births 1979 - 84") +
tm_layout(legend.position = c(0.01, 0.65),
scale = 0.65,
legend.frame = T) +
tm_compass(size = 5, type = "rose", lwd = 3) +
tm_scale_bar()
tm_shape(nc) + tm_polygons(col = "BIR79", title = "Births 1979 - 84") +
tm_layout(legend.position = c(0.01, 0.65),
scale = 0.65,
legend.frame = T) +
tm_compass(size = 5, type = "rose", lwd = 3) +
tm_scale_bar(breaks = c(0, 50, 100, 150, 200), text.size = 2, position = c(0.06, 0))
Documentation for the tm_layout() function Documentation for the tm_compass() function Documentation for the tm_scale_bar() function
tm_shape(nc) + tm_polygons(col = "BIR79", title = "Births 1979 - 84") +
tm_layout(legend.position = c(0.01, 0.65),
scale = 0.65,
legend.frame = T) +
tm_compass(size = 5, type = "rose", lwd = 3) +
tm_scale_bar(breaks = c(0, 50, 100, 150, 200), text.size = 1, position = c(0.06, 0),
lwd = 5, color.dark = "darkgreen", color.light = "blue")
MyMap <- tm_shape(nc) + tm_polygons(col = "BIR79", title = "Births 1979 - 84") +
tm_layout(legend.position = c(0.01, 0.65),
scale = 0.65,
legend.frame = T,
bg.color = "lightblue") +
tm_compass(size = 5, type = "rose", lwd = 3) +
tm_scale_bar(breaks = c(0, 50, 100, 150, 200), text.size = 1, position = c(0.06, 0),
lwd = 3)
MyMap
tmap_mode("view")
tmap mode set to interactive viewing
MyMap
Compass not supported in view mode.
legend.postion is used for plot mode. Use view.legend.position in tm_view to set the legend position in view mode.
Warning: In view mode, scale bar breaks are ignored.
tmap_mode("plot")
tmap mode set to plotting
tm_shape(nc) + tm_polygons(col="BIR79", title = "Births 1979 - 84", palette = "Blues") +
tm_layout(legend.position = c(0.01,0.65),
scale=0.65,
legend.frame = T,
bg.color = "lightblue") +
tm_compass(size = 5, type = "rose", lwd = 3) +
tm_scale_bar(breaks = c(0,50,100,150,200), text.size = 1, position = c(0.06,0),
lwd = 3)
tm_shape(nc) +
tm_polygons(col="BIR79", title = "Births 1979 - 84", palette = "Blues", n = 4) +
tm_layout(legend.position = c(0.01,0.65),
scale=0.65,
legend.frame = T,
bg.color = "lightblue") +
tm_compass(size = 5, type = "rose", lwd = 3) +
tm_scale_bar(breaks = c(0,50,100,150,200), text.size = 1, position = c(0.06,0),
lwd = 3)
tm_shape(nc) +
tm_polygons(col="BIR79", title = "Births 1979 - 84", palette = "Blues",
style = "pretty") +
tm_layout(legend.position = c(0.01,0.65),
scale=0.65,
legend.frame = T,
bg.color = "lightblue") +
tm_compass(size = 5, type = "rose", lwd = 3) +
tm_scale_bar(breaks = c(0,50,100,150,200), text.size = 1, position = c(0.06,0),
lwd = 3)
library(RColorBrewer)
display.brewer.all()
tm_shape(nc) + tm_polygons(col="BIR79", title = "Births 1979 - 84", palette = "YlGnBu") +
tm_layout(legend.position = c(0.01,0.65),
scale=0.65,
legend.frame = T,
bg.color = "lightblue") +
tm_compass(size = 5, type = "rose", lwd = 3) +
tm_scale_bar(breaks = c(0,50,100,150,200),size = 1, position = c(0.06,0),
lwd = 3)
Warning: The argument size of tm_scale_bar is deprecated. It has been renamed to text.size
tm_shape(nc) + tm_polygons(col="BIR79", title = "Births 1979 - 84", palette = "-YlGnBu") +
tm_layout(legend.position = c(0.01,0.65),
scale=0.65,
legend.frame = T,
bg.color = "lightblue") +
tm_compass(size = 5, type = "rose", lwd = 3) +
tm_scale_bar(breaks = c(0,50,100,150,200), text.size = 1, position = c(0.06,0),
lwd = 3)
summary(nc$BIR79)
Min. 1st Qu. Median Mean 3rd Qu. Max.
319 1336 2636 4224 4889 30757
nc %>% ggplot(aes(x=BIR79)) + geom_histogram()
Breaks <- c(0, 2000, 4000, 6000, 31000)
Labels <- c("0 - 2000", "2000 - 4000", "4000 - 6000", ">6000")
tm_shape(nc) + tm_polygons(col = "BIR79", title = "Births 1979 - 84", palette = "YlGnBu",
breaks = Breaks, labels = Labels) +
tm_layout(legend.position = c(0.01, 0.65),
scale = 0.65,
legend.frame = T,
bg.color = "lightblue") +
tm_compass(size = 5, type = "rose", lwd = 3) +
tm_scale_bar(breaks = c(0, 50, 100, 150, 200), text.size = 1, position = c(0.06, 0),
lwd = 3)
MyPalette <- c("#f2f0f7", "#cbc9e2", "#9e9ac8", "#6a51a3")
tm_shape(nc) + tm_polygons(col = "BIR79", title = "Births 1979 - 84", palette = MyPalette,
breaks = Breaks, labels = Labels) +
tm_layout(legend.position = c(0.01, 0.65),
scale = 0.65,
legend.frame = T,
bg.color = "lightblue") +
tm_compass(size = 5, type = "rose", lwd = 3) +
tm_scale_bar(breaks = c(0, 50, 100, 150, 200), text.size = 1, position = c(0.06, 0),
lwd = 3)
MontevideoMap <- tm_shape(Montevideo) +
tm_polygons() +
tm_shape(Accidents) + tm_bubbles()
UruguayMap <- tm_shape(Uruguay) +
tm_polygons()
library(grid)
MontevideoMap
print(UruguayMap, vp = viewport(0.17, 0.8, width = 0.25, height = 0.25))
MontevideoMap <- tm_shape(Montevideo) +
tm_polygons(col = "deepskyblue1")
MontevideoNeigh <- Uruguay %>% filter(NOMBRE %in% c("SAN JOSE", "CANELONES", "MONTEVIDEO"))
AuxMap <- tm_shape(MontevideoNeigh) + tm_polygons(col = "green")
MontevideoMap
print(AuxMap, vp = viewport(0.17, 0.8, width = 0.2, height = 0.2))
Our map needs some context!
MontevideoBox <- st_bbox(Montevideo) %>% st_as_sfc() #Why not st_as_sf?
tm_shape(MontevideoBox) + tm_polygons(col = "white", border.col = "blue", lwd = 4)
#plot(st_geometry(MontevideoBox))
AuxMap <- tm_shape(MontevideoNeigh) + tm_polygons(col = "green") +
tm_shape(MontevideoBox) + tm_polygons(border.col = "red", lwd = 2.5, alpha = 0.3)
MontevideoMap
print(AuxMap, vp = viewport(0.17, 0.8, width = 0.2, height = 0.2))
tm_shape(Montevideo) + tm_polygons() +
tm_shape(Accidents) + tm_dots(alpha = 0.5) + tm_facets(by = "ANIO")
tm_shape(Montevideo) + tm_polygons() +
tm_shape(Accidents) + tm_dots(alpha = 0.5) + tm_facets(by = "ANIO", nrow = 1)
tm_shape(Montevideo) + tm_polygons() +
tm_shape(Accidents) + tm_dots(alpha = 0.5) + tm_facets(by = "ANIO", ncol = 1)
Accidents2006 <- Accidents %>% filter(ANIO == "2006")
Accidents2010 <- Accidents %>% filter(ANIO == "2010")
Complements <- tm_compass() + tm_scale_bar()
Map2006 <- tm_shape(Accidents2006) + tm_dots() + Complements
Map2010 <- tm_shape(Accidents2010) + tm_dots() + Complements
tmap_arrange(Map2006, Map2010)