---
title: "Coding club 24/02"
output:
flexdashboard::flex_dashboard:
source_code: embed
orientation: rows
vertical_layout: scroll
theme:
version: 4
primary: "#C04384"
---
```{r set_theme_inbo, echo = FALSE, eval = FALSE}
theme_set(theme_inbo(base_size = 12))
switch_colour(inbo_steun_blauw)
```
```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse) # to do datascience
library(here) # to work easily with paths
library(sf) # to work with geospatial vector data
library(leaflet) # to make dynamic maps
library(DT)
library(shiny)
library(INBOtheme)
```
```{r}
cray_df <- readr::read_tsv(
here::here("data", "20250224_craywatch_cleaned.txt"),
na = "",
guess_max = 10000
)
```
Overzicht
=====================================
Row
-----------------------------------------------------------------------
### Craywatch data
```{r}
dataset_name <- "Waarnemingen.be - Non-native animal occurrences in Flanders and the Brussels Capital Region, Belgium"
n_obs_craywatch <- cray_df %>%
dplyr::filter(datasetName == dataset_name) %>%
nrow()
tot_obs <- nrow(cray_df)
percentage_craywatch <- n_obs_craywatch / tot_obs * 100
gauge(n_obs_craywatch, min = 0, max = tot_obs )
```
### Total number of Craywatch observations
```{r}
valueBox(n_obs_craywatch, icon = "fa-solid fa-camera")
```
Row {.tabset .tabset-fade}
-----------------------------------------------------------------------
### Aantal waarnemingen per dataset
```{r fig.width=15, fig.height=7}
cray_df |>
group_by(datasetName) |>
summarise(n_obs = n()) |>
arrange(n_obs) -> n_obs_per_dataset
ggplot(n_obs_per_dataset, aes(x = n_obs, y = reorder(datasetName, n_obs))) +
geom_bar(stat = "identity", color = "#C04384", fill = "#C04384") +
labs(x = "Aantal waarnemingen", y = NULL) +
theme_minimal() +
theme(
axis.text.x = element_text(angle = 45)
)
```
### Aantal waarnemingen per provincie
```{r fig.width=15, fig.height=7}
cray_df |>
dplyr::filter(stateProvince != "NA") |>
group_by(stateProvince) |>
summarise(n_obs = n()) |>
arrange(n_obs) -> n_obs_per_province
ggplot(n_obs_per_province, aes(x = n_obs, y = reorder(stateProvince, n_obs))) +
geom_bar(stat = "identity", color = "#C04384", fill = "#C04384") +
labs(x = "Aantal waarnemingen", y = NULL) +
theme_minimal() +
theme(
axis.text.x = element_text(angle = 45)
)
```
Visualisatie
=====================================
### Histogram (Species per date (year/month))
```{r plot-histogram}
n_obs_per_month_species <-
cray_df %>%
count(year, month, species) %>%
# combine year and month to a single date
mutate(date = as.Date(paste0(year, "-", month, "-01"))) %>%
arrange(date, species) %>%
relocate(date,species, n, everything())
ggplot(n_obs_per_month_species,
aes(x = date, y = n, fill = species)) +
geom_bar(stat = 'identity') +
# Use inferno colors for the species
scale_fill_viridis_d(option = "inferno") +
# Add title and labels
ggtitle("Number of observations per month and species") +
xlab("Date") + ylab("Number of observations")
```
### Leaflet map
```{r leaflet-map}
cray_fl <- sf::st_as_sf(cray_df,
coords = c("decimalLongitude", "decimalLatitude"),
crs = 4326)
# Create a palette that maps species to colors
pal <- colorFactor("inferno", cray_fl$species)
leaflet(cray_fl) %>%
addTiles() %>%
addCircleMarkers(popup = ~paste0(cray_fl$eventDate, ": ", cray_fl$species),
color = pal(cray_fl$species),
stroke = FALSE,
fillOpacity = 0.5,
radius = 4) %>%
addLegend(pal = pal, values = ~species,
position = "bottomright")
```
Data
=====================================
Row
-------------------------------------
```{r}
DT::datatable(cray_df, options = list(
pageLength = 100
))
```