library(tidyverse)
library(sf)
library(tmap)
setwd("~/Dropbox/Teaching/SpatialAnalysis-MontevideoWorkshop2019/Notebooks/Montevideo_Data/Accidentes2006-2010")
Accidents <- st_read("accidentes2006-2010.shp")
Accidents = st_set_crs(Accidents, 32721)
setwd("~/Dropbox/Teaching/SpatialAnalysis-MontevideoWorkshop2019/Notebooks/Montevideo_Data/afe_estaciones")
Stations <- st_read("afe_estaciones.shp")
Stations <- st_set_crs(Stations, 32721)
setwd("~/Dropbox/Teaching/SpatialAnalysis-MontevideoWorkshop2019/Notebooks/Montevideo_Data/Vectoriales_2011")
Uruguay <- st_read("ine_depto.shp")
Uruguay <- st_set_crs(Uruguay, 32721)
setwd("~/Dropbox/Teaching/SpatialAnalysis-MontevideoWorkshop2019/Notebooks/Montevideo_Data/CyclingMontevideoData/v_bi_bicicircuitos")
Bicicircuitos <- st_read("v_bi_bicicircuitos.shp")
tm_shape(Uruguay) +
tm_polygons()
Montevideo <- Uruguay %>% filter(NOMBRE=="MONTEVIDEO")
tm_shape(Montevideo) +
tm_polygons() +
tm_shape(Accidents) + tm_bubbles()
MyMap <- tm_shape(Uruguay) +
tm_polygons(col = "deepskyblue1")
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 `/Library/Frameworks/R.framework/Versions/3.5/Resources/library/sf/shape/nc.shp' using driver `ESRI Shapefile'
Simple feature collection with 100 features and 14 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
epsg (SRID): 4267
proj4string: +proj=longlat +datum=NAD27 +no_defs
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.position = c(0.2,0.03),
scale=0.8,
#legend.title.size = 1,
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),size = 2, position = c(0.06,0))
In the documentation In the documentation In the documentation
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),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),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.
tmap_mode("plot")
tmap mode set to plotting
UruguayMap <- tm_shape(Uruguay) +
tm_polygons()
tmap_leaflet(UruguayMap)
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),size = 1, position = c(0.06,0),
lwd = 3)
library(RColorBrewer)
display.brewer.all()
library(RColorBrewer)
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)
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)
summary(nc$BIR79)
Min. 1st Qu. Median Mean 3rd Qu. Max.
319 1336 2636 4224 4889 30757
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),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),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_sf()
Error in UseMethod("st_as_sf") :
no applicable method for 'st_as_sf' applied to an object of class "bbox"
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")
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)