plot()
functionplot()
for a quick look at dataI use plot()
for a quick look at data
I use {tmap} for everything else
I use plot()
for a quick look at data
I use {tmap} for everything else
{tmap} has an insane amount of customization allowed, we will only touch the surface
ggplot2
?ggplot2
?ggplot2
and use it every dayggplot2
?I love ggplot2
and use it every day
But I find making maps significantly easier in tmap
tmap
plot()
plot()
is not super excitingplot()
is great for a quick look at your dataplot()
has methods for vector and raster dataplot()
has methods for vector and raster dataThe packages need to be loaded to plot vector and raster data
No need to type with me, you'll practice in the exercise
library(sf) # read vectorslibrary(raster) # read rasterslibrary(tmap) # mapping
boroughs <- read_sf("boroughs.gpkg")schools <- read_sf("schools.shp")canopy <- raster("canopy.grd")
plot(boroughs)
plot(boroughs)
plot(boroughs['Shape_Area'], main = "Area", pal = rev(heat.colors(5)))
st_geometry()
You can extract just the geometry with st_geometry()
Then call plot()
on the output
st_geometry()
and then plotst_geometry(boroughs) %>% plot()
plot(st_geometry(boroughs))
st_geometry(boroughs) %>% plot()
plot(st_geometry(boroughs))
plot()
use add = TRUE
plot(st_geometry(boroughs))plot(st_geometry(schools), add = TRUE, pch = 16, col = "red", cex = 0.5)
add = TRUE
, only works with st_geometry()
add = TRUE
, only works with st_geometry()
# This won't work!plot(boroughs)plot(schools, add = TRUE)
plot()
with rastersplot(canopy) # a single band raster
plot()
maps each layer separately
plot(manhattan)
plot()
maps each layer separately
plot(manhattan)
You can plot them together...
You can plot them together...
plotRGB(manhattan)
plot()
plot()
plot(canopy)plot(st_geometry(schools), add = T, pch = 16, col = "red", cex = 0.5)
tm_text(text, size = 1, col = NA, root = 3, clustering = FALSE, size.lim = NA, sizes.legend = NULL, sizes.legend.labels = NULL, sizes.legend.text = "Abc", n = 5, style = ifelse(is.null(breaks), "pretty", "fixed"), breaks = NULL, interval.closure = "left", palette = NULL, labels = NULL, labels.text = NA, midpoint = NULL, stretch.palette = TRUE, contrast = NA, colorNA = NA, textNA = "Missing", showNA = NA, colorNULL = NA, fontface = NA, fontfamily = NA, alpha = NA, case = NA, shadow = FALSE, bg.color = NA, bg.alpha = NA, size.lowerbound = 0.4, print.tiny = FALSE, scale = 1, auto.placement = FALSE, remove.overlap = FALSE, along.lines = FALSE, overwrite.lines = FALSE, just = "center", xmod = 0, ymod = 0, title.size = NA, title.col = NA, legend.size.show = TRUE, legend.col.show = TRUE, legend.format = list(), legend.size.is.portrait = FALSE, legend.col.is.portrait = TRUE, legend.size.reverse = FALSE, legend.col.reverse = FALSE, legend.hist = FALSE, legend.hist.title = NA, legend.size.z = NA, legend.col.z = NA, legend.hist.z = NA, group = NA, auto.palette.mapping = NULL, max.categories = NULL)
# data set up layer tm_shape(boroughs) + tm_polygons()
# data set up layer tm_shape(boroughs) + tm_polygons()
qtm()
Instead of plot()
I often use this
qtm(boroughs)
tm_shape(boroughs) + tm_polygons() + tm_dots(size = 2) + tm_text("BoroName", col = "red", size = 1.5)
tm_shape(boroughs) + tm_polygons() + tm_dots(size = 2) + tm_text("BoroName", col = "red", size = 1.5)
tm_shape(boroughs) + tm_borders() + tm_shape(schools) + tm_dots(size = 0.25)
mymap <- tm_shape(boroughs) + tm_polygons()
mymap <- tm_shape(boroughs) + tm_polygons()
mymap + tm_dots(size = 2) + tm_text("BoroName", col = "red")
tm_shape(boroughs) + tm_polygons("Shape_Area")
tm_shape(boroughs) + tm_polygons("Shape_Area")
tm_symbols()
tm_shape(boroughs) + tm_borders() + tm_symbols("Shape_Area", scale = 2)
tm_shape(boroughs) + tm_polygons(c("Shape_Area", "BoroName"))
tm_shape()
can accept vector or rastertm_raster()
tm_shape(canopy) + tm_raster()
tm_raster()
tm_shape(canopy) + tm_raster()
tm_rgb()
tm_shape(manhattan) + tm_rgb()
tm_rgb()
tm_shape(manhattan) + tm_rgb()
tm_shape(manhattan) + tm_rgb()+ tm_shape(boroughs) + tm_borders(col = "white", lwd = 2)
tm_shape(manhattan) + tm_rgb()+ tm_shape(boroughs) + tm_borders(col = "white", lwd = 2)
Use a function from the companion package, {tmaptools}
Use a function from the companion package, {tmaptools}
osmtiles <- tmaptools::read_osm(boroughs, type="stamen-terrain")
tm_shape(osmtiles) + tm_raster() + tm_shape(boroughs) + tm_borders(lwd = 2, col = "yellow")
First shape is master by default
in tm_shape() can use is.master = TRUE
tm_shape(canopy) + tm_raster(title = "(percent canopy)", alpha = 0.75) + tm_shape(boroughs) + tm_borders(lwd = 2, col = "blue")
tm_shape(canopy) + tm_raster(title = "(percent canopy)", alpha = 0.75) + tm_shape(boroughs, is.master = TRUE) + tm_borders(lwd = 2, col = "blue")
tm_shape(canopy) + tm_raster(title = "(percent canopy)", alpha = 0.75) + tm_shape(boroughs, is.master = TRUE) + tm_borders(lwd = 2, col = "blue")
palette_explorer()
is greattmaptools::palette_explorer()
pal
argumenttm_shape(neighborhoods) + tm_polygons("shape_area", n = 5, pal = "Greens")
pal
argumenttm_shape(neighborhoods) + tm_polygons("shape_area", n = 5, pal = "Greens")
-
to reverse the palettetm_shape(neighborhoods) + tm_polygons("shape_area", n = 5, pal = "-Greens")
-
to reverse the palettetm_shape(neighborhoods) + tm_polygons("shape_area", n = 5, pal = "-Greens")
tm_layout()
tm_layout()
tm_shape(neighborhoods) + tm_polygons("shape_area", n = 5, pal = "Greens") + tm_layout(legend.show = FALSE, frame = FALSE)
tmap_save()
tmap_save()
mymap <- tm_shape(boroughs) + tm_polygons()tmap_save(mymap, "mymap.png")
tmap_arrange()
tmap_arrange()
# Create three mapsm1 <- tm_shape(boroughs) + tm_polygons()m2 <- tm_shape(neighborhoods) + tm_polygons()m3 <- tm_shape(schools) + tm_dots(size = 0.25)
tmap_arrange(m1, m2, m3, nrow = 1)
tmap_arrange(m1, m2, m3, nrow = 1)
tmap_save()
tm_layout()
tm_style()
tm_facets()
tm_animation()
tm_scale_bar()
tm_compass()
geom_sf()
geom_sf()
no need to specify x and ygeom_line()
, geom_points()
etcaes()
with the fill argumentaes()
with the fill argumentlibrary(ggplot2)ggplot() + geom_sf(data = boroughs, aes(fill = BoroCode)) + geom_sf(data = schools, color = "purple")
Main reason is it requires layers to be unprojected (we will discuss)
There are work-arounds like leafletCRS()
But {tmap} and {mapview} do a great job
tmap_mode()
to change from static to interactivetmap_mode()
to change from static to interactivetmap_mode("plot") # defaultmy_map
tmap_mode("view") # make interactivemy_map # recreate plot
tmap_mode()
with no argument will give the current modetmap_mode()
with no argument will give the current modetmap_mode()# current tmap mode is "view"
tmap
can do side-by-side linked plotstmap
can do side-by-side linked plotstmap_mode("view")tm_shape(boroughs) + tm_polygons(c("pop2018", "pop_change"), palette = "Oranges")
tmap_save()
mymap <- tm_shape(boroughs) + tm_polygons()tmap_save(mymap, "mymap.html")
tmap_save()
mymap <- tm_shape(boroughs) + tm_polygons()tmap_save(mymap, "mymap.html")
library(mapview)mapview(boroughs)
mapview(x, map = NULL, maxpixels = mapviewGetOption("mapview.maxpixels"), col.regions = mapviewGetOption("raster.palette")(256), at = NULL, na.color = mapviewGetOption("na.color"), use.layer.names = FALSE, values = NULL, map.types = mapviewGetOption("basemaps"), alpha.regions = 0.8, legend = mapviewGetOption("legend"), legend.opacity = 1, trim = TRUE, verbose = mapviewGetOption("verbose"), layer.name = NULL, homebutton = TRUE, native.crs = FALSE, method = c("bilinear", "ngb"), label = TRUE, query.type = c("mousemove", "click"), query.digits, query.position = "topright", query.prefix = "Layer", viewer.suppress = FALSE, ...)
tmap_mode("view")tm_shape(boroughs) + tm_polygons()
list()
list()
library(mapview)mapview(list(boroughs, schools))
mapview(boroughs) + mapview(schools)
mapview(boroughs) + mapview(schools)
mapview(boroughs) + schools
zcol
argumentmapview(boroughs, zcol = "Shape_Area")
mapview(canopy, alpha.regions = 0.4)
mapshot
Output is very similar to {tmap} (html file and folder)
mapshot
Output is very similar to {tmap} (html file and folder)
mymap <- mapview(boroughs)mapshot(mymap, "mymap.html")
You can include in R markdown
You can include in shiny application
You can include in R markdown
You can include in shiny application
If you save with tmap_save()
or mapshot()
you can upload the files directly to a server
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |