---
title: "Hurricane Data"
subtitle: "with usmap()"
theme: flatly 
date: today
toc: true
format:
  html: 
    code-overflow: wrap
    toc: true
    toc-title: "Contents"
    # hide code?

execute:
  warning: false
  message: false
---



### Hurricane Data

One interesting analysis is

. [Hurricane Data Analysis in R](https://michaelminn.net/tutorials/r-hurricanes/index.html)

Another, lighter visualization: 

. [Hurricane Watch](https://flowingdata.com/2024/10/09/process-309-hurricane-watch/) from FlowingData.

. [2020 Hurricane Season](https://en.wikipedia.org/wiki/2020_Atlantic_hurricane_season)

### New packages

* We'll use the [sf](https://r-spatial.github.io/sf/) package.

* Also, the [usmap](https://cran.r-project.org/web/packages/usmap/vignettes/usmap2.html) package.

* And, the [clock](https://www.tidyverse.org/blog/2021/03/clock-0-1-0/#what-about-lubridate) package.

* And, the [sf](https://r-spatial.github.io/sf/) package for spatial features.

```{r}
# packages to install
library(tidyverse)
# install.packages("usmap")
# install.packages("sf")
library(sf) # spatial features
library(usmap)
# install.packages("clock")
# 
# just install, do not call library on usmap
```

## Updated Storms Data
 
* Download new storm data [here](Week7/storms.csv)
  
* This was created by following [this](https://github.com/tidyverse/dplyr/blob/main/data-raw/storms.R)
and getting the data [here](https://www.nhc.noaa.gov/data/#hurdat).
  
## Plot trajectories on a map



```{r}
# load updated storms data
storms <- read_csv("../data/storms.csv") |>
  # the usmap_transform() function requires "lon"
  rename(lon = long) |>
  mutate(category = as_factor(category))

df_s <- storms |> 
  filter(status == "hurricane",
         as.numeric(category) > 2,
         year == 2023)

# project lon/lat coords to match the usmap projection
# be sure to look at names(df_t)
df_t <- usmap_transform(df_s)

# can also do: include = c("CO","MI") below
plot_usmap(include = .south_region) + 
  # geom_sf does a lot for you here, 
  # as it uses the "geometry" variable to determine location on map
  geom_sf(data = df_t) +
  geom_sf_label(data = df_t, aes(label = name))
```

```{r}
# date_build uses the "clock" library
storms <- storms |> mutate(
    date = clock::date_build(year, month, day))

storms |> 
  filter(status == "hurricane",
         year == 2023) |> 
ggplot(
       aes(
         x = date,
         y = name,
         color = category)) + 
#  geom_line(size=2) + 
  geom_point(aes(size = category))
```


```{r}
library(ggpointdensity)
 storms |> ggplot(aes(x=year,y=lat)) + geom_pointdensity(size=.6) + 
+     scale_color_viridis_b()
```

