Column

Average temperatures and annual precipitation in 10 locations in NY 1981-2010

Column

Distribution of annual snowfall for 10 locations in NY 1981-2010

Number of years with over 1000 mm precipitation by locations

---
title: "NY NOAA Dashboard"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    source_code: embed
---

```{r, setup, include = FALSE}
library(flexdashboard)
library(tidyverse)
library(p8105.datasets)
library(plotly)

data("ny_noaa")

#Select top 10 locations with the most data
ny_noaa %>% 
  drop_na() %>% 
  group_by(id) %>% 
  summarize(n_obs = n()) %>% 
  mutate(rank = min_rank(desc(n_obs))) %>% 
  filter(rank < 11) %>% 
  pull(id)

ny_noaa = 
  ny_noaa %>% 
  drop_na() %>% 
  filter(id %in% c("USC00304912", "USC00305426", "USC00306314", "USW00004725", "USW00014732", "USW00014733", "USW00014735", "USW00014768", "USW00014771", "USW00094789")) %>% 
  mutate(
    location = recode(
      id,
      USC00304912 = "Lowville",
      USC00305426 = "Mohonk Lake",
      USC00306314 = "Oswego",
      USW00004725 = "Binghamton",
      USW00014732 = "Laguardia Airport",
      USW00014733 = "Buffalo",
      USW00014735 = "Albany Airport",
      USW00014768 = "Rochester",
      USW00014771 = "Syracuse Hancock Airport",
      USW00094789 = "JFK Airport"),
    tmin = as.numeric(tmin) / 10,
    tmax = as.numeric(tmax) / 10,
    prcp = prcp / 10) %>%
  mutate(year = lubridate::year(date)) %>% 
  select(location, id, everything())
ny_noaa
```

Column {data-width=550}
-----------------------------------------------------------------------

### Average temperatures and annual precipitation in 10 locations in NY 1981-2010

```{r}
ny_noaa %>%
  group_by(location, year) %>% 
  summarize(
    tmin_mean = mean(tmin),
    tmax_mean = mean(tmax),
    prcp_total = sum(prcp)) %>% 
  mutate(text_label = str_c("Location: ", location, "\nYear: ", year, "\nPrecipitation (mm): ", prcp_total)) %>% 
  plot_ly(
    x = ~tmin_mean, y = ~tmax_mean, type = "scatter", mode = "markers",
    color = ~prcp_total, text = ~text_label) %>% 
  layout(
    xaxis = list(title = "Avg. Tmin (C)", zeroline = FALSE, range = c(-5, 15)),
    yaxis = list(title = "Avg. Tmax (C)"))
```

Column {data-width=450}
-----------------------------------------------------------------------

### Distribution of annual snowfall for 10 locations in NY 1981-2010

```{r}
ny_noaa %>%
  group_by(location, year) %>%
  summarize(annual_snow = sum(snow)) %>%
  ungroup() %>% 
  mutate(location = fct_reorder(location, annual_snow)) %>% 
  plot_ly(y = ~annual_snow, color = ~location, type = "box", colors = "viridis") %>% 
  layout(
    xaxis = list(title = "Location", tickangle = 45),
    yaxis = list(title = "Annual Snowfall (mm)"))
```


### Number of years with over 1000 mm precipitation by locations

```{r}
ny_noaa %>% 
  group_by(location, year) %>% 
  summarize(prcp_total = sum(prcp)) %>% 
  filter(prcp_total > 1000) %>%
  ungroup() %>% 
  count(location) %>% 
  mutate(location = fct_reorder(location, n)) %>% 
  plot_ly(x = ~location, y = ~n, color = ~location, type = "bar", colors = "viridis") %>% 
  layout(
    xaxis = list(title = "Location", tickangle = 45),
    yaxis = list(title = "n"))
```