inst/doc/palettephines-intro.R

## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 4
)

## ----palettes, echo = TRUE, warning = FALSE, message = FALSE------------------
# 1. Set up required libraries
library(palettephines)
library(ggplot2)

# 2. Capture current settings and setup the layout
# We use no.readonly = TRUE to ensure we only try to reset writeable parameters
oldpar <- par(no.readonly = TRUE) 
par(mfrow = c(3, 2), mar = c(4, 4, 3, 1))

# 3. Call the preview functions
# Using names(phines_metadata) is smart—it makes the code future-proof
invisible(lapply(names(phines_metadata), function(pal_name) {
  show_phines(pal_name)
}))

# 4. Reset to original user settings
par(oldpar)

## ----eda, echo = TRUE, warning = FALSE, message = FALSE-----------------------
#1. Load libraries
library(sf)
library(dplyr)
library(sfdep)

# 2. Simulate synthetic dataset
set.seed(42)
bbox <- st_bbox(c(xmin = 0, xmax = 10, ymin = 0, ymax = 10), crs = 3857)

# 3. Create the raw spatial data
reef_sf <- st_make_grid(bbox, cellsize = c(1, 1), n = c(10, 10)) %>%
  st_sf() %>%
  mutate(id = row_number(), sst_stress = runif(n(), 0, 0.3))

# 4. Inject Hotspot
hotspot_ids <- c(44, 45, 46, 54, 55, 56, 64, 65, 66)
reef_sf$sst_stress[hotspot_ids] <- runif(length(hotspot_ids), 0.7, 0.9)

# 5. Plot: Exploratory Data Analysis
ggplot(reef_sf) +
  geom_sf(aes(fill = sst_stress)) +
  scale_fill_viridis_c() +
  theme_void() +
  labs(title = "Step 1: Exploratory Data Analysis using Viridis", 
       subtitle = "Perceptually uniform mapping of raw thermal stress values")


## ----lisa, echo = TRUE, warning = FALSE, message = FALSE----------------------

# 6. Run calculations
reef_lisa <- reef_sf %>%
  mutate(nb = st_contiguity(geometry),
         wt = st_weights(nb),
         lisa = local_moran(sst_stress, nb, wt)) %>%
  tidyr::unnest(lisa)

# 7. Filter for the subsequent interpretive phase
hotspots <- reef_lisa %>%
  filter(p_folded_sim < 0.05, mean == "High-High")

# 8. Plot: Pure LISA Classification
ggplot(reef_lisa) +
  geom_sf(aes(fill = mean), color = "white", size = 0.1) +
  scale_fill_manual(
    values = c("High-High" = "#E31A1C", "Low-Low" = "#1F78B4", 
               "Low-High" = "#A6CEE3", "High-Low" = "#FB9A99"),
    na.value = "grey95", name = "LISA Cluster"
  ) +
  theme_minimal() +
  labs(title = "Step 2: Pure LISA Analysis",
       subtitle = "Mathematical identification of spatial clumping (p < 0.05)")


## ----wrap-local-context, echo = TRUE, warning = FALSE, message = FALSE--------

# 9. Plot: Wrap Local Philippine context 
ggplot() +
  geom_sf(data = reef_lisa, fill = "grey95", color = "white", size = 0.1) +
  geom_sf(data = hotspots, aes(fill = sst_stress), color = "black", size = 0.5) +
  scale_fill_phines(
    palette = "coral_bleach", discrete = FALSE, name = "RHI Alert Level",
    breaks = c(0.7, 0.8, 0.9), labels = c("Stress", "Bleaching", "Mortality")
  ) +
  theme_minimal() +
  labs(title = "Step 3: Local Interpretation",
       subtitle = "Significant clusters translated to Reef Health Index (RHI) states")


## ----cite, echo = TRUE, warning = FALSE, message = FALSE----------------------

# 10. Translate to written insight
if (nrow(hotspots) > 0) {
  representative_value <- median(hotspots$sst_stress, na.rm = TRUE)
  report_status <- cite_phines(palette = "coral_bleach", value = representative_value)
} else {
  report_status <- "No significant thermal hotspots detected."
}

cat("#> Official Analytical Report:", report_status)

Try the palettephines package in your browser

Any scripts or data that you put into this service are public.

palettephines documentation built on April 25, 2026, 1:07 a.m.