-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreen_crab_report.Rmd
114 lines (79 loc) · 3.79 KB
/
green_crab_report.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
---
title: "Green Crab in NS"
output: html_document
---
# What is Rmarkdown
Rmarkdown is a form of literate programming that mixes R and markdown. First lets learn a little about markdown. Markdown is a human readable language with tags that define special formatting elements use to format documents. As you saw, when we render (i.e. knit) this document, we can get an html, pdf, or word document. This means you can make a whole report/manuscript reproducible including the text.
Notice that instead of octothorpes (#) being used as comments in markdown, they are titles
# Title
## Main section
### Sub-section
#### Sub-sub section
You can also have special formatting for text:
**bold**
_italics_
`code-type`
Additionally, R functions and code do nothing (yet):
mean(c(2,3,4))
... nothing! Until you place things in an R chunk:
```{r}
mean(c(2,3,4))
```
Will by default display the code and the output. You can hide the code in the rendered version:
```{r, echo=FALSE}
mean(c(2,3,4))
```
or you can hide the output:
```{r, results='hide'}
mean(c(2,3,4))
```
or you can also hide messages:
```{r, message=FALSE}
message("You will not see the message.")
```
Alternatively, you can render r 'inline' using back-ticks and the contents need to begin with 'r '. For example the mean(c(2,3,4)) is `r mean(c(2,3,4))` (that 3 was interpreted from the r code when we rendered the document!)
This makes these types of documents extremely responsive to changes or updates in data. An extremely powerful tool in your 'reproducibility' tool belt!
# Green Crab reporting
Suppose I want to create a reproducible report about the extent of the European green crab invasion in Nova Scotia. Let also pretend the report starts HERE, because we ran some R code above and didn't yet load our packages which should always happen at the top of the script, but you needed to learn about Rmarkdown first so I made an exception. We're also hiding all of this because we don't need that in the report!
```{r loading packages, message=FALSE, results='hide', echo=FALSE}
library(robis) # interface to get OBIS data
library(sf) # the GIS package of choice
library(rnaturalearth) # good background maps
library(ggplot2)
library(dplyr)
```
Cool right!
Now let's make a quick basemap:
```{r making basemap, message=FALSE, echo=FALSE}
latlong <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0" # for converting to lats/longs
# and a projection to use for nice plots in the maritimes:
maritimes_eqdc_proj <- "+proj=eqdc +lat_1=42.0 +lat_2=47.0 +lon_0=-62.0"
# getting and transforming Nova Scotia
NS <- ne_states(country = c("Canada"),returnclass = "sf") %>%
dplyr::filter(postal=="NS") %>% # get NS
st_transform(maritimes_eqdc_proj) # transform
map <- ggplot(NS) +
geom_sf(fill="burlywood",colour="burlywood",alpha=0.7)
map
```
Now let's get some green crab data from [OBIS](https://obis.org/). You can query by species scientific name and spatial geometry in text format. Our `NS` polygon is too complicated, so let's make a box around `NS`
```{r getting obis data}
txt_geom <- NS %>%
st_transform(latlong) %>% # transform back to latlong
st_bbox() %>% # get bounding box
st_as_sfc() %>% # convert bounding box to sf column polygon
st_as_text() # convert to text
crabs <- occurrence(scientificname = "Carcinus maenas",
geometry = txt_geom)
glimpse(crabs)
```
But not the `decimalLatitude` and `decimalLongitude` columns are not in the right format/units for plotting on a projected map.
```{r mapping crabs}
crabssf <- crabs %>%
st_as_sf(coords=c("decimalLongitude",
"decimalLatitude"),
crs=latlong) %>% # convert dataframe to sf object
st_transform(maritimes_eqdc_proj) # transform to eqdc
map +
geom_sf(data=crabssf)
```