---
title: "Glowworms"
execute:
  execute-dir: file
format:
  html:
    toc: true
    toc-depth: 2
    number-sections: true
    toc-location: left
    fig-cap-location: top
    code-fold: false
    code-tools: true
    theme: flatly
    page-layout: full
editor: visual
vignette: >
  %\VignetteIndexEntry{Glowworms}
  %\VignetteEngine{quarto::html}
  %\VignetteEncoding{UTF-8}
---

```{r, echo=FALSE, message=FALSE, warning=FALSE}
# Ensure the temporary library from R CMD check is visible (esp. on Windows)
libdir <- Sys.getenv("R_LIBS")
if (nzchar(libdir)) {
  parts <- strsplit(libdir, .Platform$path.sep, fixed = TRUE)[[1]]
  .libPaths(unique(c(parts, .libPaths())))
}

# now load your package
suppressPackageStartupMessages(library(ecotourism))
```

::: {style="text-align:center"}
![](image/glowworm.jpg){width="300"}

[Photo by Alan Rockefeller. Licensed under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/).]{style="font-size: 50%; align: center; margin-top:0.02em;"}
:::

## Introduction

This vignette demonstrates how to **analyze occurrence data for Glowworms in Australia**, using records from the [Atlas of Living Australia (ALA)](https://www.ala.org.au/).

The dataset has been prepared for you to explore, making it suitable for both study and practice with real-world ecological data. In this vignette we provide short examples of how to manipulate and visualize the dataset, but you are encouraged to develop your own creative approaches for analysis and visualization.

------------------------------------------------------------------------

This is the glimpse of your data :

```{r, echo=TRUE, eval=TRUE, message=FALSE, warning=FALSE}
library(dplyr)
library(ecotourism)
data("glowworms")
glowworms |> glimpse()
```

## Visualization

### Spatial Distribution Map

Distribution of Occurrence Glowworms Sightings in Australia

```{r echo=TRUE, fig.width=6, fig.height=4, eval=TRUE}
library(ggplot2)
library(ggthemes)

glowworms |> 
  ggplot() +
    geom_sf(data = oz_lga) +
    geom_point(aes(x = obs_lon, y = obs_lat), color = "red") +
    theme_map()
```

------------------------------------------------------------------------

## Weekly, Monthly, and Yearly Trends

Weekday Distribution of Glowworms Sightings

```{r echo=TRUE, fig.width=6, fig.height=4, eval=TRUE}

week_order <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")

glowworms |> 
  ggplot(aes(x = factor(weekday, levels = week_order))) +
    geom_bar() +
    labs(x = "Weekday", y = "Number of Records") +
    theme_minimal()
```

Monthly Distribution of Glowworms Sightings

```{r echo=TRUE, fig.width=6, fig.height=4, eval=TRUE, message=FALSE, warning=FALSE}
library(lubridate)
glowworms |>
    dplyr::mutate(month = month(month, label = TRUE, abbr = TRUE)) |>
    ggplot(aes(x = factor(month))) +
    geom_bar() +
    labs(x = "Month", y = "Number of Records") +
    theme_minimal()
```

Yearly Distribution of Glowworms Sightings

```{r echo=TRUE, fig.width=6, fig.height=4, eval=TRUE}
glowworms |>
    ggplot(aes(x = factor(year))) +
    geom_bar() +
    labs(x = "Year", y = "Number of Records")+
    theme_minimal()
```

------------------------------------------------------------------------

## Relational visualization

We want to see if `glowworms` occurrences are related to tourism quarter trips on the same day from the weather dataset.

Here’s a short R script that:

1.  Joins `glowworms` with **weather** using `ws_id` and `date`.

2.  Counts daily occurrences.

3.  Plots quarter number of `glowworms` sightings.

```{r, echo=TRUE, eval=TRUE, message=FALSE, warning=FALSE, fig.width=6, fig.height=4}

# Prepare glowworms occurrence counts per quarter
glowworms_quarterly <- glowworms |>
  mutate(quarter = quarter(date)) |>
  group_by(year, quarter, ws_id) |>
  summarise(occurrence = n(), .groups = "drop")

# tourism quarterly spot data set near glowworms occurrence
tourism_sub <- tourism_quarterly |>
   filter(ws_id %in% glowworms$ws_id)

glowworms_tourism <- glowworms_quarterly |>
  left_join(tourism_sub, by=c("ws_id", "year", "quarter"))

# Simple scatter plot: precipitation vs glowworms occurrence
ggplot(glowworms_tourism, aes(x = quarter, y = occurrence)) +
  geom_jitter(width=0.1) +
  stat_summary(colour="red", geom = "point", size=3) +
  labs(
    title = "Relationship between tourism and glowworms occurrence",
    x = "Quarter",
    y = "Occurrence (jittered)"
  ) +
  theme_minimal()
```
