-
Notifications
You must be signed in to change notification settings - Fork 0
/
watervogels.Rmd
188 lines (129 loc) · 4.91 KB
/
watervogels.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
---
title: "Watervogels"
author: "gehau"
date: "December 7, 2017"
output:
html_document: default
pdf_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
```
## Watervogels
```{r lees_bestand}
setwd("/home/gehau/git/watervogels")
waarnemingen <- readRDS("data/V3/occurrence.rds")
waarnemingen <- waarnemingen[which( !is.na(waarnemingen$decimalLatitude), arr.ind=TRUE),]
waarnemingen <- waarnemingen[which( !is.na(waarnemingen$decimalLongitude), arr.ind=TRUE),]
```
```{r reshape , include=FALSE}
require(reshape) #om colsplit te gebruiken
```
```{r transform , warning=FALSE}
date_split <-
colsplit(waarnemingen$eventDate,
split = "-",
names = c('jaar', 'maand', 'tmp'))
day_split <-
colsplit(date_split$tmp,
split = "T",
names = c('dag', 'tijd'))
jaar <- as.character(date_split$jaar)
maand <- as.character(date_split$maand)
dag <- as.character(day_split$dag)
tijd <- as.character(day_split$tijd)
soort <- waarnemingen$vernacularName
aantal_individuen <- waarnemingen$individualCount
waarnemingen <- cbind(waarnemingen, soort, aantal_individuen, jaar, maand, dag, tijd)
wnm <- subset(waarnemingen, select = c(eventID,soort, aantal_individuen,jaar, maand, dag, tijd))
```
```{r ggplot, warning=FALSE}
# create plot
p <- ggplot(data = wnm, aes(x = aantal_individuen)) + theme(axis.text.x = element_text(angle = 60, hjust = 1))
# add geom
p <- p + geom_histogram()
p <- p + scale_x_log10()
p <- p + aes(fill = maand)
p <- p + scale_fill_discrete(
breaks=c("1","2","3","4","5","6","7","8","9","10","11","12"),
labels=c("januari", "februari", "maart", "april", "mei", "juni", "juli", "augustus", "september", "oktober", "november", "december"))
p
```
```{r map, warning=FALSE}
p <- ggplot(data = wnm, aes(x = jaar, y = aantal_individuen)) + theme(axis.text.x = element_text(angle = 60, hjust = 1))
# add geom
p <- p + geom_bar(stat = 'identity', width = .5)
# update data layer with new mapping
p <- p %+% aes(fill = maand)
p <- p + scale_fill_discrete(
breaks=c("1","2","3","4","5","6","7","8","9","10","11","12"),
labels=c("januari", "februari", "maart", "april", "mei", "juni", "juli", "augustus", "september", "oktober", "november", "december"))
# titel en subtitel
p <- p %+% labs(subtitle = "todo", title = "Aantal watervogels")
p
```
```{r ggplot2, warning=FALSE}
# add facets + fix x-axis
p + facet_wrap(~maand, scales = 'free_x')
```
```{r boxplot}
# create boxplot var
bp <- ggplot(wnm, aes(x = jaar, y = aantal_individuen, fill = maand)) +
geom_boxplot() + scale_y_log10() + theme(axis.text.x = element_text(angle = 60, hjust = 1)) +
scale_fill_discrete(
breaks=c("1","2","3","4","5","6","7","8","9","10","11","12"),
labels=c("januari", "februari", "maart", "april", "mei", "juni", "juli", "augustus", "september", "oktober", "november", "december")) + labs(subtitle = "todo", title = "Aantal watervogels")
# boxplot
bp
# we'll use this a lot
fw <- facet_wrap(~maand, scales = 'free_y')
bp + fw
#
# bp.h <- bp %+% aes(x = as.factor(jaar))
#
# bp.h + fw
```
# Create correlation grid
```{r dplyr , include=FALSE}
library(dplyr) #
```
Top tien van de watervogels
```{r topTien, warning=FALSE}
vogelsoorten <- subset(wnm, select = c(soort, aantal_individuen))
vogelsoorten %>%
group_by(soort) %>%
summarise(aantal_individuen = sum(aantal_individuen)) %>%
arrange(desc(aantal_individuen)) %>%
head(n = 10L)
```
How correlated is the abundance of different species?
```{r GGally , include=FALSE}
# import library
library(GGally)
```
```{r corr, warning=FALSE}
# create a sample of 1000 obs
sample_wnm <- subset(wnm, select = c(eventID, soort, aantal_individuen, maand)) %>%
spread(soort, aantal_individuen)%>% #spread dplyr
subset(select = c(Smient,Kievit,Kokmeeuw,Kuifeend,Tafeleend,Kolgans,maand))
set.seed(888)
#sample_wnm <- sample_wnm[sample(1:nrow(sample_wnm),1000),]
# look at correlations
ggpairs(data=sample_wnm, # data.frame with variables
title="Bird species occurence correlations") + theme(axis.text.x = element_text(angle = 60, hjust = 1))
```
```{r add alpha and trendline, warning=FALSE}
ggpairs(data = sample_wnm,
lower = list(continuous = wrap("smooth", alpha=1/5, shape = I('.'),
colour ='blue')),
title="Bird species occurence correlations") + theme(axis.text.x = element_text(angle = 60, hjust = 1))
```
```{r line plot}
# create line plot
p <- ggplot( waarnemingen[waarnemingen$municipality == "Kalmthout",], aes(verbatimEventDate, individualCount)) +
geom_line(colour = 'darkgrey') + scale_y_log10() #+ theme(axis.text.x = element_text(angle = 60, hjust = 1))
# change default grouping
p <- p %+% aes(group = vernacularName)
p + facet_wrap(~jaar, scales = 'free_x')
```