-
Notifications
You must be signed in to change notification settings - Fork 13
/
mapview.Rmd
125 lines (79 loc) · 3.45 KB
/
mapview.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
---
title: "mapview"
author: "Jamie Montgomery"
date: "2/4/2020"
output: html_document
---
# Overview
This is a basic introduction to the `mapview` packaged developed by Tim Appelhans and others. For more information on the package and even more tutorials check out [https://r-spatial.github.io/mapview/index.html](https://r-spatial.github.io/mapview/index.html)
## Setup
You will need the `sf`, `mapview` and `raster` packages.
```{r setup, warning = F, message = F}
knitr::opts_chunk$set(echo = TRUE, warning = F, message = F)
library(sf)
library(mapview)
library(raster)
```
## Load spatial data
### Point data
The `sb-tweets` shapefile is a spatial dataframe of tweets from Santa Barbara between September & November of 2019.
```{r load_tweets}
sb_tweets <- read_sf("data/sb-tweets.shp")
```
Time for `mapview`
```{r mapview}
mapview(sb_tweets)
```
Play with color
```{r}
mapview(sb_tweets, zcol = "usr_typ")
```
```{r}
mapview(sb_tweets, zcol = "Month")
```
Playing with continuous (using days here which isn't usually what you would do)
```{r}
mapview(sb_tweets, zcol = "Day")
```
### Polygon data
The California Protected Areas Database (CPAD) can be downloaded at: [https://www.calands.org/](https://www.calands.org/). I've already cropped it to the Santa Barbara area, and selected just a few of the columns, but the full dataset covers the entire state and has a lot more information.
```{r load_cpad}
#read in data
cpad <- read_sf("data/ca_protected_areas.shp")
#simple view
mapview(cpad)
```
You can play with color using the `zcol` argument to set the variable to color by. In this case we will use Access Type.
```{r color_by_variable}
mapview(cpad, zcol = "ACCESS_TYP")
```
By adding `burst = TRUE` to the `mapview` function you can split out a variable into unique layers according to all unique values of the variable
```{r burst}
mapview(cpad, zcol = "ACCESS_TYP", burst = TRUE)
```
You can set your own color palette or even discrete colors using the `col.regions` argument.
```{r set_colors}
mapview(cpad, zcol = "ACCESS_TYP", col.regions = c("purple", "yellow", "darkgreen", "orange"))
mapview(cpad, zcol = "ACCESS_TYP", col.regions = mapviewPalette("mapviewSpectralColors"))
```
Easily combine layers using the `+`, and set label titles.
```{r combine_layers}
mapview(cpad, zcol = "ACCESS_TYP", layer.name = "CPAD Access") +
mapview(sb_tweets, zcol = "usr_typ", layer.name = "SB Tweets", col.regions = c("darkgreen", "darkblue"))
```
You can change the basemap and also turn on and off layers that you've added to the map using the controls on the left hand side of the map.
### Raster
You can also plot raster data. The data here is fish catch in tons/km2 for the US Northeast region from 2015. It's a big raster but mapview scales it for viewing. As you hover over the cells you see the value pop up on the top right portion of the map.
```{r raster}
catch <- raster("data/catch_2015.tif")
mapview(catch)
```
## Save with `mapshot`
You can use the `mapshot` argument to save a map either as an interactive .html to share with others or post online, or a static png. When I first tried this I got a PhantomJS error:
> `PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.`
and had to install it before it worked...
```{r save}
#first name the map
map <- mapview(sb_tweets, zcol = "usr_typ")
mapshot(map, file = "sb_tweet_map.png")
```