library(tidyverse)
library(readr)
library(patchwork)
library(ggthemes)
library(maps)
library(mapproj)
library(usdata)
library(plotly)
library(leaflet)
library(DT)

Code from Project 2:

cities <- read_csv("forecast_cities.csv")
weather <- read_csv("weather_forecasts.csv")
outlook <- read_csv("outlook_meanings.csv")
states <- map_data("state")

 forecast <- cities %>%
   left_join(weather, by = c("city", "state")) 
forecast <- forecast %>%
 mutate(
   state = abbr2state(state),
    state = tolower(state),
     dif = observed_temp - forecast_temp
   ) 

Making the map interactive:

plot1 <- forecast %>%
  group_by(state) %>%
  summarize(
    mean_dif = mean(dif, na.rm = TRUE)
  ) %>%
  ggplot(aes(map_id = state, fill = mean_dif, text = state)) +
  geom_map(map = states) +
  scale_fill_gradient2(low = "darkgreen", high = "blue", mid = "white", 
                       midpoint = 0, 
                       name = "Difference in Temperature") +
  expand_limits(x = states$long, y = states$lat) +
  coord_map() +
  theme_map() +
  labs(
    title = "Discrepencies in Temperature Prediction in the US"
  ) +
  theme(legend.position = "right")

ggplotly(plot1, tooltip = c("fill", "text"))
worst_states <- c("oregon", "montana", "nevada", "alaska", "massachusetts", 
                  "hawaii")
new_forecast <- forecast %>%
  mutate(
    forecast_outlook = tolower(forecast_outlook),
    city = tolower(city)
  ) %>%
  filter(state %in% worst_states) %>%
  filter(!is.na(dif)) %>%
  select(city, state, elevation, wind, distance_to_coast, avg_annual_precip, 
         forecast_outlook, dif)

datatable(new_forecast,
          rownames = FALSE,
          filter = "top")  %>%
  formatStyle(columns = colnames(new_forecast), fontSize = '12pt')
forecast %>%
  group_by(state) %>%
  summarize(
    mean_dif = mean(dif, na.rm = TRUE)
  ) %>%
  slice_max(mean_dif, n=6)
## # A tibble: 6 × 2
##   state         mean_dif
##   <chr>            <dbl>
## 1 alaska           1.36 
## 2 massachusetts    1.27 
## 3 oregon           1.14 
## 4 hawaii           1.13 
## 5 montana          1.09 
## 6 nevada           0.992
forecast %>%
  filter(!is.na(high_or_low)) %>%
ggplot(aes(x = high_or_low, y = dif)) + geom_boxplot() +
  labs(title = "Weather prediction by high or low temperatures",
     x = "High or Low",
     y = "Difference")

ggplot(forecast, aes(x = avg_annual_precip, y = dif)) + 
  geom_point(alpha = 0.5, color = "darkmagenta") +
  labs(title = "Precipitation vs. Difference",
     x = "Average annual precipitation",
     y = "Difference") 

worst_states <- c("oregon", "montana", "nevada", "alaska", "massachusetts", 
                  "hawaii")
new_forecast <- forecast %>%
  filter(state %in% worst_states)

ggplot(new_forecast, aes(x = avg_annual_precip, y = dif)) + 
  geom_point(alpha = 0.5, color = "darkmagenta") + geom_smooth() +
  labs(title = "Precipitation vs. Difference for states with highest difference",
     x = "Average annual precipitation",
     y = "Difference") 

ggplot(new_forecast, aes(x = avg_annual_precip, y = dif, fill = avg_annual_precip)) + 
  geom_bar(stat = "summary", width = 10, alpha = 0.5)  +
  scale_fill_viridis_b() +
  labs(title = "Precipitation bar chart for states with highest difference",
       x = "Average annual precipitation",
       y = "Difference",
       fill = "Average annual precipitation")

ggplot(new_forecast, aes(x = state, y = forecast_outlook, fill = avg_annual_precip)) +
  geom_tile() + 
  scale_fill_gradient(low = "white", high = "darkmagenta") +
  labs(
    title = "Heat map of temperature predictions",
    x = "State",
    y = "Forecast Outlook",
    fill = "Avg. Annual Precip."
  )