-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGN2-synthese-dashboard.rmd
369 lines (306 loc) · 12.7 KB
/
GN2-synthese-dashboard.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
---
title: "Rapport des observations d'invertébrés dans la base GeoNature du Parc national des Ecrins"
developer: "Rémy Moine, Damien Combrisson, Gil Deluermoz, Théo Lechémia"
output:
flexdashboard::flex_dashboard:
orientation: columns
social:
theme: united
---
```{r setup, include=FALSE, results=FALSE}
library(lubridate)
##############################################################################
#DEBUT SECTION
##############################################################################
date_rapport_min10<-'2017-01-01'# période de 1 an avant la période concernée (médianne)(date_rapport_min moins 1 an)
date_rapport_min<-'2018-01-01'# début période concernée par le rapport (AAAA-MM-JJ)
date_rapport_max<-'2018-12-31'# fin période concernée par le rapport (AAAA-MM-JJ)
res<-"1 month"
id<-"rpne"
pw<-"18Rpne95"
library(highcharter)
library(dplyr)
library(viridisLite)
library(forecast)
library(treemap)
library(flexdashboard)
thm <-
hc_theme(
colors = c("#981D97", "#FF8200", "#CEDC00", "#00A3E0"),
chart = list(
backgroundColor = "#981D97",
style = list(fontFamily = "Source Sans Pro")
),
xAxis = list(
gridLineWidth = 1
)
)
library(RPostgreSQL)
# loads the PostgreSQL driver
drv <- dbDriver("PostgreSQL")
# creates a connection to the postgres database
# note that "con" will be used later in each connection to the database
con <- dbConnect(drv, dbname = "geonature2db",
host = "url", port = 5432,
user = id, password = pw)
```
```{sql Données observées période, connection=con, output.var="danow", echo=F, message=F, warning=F }
SELECT s.id_synthese,
s.date_min as dateobs,
s.observers as observateurs,
s.determiner as determinateur,
n.cd_nom,
tx.nom_valide,
tx.lb_nom AS nom_latin,
tx.classe,
tx.ordre,
tx.famille,
tx.phylum,
s.count_min as effectif_total,
s.altitude_min AS altitude,
st_x(st_transform(s.the_geom_point, 2154))::integer AS x,
st_y(st_transform(s.the_geom_point, 2154))::integer AS y,
s.meta_create_date as date_insert,
s.meta_update_date as date_update,
sou.name_source as nom_source,
st_x(st_transform(s.the_geom_point, 4326))::integer AS xwgs,
st_y(st_transform(s.the_geom_point, 4326))::integer AS ywgs
FROM gn_synthese.synthese s
JOIN taxonomie.taxref tx ON tx.cd_nom = s.cd_nom
JOIN taxonomie.bib_noms n ON n.cd_nom = s.cd_nom
JOIN gn_synthese.t_sources sou ON sou.id_source = s.id_source
WHERE
tx.phylum in ('Arthropoda', 'Mollusca') AND
s.date_min >= ?date_rapport_min AND
s.date_min < ?date_rapport_max
;
```
```{sql connection= con,output.var="spnew",echo=F}
WITH
taxon_after AS (
SELECT DISTINCT s.cd_nom
FROM gn_synthese.synthese s
JOIN taxonomie.taxref tx ON tx.cd_nom = s.cd_nom
JOIN gn_synthese.t_sources sou ON sou.id_source = s.id_source
WHERE
tx.phylum in ('Arthropoda', 'Mollusca') AND
s.date_min >= ?date_rapport_min AND
s.date_min < ?date_rapport_max
)
,taxon_before AS (
SELECT DISTINCT s.cd_nom
FROM gn_synthese.synthese s
JOIN taxonomie.taxref tx ON tx.cd_nom = s.cd_nom
JOIN gn_synthese.t_sources sou ON sou.id_source = s.id_source
WHERE
tx.phylum IN ('Arthropoda', 'Mollusca') AND
s.date_min < ?date_rapport_min
)
SELECT DISTINCT
s.observers AS observateurs,
s.date_min aS dateobs,
t.cd_nom,
t.nom_valide,
t.classe,
t.ordre,
t.famille,
st_x(st_transform(s.the_geom_point, 2154))::integer AS x,
st_y(st_transform(s.the_geom_point, 2154))::integer AS y,
sou.name_source as nom_source,
st_x(st_transform(s.the_geom_point, 4326))::float AS xwgs,
st_y(st_transform(s.the_geom_point, 4326))::float AS ywgs
FROM taxon_after ta
JOIN taxonomie.taxref t ON t.cd_nom = ta.cd_nom
JOIN gn_synthese.synthese s ON s.cd_nom = ta.cd_nom
JOIN gn_meta.t_datasets l ON l.id_dataset = s.id_dataset
JOIN gn_synthese.t_sources sou ON sou.id_source = s.id_source
WHERE ta.cd_nom NOT IN (SELECT cd_nom FROM taxon_before)
ORDER BY t.classe, t.ordre, t.famille, t.nom_valide
;
```
```{sql Espèces observées sur 1 ans avant période, connection=con, output.var="sp10ans", include=F }
SELECT s.id_synthese,
s. date_min AS dateobs,
n.cd_nom,
tx.nom_valide,
tx.lb_nom AS nom_latin,
s.altitude_min AS altitude,
st_x(st_transform(s.the_geom_point, 2154))::integer AS x,
st_y(st_transform(s.the_geom_point, 2154))::integer AS y,
st_x(st_transform(s.the_geom_point, 2154))::integer AS xwgs,
st_y(st_transform(s.the_geom_point, 2154))::integer AS ywgs
FROM gn_synthese.synthese s
JOIN taxonomie.taxref tx ON tx.cd_nom = s.cd_nom
JOIN taxonomie.bib_noms n ON n.cd_nom = s.cd_nom
WHERE
tx.phylum IN ('Arthropoda', 'Molusca') AND
s.date_min < ?date_rapport_min AND
s.date_max >= ?date_rapport_min10
;
```
```{sql Espèces observées avant période, connection=con, output.var="sptotref", include=F }
SELECT s.id_synthese,
s.date_min as dateobs,
n.cd_nom,
tx.nom_valide,
tx.lb_nom AS nom_latin,
s.altitude_min AS altitude,
st_x(st_transform(s.the_geom_point, 2154))::integer AS x,
st_y(st_transform(s.the_geom_point, 2154))::integer AS y,
st_x(st_transform(s.the_geom_point, 2154))::integer AS xwgs,
st_y(st_transform(s.the_geom_point, 2154))::integer AS ywgs
FROM gn_synthese.synthese s
JOIN taxonomie.taxref tx ON tx.cd_nom = s.cd_nom
JOIN taxonomie.bib_noms n ON n.cd_nom = s.cd_nom
WHERE
tx.phylum in ('Arthropoda', 'Molusca') AND
s.date_min < ?date_rapport_min AND
s.date_max >= ?date_rapport_min10
;
```
```{r meta, include=FALSE}
cache<-dbDisconnect(con)
nbobs<-length(unique(danow$id_synthese))
dates<-sort(unique(danow$dateobs))
nbtaxa_cd<-length(unique(danow$cd_nom))
nbnewsp<-length(unique(spnew$cd_nom))
tauxnew<-round(nbnewsp/nbtaxa_cd*100,0)
nom<-as.character(paste("Rapport-Annuel-Invertebres_",unique(last(year(dates))),"_especes-nouvelles-observées_PNE.csv",sep=""))
write.csv(spnew,file=nom)
```
Le rapport couvre la période du `r paste(day(date_rapport_min),month(date_rapport_min),year(date_rapport_min),sep="/")` au `r paste(day(date_rapport_max),month(date_rapport_max),year(date_rapport_max),sep="/")`.
Column{.tabset data-width=600}
-----------------------------------------------------------------------
### Vue générale
```{r fig.}
library(ggplot2)
library(plotly)
library(lubridate)
nbtaxageonat<-length(unique(sptotref$cd_nom))
sumup<-data.frame(denomb=c(nbtaxageonat,nbtaxa_cd-nbnewsp,nbnewsp),
category=c(paste("Taxons dans Géonature non recontactés (",ymd(date_rapport_min10)," -> ",ymd(date_rapport_min10),")",sep=""),
paste("Taxons déjà référencés dans Géonature recontactés (",ymd(date_rapport_min)," -> ",ymd(date_rapport_max),")",sep=""),
paste("Nouveaux taxons (",ymd(date_rapport_min)," -> ",ymd(date_rapport_max),")",sep="")))
sumup$fraction = sumup$denomb / sum(sumup$denomb)
sumup = sumup[order(sumup$fraction), ]
sumup$ymax = cumsum(sumup$fraction)
sumup$ymin = c(0, head(sumup$ymax, n=-1))
col<-c("#FF8200", "#981D97","#CEDC00")
sumup %>%
plot_ly(labels= ~category, values = ~denomb,
insidetextfont = list(col = '#FFFFFF'),
marker = list(colors = col,
line = list(col = '#FFFFFF', width = 1))) %>%
add_pie(hole = 0.6) %>%
layout(showlegend = F,
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
```
### Répartition du nombre d'observations
```{r métadonnées_graph}
library(lubridate)
library(ggplot2)
library(plotly)
library(stringr)
date<-as_date(paste(year(dates),month(sp10ans$dateobs),day(sp10ans$dateobs),sep="-"))
date1<-data.frame(date,count=rep(1,length(date)))
agg0<-aggregate(count~date,data=date1,sum)
danow1<-data.frame(danow,week=week(danow$dateobs))
BREAKS1<-aggregate(dateobs~week,data=danow1,max)
p<-ggplot(danow,aes(x=dateobs))+
geom_vline(aes(xintercept=as.numeric(median(agg0$date), na.rm=T),color=paste(day(median(agg0$date)),"/",month(median(agg0$date))," (période du\n ",ymd(date_rapport_min10)," au ",ymd(date_rapport_min),")",sep="")),linetype="dashed")+
geom_vline(aes(xintercept=as.numeric(median(danow$dateobs), na.rm=T),color=paste(day(median(danow$dateobs)),"/",month(median(danow$dateobs))," (période du\n ",ymd(date_rapport_min)," au ",ymd(date_rapport_max),")",sep="")),linetype="dashed")+
scale_color_manual(values=c("#981D97","#CEDC00"),name = "\nDate médiane: ")+
geom_freqpoly(color="#981D97",breaks=BREAKS1$dateobs)+
xlab("Date")+
ylab("Nombre d'observations")+
scale_x_date(date_labels="%d/%m",breaks="1 month")+
theme(axis.text.x = element_text(size=8, angle=45,vjust= 1, hjust= 1),
panel.grid.major = element_line(colour="grey"),
legend.position = "bottom",
panel.background = element_blank())
ggplotly(p,tooltip="count")
```
Column {.tabset width=400}
-----------------------------------------------------------------------
### Répartition taxonomique
```{r Préparation des données}
toto<-data.frame(danow[,c(5:6,8:11)],count=rep(1,nrow(danow)))
agg1<-aggregate(toto$count,by=list(toto$phylum,toto$classe,toto$ordre,toto$famille,toto$cd_nom,toto$nom_valide),"sum")
agg2<-agg1%>%arrange(Group.1,Group.2,Group.3,Group.4,Group.5,Group.6)
colnames(agg2)<-c("Phylum","Classe","Ordre","Famille","Cd_nom","Espèce","Nb_individus")
taux_error_taxo<-round((length(unique(toto$cd_nom))-length(unique(agg2$Cd_nom)))/length(unique(toto$cd_nom))*100,1)
name<-paste(agg2$Espèce," (cd_nom= ",agg2$Cd_nom,")",sep="")
nodesize<-agg2$Nb_individus
dagg2<-data.frame(agg2,name,nodesize)
lab<-paste(nrow(dagg2),"espèces")
```
```{r}
# par ordre --> RS, nb d'occurences, nb nouvelles espèces = en nb + en %
temp1<-data.frame(danow[,c("classe","ordre","cd_nom","nom_valide","id_synthese")],
occurence=rep(1,nrow(danow)),
count=rep(1,nrow(danow)))
library(dplyr)
library(formattable)
aggtemp<-aggregate(occurence~classe+ordre,temp1,sum)
aggtemp1<-aggtemp%>%arrange(classe,ordre)
nb_occur<-rep(0,nrow(aggtemp1))
taux_nb_occur<-rep(0,nrow(aggtemp1))
nb_sp<-rep(0,nrow(aggtemp1))
taux_nb_sp<-rep(0,nrow(aggtemp1))
nb_new_sp<-rep(0,nrow(aggtemp1))
taux_nouv_sp<-rep(0,nrow(aggtemp1))
ordre<-rep(0,nrow(aggtemp1))
for (i in c(1:nrow(aggtemp1))){
at<-which(aggtemp1[i,"ordre"]==temp1$ordre)
nb_occur[i]<-length(unique(temp1[at,"id_synthese"]))
ordre[i]<-unique(temp1[at,"ordre"])
nb_sp[i]<-length(unique(temp1[at,"cd_nom"]))
nb_new_sp[i]<-length(which(unique(temp1[at,"cd_nom"])%in%unique(spnew$cd_nom)))
}
datatab<-data.frame(aggtemp1[,-3],
nb_occur,
taux_nb_occur,
nb_sp,
taux_nb_sp,
nb_new_sp,
taux_nouv_sp)
datatab$taux_nb_occur<-round(datatab$nb_occur/sum(datatab$nb_occur)*100)
datatab$taux_nb_sp<-round(datatab$nb_sp/sum(datatab$nb_sp)*100)
datatab$taux_nouv_sp<-round(datatab$nb_new_sp/sum(datatab$nb_sp)*100)
colnames(datatab)<-c("Classe","Ordre",
"Nb. occurences",
"% occurences",
"Nb. espèces",
"% espèces",
"Nb. nouvelles espèces",
"% espèces nouvelles")
datatab<-rbind(datatab,TOTAL=c(NA,NA,colSums(datatab[,-c(1,2)])))
formattable(datatab)
```
### Localisation
```{r, Cartographie, echo= F}
library(ggmap)
library(sp)
toto<-data.frame(danow[,c("id_synthese","nom_valide","observateurs","dateobs","effectif_total","altitude","x","y","cd_nom")])
merc<-CRS("+init=epsg:3857")
l93<-CRS("+init=epsg:2154")
wgs84<-CRS("+init=epsg:4326")
sp_toto_l93<-SpatialPointsDataFrame(coords=toto[,c(7,8)],data=toto[,-c(7,8)])
proj4string(sp_toto_l93)<-l93
sp_toto_wgs84<-spTransform(sp_toto_l93,CRS("+init=epsg:4326"))
sp_toto_merc<-spTransform(sp_toto_wgs84,CRS("+init=epsg:3857"))
popup<-paste(sp_toto_wgs84@data[,2]," (cd_nom=",sp_toto_wgs84@data[,7],", observation n° ",sp_toto_wgs84@data[,1], "), le ",sp_toto_wgs84@data[,4]," par ",sp_toto_wgs84@data[,3]," (",sp_toto_wgs84@data[,5],"individus à ",sp_toto_wgs84@data[,6]," mètres d'altitude).")
colramp<-rep("#981D97",length(sp_toto_wgs84@data$cd_nom))
newsp1<-which(sp_toto_wgs84@data$cd_nom%in%spnew$cd_nom)
colramp[newsp1]<-"#FF8200"
library(leaflet)
leaflet() %>%
addTiles() %>%
addCircleMarkers(lng=as.vector(sp_toto_wgs84@coords[,1]), lat=as.vector(sp_toto_wgs84@coords[,2]),radius=5, popup=popup, color=colramp,
stroke = FALSE, fillOpacity = 1)%>%
addLegend("bottomright",colors=unique(colramp),
title=c("Observations de "),
labels=c("taxons déjà connus","nouvelles espèces"),
opacity=1 )
```