-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathREADME.Rmd
148 lines (118 loc) · 3.49 KB
/
README.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# cartomisc
<!-- badges: start -->
[![R build status](https://github.com/statnmap/cartomisc/workflows/R-CMD-check/badge.svg)](https://github.com/statnmap/cartomisc/actions)
<!-- badges: end -->
The goal of {cartomisc} is to store a few useful functions for spatial data manipulation and analysis.
## Installation
You can install the dev version on Github
```{r, eval=FALSE}
# install.packages("remotes")
remotes::install_github("statnmap/cartomisc")
```
# Usage
```{r, message=FALSE}
library(dplyr)
library(raster)
library(cartomisc)
library(ggplot2)
library(sf)
```
## Extract part of the data with `gplot_data`
To draw multiple raster on the same ggplot2, or to draw raster after other objects (points, polygons, lines)
- data are extracted as a tibble
- data can then be added to a ggplot2 with `geom_tile`
```{r}
# Read some data
slogo <- stack(system.file("external/rlogo.grd", package = "raster"))
slogo
# Get partial raster data to plot in ggplot
r.gg <- gplot_data(slogo)
# Remove NA to reduce size of table
r.gg.nona <- r.gg %>%
filter(!is.na(value))
r.gg.nona
```
### Plot with ggplot2
```{r, fig.width=12, fig.height=4}
# Plot
ggplot(r.gg.nona) +
geom_tile(aes(x = x, y = y, fill = value)) +
scale_fill_gradient("Probability", low = 'yellow', high = 'blue') +
facet_wrap(vars(variable)) +
coord_equal()
```
## Sun position
Calculate sun position for hillShade
```{r}
sun_position <- sun_position(2019, 04, 26,
hour = 12, min = 0, sec = 0,
lat = 46.5, long = 6.5
)
sun_position
```
Use with hillShade
```{r}
r <- raster(system.file("external/rlogo.grd", package = "raster"))
# slope and aspect
r_slope <- terrain(r, opt = "slope")
r_aspect <- terrain(r, opt = "aspect")
# hillshade
r_hillshade <- hillShade(r_slope, r_aspect, angle = sun_position$elevation, direction = sun_position$azimuth)
# plot
image(r)
image(r_hillshade, col = grey(seq(0, 1, 0.1), alpha = 0.5), add = TRUE)
```
## Create buffer areas with attribute of the closest region
- Download some data
```{r}
# Define where to save the dataset
extraWD <- tempdir()
# Get some data available to anyone
if (!file.exists(file.path(extraWD, "departement.zip"))) {
githubURL <- "https://github.com/statnmap/blog_tips/raw/master/2018-07-14-introduction-to-mapping-with-sf-and-co/data/departement.zip"
download.file(githubURL, file.path(extraWD, "departement.zip"))
unzip(file.path(extraWD, "departement.zip"), exdir = extraWD)
}
```
- Reduce the dataset to a small region
```{r}
departements_l93 <- read_sf(dsn = extraWD, layer = "DEPARTEMENT")
# Reduce dataset
bretagne_l93 <- departements_l93 %>%
filter(NOM_REG == "BRETAGNE")
```
- Calculate the regional buffer area using `regional_seas()`
```{r}
bretagne_regional_2km_l93 <- regional_seas(
x = bretagne_l93,
group = "NOM_DEPT",
dist = units::set_units(30, km), # buffer distance
density = units::set_units(0.5, 1/km) # density of points (the higher, the more precise the region attribution)
)
```
- Plot the data
```{r}
ggplot() +
geom_sf(data = bretagne_regional_2km_l93,
aes(colour = NOM_DEPT, fill = NOM_DEPT),
alpha = 0.25) +
geom_sf(data = bretagne_l93,
aes(fill = NOM_DEPT),
colour = "grey20",
alpha = 0.5) +
scale_fill_viridis_d() +
scale_color_viridis_d() +
theme_bw()
```