library(readr)
library(ggplot2)

#reaeds in datasets
pollution_url <- '/home/mes369/csds133/mes369/24sp-csds133-mes369/5-FinalProject/pollution_2000_2023.csv'
pollution_data <- read_csv(pollution_url)
## New names:
## Rows: 665414 Columns: 22
## ── Column specification
## ──────────────────────────────────────────────────────── Delimiter: "," chr
## (4): Address, State, County, City dbl (17): ...1, O3 Mean, O3 1st Max Value, O3
## 1st Max Hour, O3 AQI, CO Mean... date (1): Date
## ℹ Use `spec()` to retrieve the full column specification for this data. ℹ
## Specify the column types or set `show_col_types = FALSE` to quiet this message.
## • `` -> `...1`
weather_url = '/home/mes369/csds133/mes369/24sp-csds133-mes369/5-FinalProject/WeatherEvents_Jan2016-Dec2022 2.csv'
weather_data = read_csv(weather_url)
## Rows: 8627181 Columns: 14
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (8): EventId, Type, Severity, TimeZone, AirportCode, City, County, State
## dbl  (4): Precipitation(in), LocationLat, LocationLng, ZipCode
## dttm (2): StartTime(UTC), EndTime(UTC)
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#should clean data remove NA's etc 
#making a plot for amount of severe weather events by state using weather dataset
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
#exploratory analysis
str(weather_data) 
## spc_tbl_ [8,627,181 × 14] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ EventId          : chr [1:8627181] "W-1" "W-2" "W-3" "W-4" ...
##  $ Type             : chr [1:8627181] "Snow" "Snow" "Snow" "Snow" ...
##  $ Severity         : chr [1:8627181] "Light" "Light" "Light" "Light" ...
##  $ StartTime(UTC)   : POSIXct[1:8627181], format: "2016-01-06 23:14:00" "2016-01-07 04:14:00" ...
##  $ EndTime(UTC)     : POSIXct[1:8627181], format: "2016-01-07 00:34:00" "2016-01-07 04:54:00" ...
##  $ Precipitation(in): num [1:8627181] 0 0 0.03 0 0 0 0 0 0 0 ...
##  $ TimeZone         : chr [1:8627181] "US/Mountain" "US/Mountain" "US/Mountain" "US/Mountain" ...
##  $ AirportCode      : chr [1:8627181] "K04V" "K04V" "K04V" "K04V" ...
##  $ LocationLat      : num [1:8627181] 38.1 38.1 38.1 38.1 38.1 ...
##  $ LocationLng      : num [1:8627181] -106 -106 -106 -106 -106 ...
##  $ City             : chr [1:8627181] "Saguache" "Saguache" "Saguache" "Saguache" ...
##  $ County           : chr [1:8627181] "Saguache" "Saguache" "Saguache" "Saguache" ...
##  $ State            : chr [1:8627181] "CO" "CO" "CO" "CO" ...
##  $ ZipCode          : num [1:8627181] 81149 81149 81149 81149 81149 ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   EventId = col_character(),
##   ..   Type = col_character(),
##   ..   Severity = col_character(),
##   ..   `StartTime(UTC)` = col_datetime(format = ""),
##   ..   `EndTime(UTC)` = col_datetime(format = ""),
##   ..   `Precipitation(in)` = col_double(),
##   ..   TimeZone = col_character(),
##   ..   AirportCode = col_character(),
##   ..   LocationLat = col_double(),
##   ..   LocationLng = col_double(),
##   ..   City = col_character(),
##   ..   County = col_character(),
##   ..   State = col_character(),
##   ..   ZipCode = col_double()
##   .. )
##  - attr(*, "problems")=<externalptr>
summary(weather_data)
##    EventId              Type             Severity        
##  Length:8627181     Length:8627181     Length:8627181    
##  Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character  
##                                                          
##                                                          
##                                                          
##                                                          
##  StartTime(UTC)                    EndTime(UTC)                   
##  Min.   :2016-01-01 05:00:00.00   Min.   :2016-01-01 05:15:00.00  
##  1st Qu.:2017-10-07 22:15:00.00   1st Qu.:2017-10-07 23:13:00.00  
##  Median :2019-05-19 05:35:00.00   Median :2019-05-19 06:53:00.00  
##  Mean   :2019-06-17 01:49:22.55   Mean   :2019-06-17 03:13:21.03  
##  3rd Qu.:2021-02-15 21:14:00.00   3rd Qu.:2021-02-15 22:51:00.00  
##  Max.   :2022-12-31 23:55:00.00   Max.   :2023-01-01 00:00:00.00  
##                                                                   
##  Precipitation(in)     TimeZone         AirportCode         LocationLat   
##  Min.   :   0.0000   Length:8627181     Length:8627181     Min.   :24.56  
##  1st Qu.:   0.0000   Class :character   Class :character   1st Qu.:34.60  
##  Median :   0.0000   Mode  :character   Mode  :character   Median :39.35  
##  Mean   :   0.0929                                         Mean   :38.79  
##  3rd Qu.:   0.0500                                         3rd Qu.:43.06  
##  Max.   :1104.1300                                         Max.   :48.94  
##                                                                           
##   LocationLng          City              County             State          
##  Min.   :-124.56   Length:8627181     Length:8627181     Length:8627181    
##  1st Qu.: -97.82   Class :character   Class :character   Class :character  
##  Median : -89.77   Mode  :character   Mode  :character   Mode  :character  
##  Mean   : -91.91                                                           
##  3rd Qu.: -81.95                                                           
##  Max.   : -67.79                                                           
##                                                                            
##     ZipCode     
##  Min.   : 1022  
##  1st Qu.:31216  
##  Median :53913  
##  Mean   :52412  
##  3rd Qu.:73503  
##  Max.   :99362  
##  NA's   :69199
#converts character class state and county to factor - should do this with year to and look at over the years 
weather_data$State  <- as.factor(weather_data$State)
weather_data$County <- as.factor(weather_data$County)

#creates a new column with the year of the event's start 
weather_data <- weather_data %>% mutate(year = year(`StartTime(UTC)`))

#creates a dataset of the mean precipitation for each state
state_precip_mean <- weather_data %>% group_by(State) %>% summarise(precip_mean = mean(`Precipitation(in)`))

#creates a gg bar plot based on the average amount of precipitation for each state 
#would be really cool if could make a map of the us and the state colors the average amount of precipitation 
ggplot(state_precip_mean, aes(x = State, y = precip_mean)) + geom_col()

#add new coulmn for how long weather event lasted 
weather_data <- weather_data %>% mutate(event_duration = (`EndTime(UTC)`- `StartTime(UTC)`))

#new dataset for event type, severity and duration 
weather_events <- weather_data %>% dplyr::select(State, Type, Severity, event_duration, year)

#new dataset just including events in Ohio, relevant for the bar plots becuase the events of each state vary alot, this could be due to size of state(becuase texas covers more land than delaware they eill obviously have more events), i just chose Ohio to get a closer look at the distrabution of events and severity 
ohio_events <- weather_data %>% filter(State == "OH")
#ggplot bar plot of the distrabution of types of events for each state
ggplot(weather_events, aes(x = State)) + 
  geom_bar(width = .5, aes(fill = Type)) + 
  labs(title = "breakdown of type of events for each state", 
       subtitle = "bar chart", 
       caption = "source: weather_data") + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.6))

#ggplot bar plot of the distrabution of severity of events for each state
ggplot(weather_events, aes(x = State)) + 
  geom_bar(width = .5, aes(fill = Severity)) + 
  labs(title = "breakdown of severity of events for each state", 
       subtitle = "bar chart", 
       caption = "source: weather_data") + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.6))

#ggplot bar plot of the distrabution of severity of events for each county in Ohio
#did this becuase the amount of events per state varied a lot so this was a way to narrow down the data to better see the distrabution of severity 
ggplot(ohio_events, aes(x = County)) + 
  geom_bar(width = .5, aes(fill = Severity)) + 
  labs(title = "breakdown of severity of events for each county in Ohio", 
       subtitle = "bar chart", 
       caption = "source: weather_data") + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.6))

#have to look at dataset and figure out why these plots have so many severity options, try to lower it to light, moderate, heavy, severe. 
#dot plot similar to the one in the DataViz-ggplot2.Rmd in the code chunk at line 434 with the diamond dataset 

#first convert duration to a numeric 
weather_events <- weather_events %>% mutate(event_duration_numeric = as.numeric(event_duration))

#on this plot the x axis is the severity and the y axis is the duration of the event 
ggplot(weather_events %>% filter(Type %in% c("Rain","Snow")), aes(x = Severity, y = event_duration_numeric)) + geom_point() 

# ylab("event duration") 
#+ xlab("severity")
#should fix the y axis measuring- not have in scientific notation 
library(streamgraph)

summary(weather_events)
##      State             Type             Severity         event_duration   
##  TX     : 583234   Length:8627181     Length:8627181     Length:8627181   
##  MN     : 452099   Class :character   Class :character   Class :difftime  
##  MI     : 418795   Mode  :character   Mode  :character   Mode  :numeric   
##  FL     : 373421                                                          
##  NC     : 364605                                                          
##  CA     : 351660                                                          
##  (Other):6083367                                                          
##       year      event_duration_numeric
##  Min.   :2016   Min.   :       0      
##  1st Qu.:2017   1st Qu.:    1200      
##  Median :2019   Median :    2400      
##  Mean   :2019   Mean   :    5038      
##  3rd Qu.:2021   3rd Qu.:    4560      
##  Max.   :2022   Max.   :97951920      
## 
#this should make an interactive graph like the one in the DataViz-ggplot2.Rmd file in the code chunk at line 700 that used baby name data 
#this plot should show year along x axis and each state will have a horizontal line that will be proportional to the amount of events they had by year
weather_events %>%
  filter(grepl("^La", State)) %>%
  group_by(year, State) %>%
  tally %>%
  streamgraph("State", "n", "year")
## Warning in widget_html(name, package, id = x$id, style = css(width =
## validateCssUnit(sizeInfo$width), : streamgraph_html returned an object of class
## `list` instead of a `shiny.tag`.
## Warning: `bindFillRole()` only works on htmltools::tag() objects (e.g., div(),
## p(), etc.), not objects of type 'list'.
#this code ran successfully but the graph never showed up on my computer it just showed a blank gray square where the graph would be