-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path05_Demo.Rmd
More file actions
172 lines (141 loc) · 5.51 KB
/
Copy path05_Demo.Rmd
File metadata and controls
172 lines (141 loc) · 5.51 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
---
title: "Appendix 1 - sPlotOpen - Demo"
author: "Francesco Maria Sabatini, Jonathan Lenoir, Helge Bruelheide"
date: "07/04/2021"
output: html_document
---
Appendix to the paper: Sabatini, Lenoir et al., sPlotOpen – An environmentally-balanced, open-access, global dataset of vegetation plots. *Global Ecology and Biogeography*.
<br>
This demo illustrates how to import and manipulate sPlotOpen data to create some basic graphics or tables together with a reference list. As a worked example, the code below will:
1. select all plots containing at least a species of *Quercus* from sPlotOpen's resampled iteration #1
2. show some summary at biome level
3. graph the distribution of the community weighted mean of a selected functional trait
4. show the geographical location of all selected plots
5. create a reference list based on the plots effectively selected.
<br>
```{r, message=F, warning=F}
#load libraries
library(tidyverse)
library(sf)
library(raster)
library(rnaturalearth)
library(RefManageR)
```
## Import data
```{r}
load("_sPlotOpenDB/sPlotOpen.RData")
ls()
```
## Extract all plots containing at least a *Quercus* species
Use only the first resampled iteration of sPlotOpen
```{r}
#select only the first resample
header.oa1 <- header.oa %>%
filter(Resample_1 == T)
DT2.oa1 <- DT2.oa %>%
filter(PlotObservationID %in% header.oa1$PlotObservationID)
CWM_CWV.oa1 <- CWM_CWV.oa %>%
filter(PlotObservationID %in% header.oa1$PlotObservationID)
```
```{r}
#get all plots containing at least one Quercus species
plotlist.quercus <- DT2.oa1 %>%
filter(str_detect(Species, "^Quercus")) %>%
distinct(PlotObservationID) %>%
pull(PlotObservationID)
header.quercus <- header.oa1 %>%
filter(PlotObservationID %in% plotlist.quercus &
Resample_1 == T)
DT2.quercus <- DT2.oa1 %>%
filter(PlotObservationID %in% plotlist.quercus)
CWM_CWV.quercus <- CWM_CWV.oa1 %>%
mutate(Quercus=ifelse(PlotObservationID %in% plotlist.quercus, T, F))
```
There are `r length(plotlist.quercus)` plots containing at least a *Quercus* species in sPlotOpen's resampled iteration 1.
<br>
## Number of plots with *Quercus* across biomes
Summarize the number of plots containing at least one *Quercus* species across biomes
```{r}
header.quercus %>%
group_by(Biome) %>%
summarize(n = n())
```
\pagebreak
## Compare Community Weighted Means
Compare the distribution of the community weighted means of Stem density, between plots containing and not containing a *Quercus* species.
```{r, warning=F}
ggplot(data = CWM_CWV.quercus) +
geom_density(aes(x = StemDens_CWM, fill = Quercus), col = NA, alpha = 0.5) +
theme_bw()
```
\pagebreak
## Geographical distribution of plots containing a *Quercus* species
<br>
Download some spatial data of the world and create a template map using the r package `rnaturalearth`, first. Transform all geographical data to Eckert IV projection.
```{r, message=F, warning=F, results="hide"}
countries <- ne_countries(returnclass = "sf") %>%
st_transform(crs = "+proj=eck4") %>%
st_geometry()
graticules <- ne_download(type = "graticules_15", category = "physical",
returnclass = "sf") %>%
st_transform(crs = "+proj=eck4") %>%
st_geometry()
bb <- ne_download(type = "wgs84_bounding_box", category = "physical",
returnclass = "sf") %>%
st_transform(crs = "+proj=eck4") %>%
st_geometry()
```
Template of Global map - with country borders
```{r, results="hide", warning=F, message=F}
w3a <- ggplot() +
geom_sf(data = bb, col = "grey20", fill = "white") +
geom_sf(data = graticules, col = "grey20", lwd = 0.1) +
geom_sf(data = countries, fill = "grey90", col = NA, lwd = 0.3) +
coord_sf(crs = "+proj=eck4") +
theme_minimal() +
theme(axis.text = element_blank(),
legend.title = element_text(size=12),
legend.text = element_text(size=12),
legend.background = element_rect(size = 0.1, linetype = "solid", colour = 1),
legend.key.height = unit(1.1, "cm"),
legend.key.width = unit(1.1, "cm"))
```
Project selected plots to Eckert IV and transform them to sf, before plotting.
```{r, results="hide", warning=F, message=F}
header.quercus.sf <- SpatialPointsDataFrame(coords = header.quercus %>%
dplyr::select(Longitude, Latitude),
proj4string = CRS("+init=epsg:4326"),
data=header.quercus %>%
dplyr::select(-Longitude, -Latitude)) %>%
st_as_sf() %>%
st_transform(crs = "+proj=eck4")
```
Show all plots containing at least one *Quercus* species. Color code based on biomes.
```{r, fig.width=8, fig.height=9, fig.align="center", warning=F, message=F}
(Figure1a <- w3a +
geom_sf(data = header.quercus.sf, aes(color = Biome),
pch = 16, size = 0.8, alpha = 0.8) +
geom_sf(data = countries, col = "grey20", fill=NA, lwd = 0.3) +
theme(legend.position = "bottom",
legend.title = element_blank()) +
guides(color = guide_legend(ncol = 2,
override.aes = list(size = 2))))
```
\pagebreak
# Create a reference list for selected plots
Create reference list as BibText
```{r}
sPlotOpen_citation(IDs=plotlist.quercus, level = "database",
out.file = "_output/demo.bib")
# show first few lines of output file
read_lines("_output/demo.bib", n_max = 25)
```
Convert to reference list
```{r, warning=F}
mybib <- RefManageR::ReadBib("_output/demo.bib", check = FALSE)
mybib
```
## sessionInfo()
```{r}
sessionInfo()
```