-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEAS_bird_plots.R
145 lines (111 loc) · 4.5 KB
/
EAS_bird_plots.R
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
########################-
#### EAS bird plots ####
########################-
## plot functions for posterior predictions
## 1) plot abundance
## plot abundance trend
plot.ab <- function(df.raw = NULL, newdat = NULL, group = NULL) {
g <- ggplot() +
# add horizontal line at y = 0
geom_hline(yintercept = 0, lty = "dashed", linewidth = 0.5) +
# add raw data (colored dots)
geom_beeswarm(data = df.raw, aes(x = year, y = ab2/2, color = nat.region),
size = 0.5, cex = 0.3, alpha = 0.5) +
# add trend
geom_ribbon(data = tmp,
aes(x = year, ymin = lwr, ymax = upr,
color = nat.region, fill = nat.region),
alpha = 0.3) +
geom_line(data = tmp,
aes(x = year, y = fit, color = nat.region),
lwd = 1) +
# add sign. slope
geom_line(aes(year, fit.slope, group = nat.region),
data = tmp,
col = "black", lty = 1, linewidth = 1, na.rm = TRUE) +
# add labs
ylim(0, max(c(df.raw$ab2/2, tmp$upr))) +
labs(x = "year", y = "abundance per km²",
title = paste0(sp, " (", mod.fam, ", ", zi.type, ")"),
subtitle = "black = period of robust increase/decrease") +
facet_wrap(~nat.region) +
theme_classic() +
theme(legend.position = "none")
## add colors
if(group == "nat.region") {
g <- g +
scale_color_viridis_d("nat. region", end = 0.8, option = "plasma") +
scale_fill_viridis_d("nat. region", end = 0.8, option = "plasma")
}
if(group == "region") {
g <- g +
scale_color_manual("region", values = c("atl" = "blue", "kon" = "orange",
"overall" = "grey40")) +
scale_fill_manual("region", values = c("atl" = "blue", "kon" = "orange",
"overall" = "grey40"))
}
return(print(g))
}
## plot index
plot.in <- function(newdat = NULL, group = NULL) {
g <- ggplot(newdat, aes(x = year, y = fit.i, ymin = lwr.i, ymax = upr.i)) +
# add horizontal lines at y = 0 and y = 1
geom_hline(yintercept = 0, lty = "dashed", lwd = 0.5) +
geom_hline(yintercept = 1, lty = "dashed", lwd = 0.5) +
# add trend
geom_ribbon(aes(color = nat.region, fill = nat.region), alpha = 0.2) +
geom_line(aes(color = nat.region), lwd = 1) +
# add sign. slope
geom_line(aes(year, fit.slope.i, group = nat.region),
col = "black", lty = 1, linewidth = 1, na.rm = TRUE) +
# add labs
ylim(0, max(tmp$upr.i)) +
labs(y = "Index", x = "survey year",
title = paste0(sp, " (", mod.fam, ", ", zi.type, ")")) +
facet_wrap(~nat.region) +
theme_classic() +
theme(legend.position = "none")
## add colors
if(group == "nat.region") {
g <- g +
scale_color_viridis_d("nat. region", end = 0.8, option = "plasma") +
scale_fill_viridis_d("nat. region", end = 0.8, option = "plasma")
}
if(group == "region") {
g <- g +
scale_color_manual("region", values = c("atl" = "blue", "kon" = "orange",
"overall" = "grey40")) +
scale_fill_manual("region", values = c("atl" = "blue", "kon" = "orange",
"overall" = "grey40"))
}
return(print(g))
}
## plot longterm trend
plot.lt <- function(newdat = NULL, group = NULL) {
g <- ggplot(newdat) +
# add horizontal line at 0
geom_hline(yintercept = 0, lty = "dashed", linewidth = 0.5) +
# add longterm trend
geom_pointrange(aes(x = nat.region, y = fit, ymin = lwr, ymax = upr, color = nat.region),
linewidth = 1, fatten = 8) +
geom_pointrange(aes(x = nat.region, y = fit, ymin = lwr25, ymax = upr75, color = nat.region),
linewidth = 2, fatten = 8) +
# add labs
labs(x = group,
y = paste0(period, "-year abundance difference \n (", yearP - period, "-", yearP, ")"),
title = paste0(sp, " (", mod.fam, ", ", zi.type, ")"),
subtitle = "Longterm trend with 50% (thick) and 95% (thin) CrI") +
theme_classic() +
theme(legend.position = "none")
## add colors
if(group == "nat.region") {
g <- g +
scale_color_viridis_d("nat. region", end = 0.8, option = "plasma")
}
if(group == "region") {
g <- g +
scale_color_manual("region", values = c("atl" = "blue", "kon" = "orange",
"overall" = "grey40"))
}
return(print(g))
}