lu_table <- read_excel("/Users/maggiedoyle/Desktop/LU_table.xlsx") |>
  clean_names() |>
  select(!shape) |>
  select(!shape_length)

lu_transportation <- lu_table |>
  select(objectid, lu, descriptio, shape_area, transportation) |>
  filter(transportation == 1) |>
  group_by(transportation) |>                                                       
  summarise_at("shape_area", sum, na.rm = TRUE) |>                               
  ungroup() |>
  mutate(category = transportation)|>
  mutate(category = case_when(
    transportation == 1 ~ "Transportation",
    TRUE ~ "1")) |>
  select(-transportation)

lu_education <- lu_table |>
  select(objectid, lu, descriptio, shape_area, education) |>
  filter(education == 1) |>
  group_by(education) |>                                                       
  summarise_at("shape_area", sum, na.rm = TRUE) |>                               
  ungroup() |>
  mutate(category = education)|>
  mutate(category = case_when(
    education == 1 ~ "Education",
    TRUE ~ "1")) |>
  select(-education)

lu_parks_open_space <- lu_table |>
  select(objectid, lu, descriptio, shape_area, parks_open_space) |>
  filter(parks_open_space == 1) |>
  group_by(parks_open_space) |>                                                       
  summarise_at("shape_area", sum, na.rm = TRUE) |>                               
  ungroup() |>
  mutate(category = parks_open_space)|>
  mutate(category = case_when(
    parks_open_space == 1 ~ "Parks/Open Space",
    TRUE ~ "1")) |>
  select(-parks_open_space)

lu_military <- lu_table |>
  select(objectid, lu, descriptio, shape_area, military) |>
  filter(military == 1) |>
  group_by(military) |>                                                       
  summarise_at("shape_area", sum, na.rm = TRUE) |>                               
  ungroup() |>
  mutate(category = military)|>
  mutate(category = case_when(
    military == 1 ~ "Military",
    TRUE ~ "1")) |>
  select(-military)

lu_industrial <- lu_table |>
  select(objectid, lu, descriptio, shape_area, industrial) |>
  filter(industrial == 1) |>
  group_by(industrial) |>                                                       
  summarise_at("shape_area", sum, na.rm = TRUE) |>                               
  ungroup() |>
  mutate(category = industrial)|>
  mutate(category = case_when(
    industrial == 1 ~ "Industrial",
    TRUE ~ "1")) |>
  select(-industrial)

lu_beach_marina <- lu_table |>
  select(objectid, lu, descriptio, shape_area, beach_marina) |>
  filter(beach_marina == 1) |>
  group_by(beach_marina) |>                                                       
  summarise_at("shape_area", sum, na.rm = TRUE) |>                               
  ungroup() |>
  mutate(category = beach_marina)|>
  mutate(category = case_when(
    beach_marina == 1 ~ "Beach/Marina",
    TRUE ~ "1")) |>
  select(-beach_marina)

lu_public_use <- lu_table |>
  select(objectid, lu, descriptio, shape_area, public_use) |>
  filter(public_use == 1) |>
  group_by(public_use) |>                                                       
  summarise_at("shape_area", sum, na.rm = TRUE) |>                               
  ungroup() |>
  mutate(category = public_use)|>
  mutate(category = case_when(
    public_use == 1 ~ "Public Use",
    TRUE ~ "1")) |>
  select(-public_use)

lu_ag <- lu_table |>
  select(objectid, lu, descriptio, shape_area, ag) |>
  filter(ag == 1) |>
  group_by(ag) |>                                                       
  summarise_at("shape_area", sum, na.rm = TRUE) |>                               
  ungroup() |>
  mutate(category = ag)|>
  mutate(category = case_when(
    ag == 1 ~ "Agriculture",
    TRUE ~ "1")) |>
  select(-ag)

lu_airport <- lu_table |>
  select(objectid, lu, descriptio, shape_area, airport) |>
  filter(airport == 1) |>
  group_by(airport) |>                                                       
  summarise_at("shape_area", sum, na.rm = TRUE) |>                               
  ungroup() |>
  mutate(category = airport)|>
  mutate(category = case_when(
    airport == 1 ~ "Airports",
    TRUE ~ "1")) |>
  select(-airport)

lu_commerical <- lu_table |>
  select(objectid, lu, descriptio, shape_area, commercial) |>
  filter(commercial == 1) |>
  group_by(commercial) |>                                                       
  summarise_at("shape_area", sum, na.rm = TRUE) |>                               
  ungroup() |>
  mutate(category = commercial)|>
  mutate(category = case_when(
    commercial == 1 ~ "Commercial",
    TRUE ~ "1")) |>
  select(-commercial)

lu_gov <- lu_table |>
  select(objectid, lu, descriptio, shape_area, gov) |>
  filter(gov == 1) |>
  group_by(gov) |>                                                       
  summarise_at("shape_area", sum, na.rm = TRUE) |>                               
  ungroup() |>
  mutate(category = gov)|>
  mutate(category = case_when(
    gov == 1 ~ "Goverment",
    TRUE ~ "1")) |>
  select(-gov)
  
lu_health_care <- lu_table |>
  select(objectid, lu, descriptio, shape_area, health_care) |>
  filter(health_care == 1) |>
  group_by(health_care) |>                                                       
  summarise_at("shape_area", sum, na.rm = TRUE) |>                               
  ungroup() |>
  mutate(category = health_care)|>
  mutate(category = case_when(
    health_care == 1 ~ "Health Care",
    TRUE ~ "1")) |>
  select(-health_care)

lu_housing <- lu_table |>
  select(objectid, lu, descriptio, shape_area, housing) |>
  filter(housing == 1) |>
  group_by(housing) |>                                                       
  summarise_at("shape_area", sum, na.rm = TRUE) |>                               
  ungroup() |>
  mutate(category = housing)|>
  mutate(category = case_when(
    housing == 1 ~ "Residential",
    TRUE ~ "1")) |>
  select(-housing)
area_data <- bind_rows(
  lu_transportation,
  lu_education,
  lu_parks_open_space,
  lu_military,
  lu_industrial,
  lu_beach_marina,
  lu_public_use,
  lu_ag,
  lu_airport,
  lu_commerical,
  lu_gov,
  lu_health_care,
  lu_housing
) 


area_data <- area_data |>
  mutate(shape_area = shape_area / 1000000 ) #convert to km 

area_top5 <- area_data |>
  arrange(desc(shape_area)) |>
  slice_head(n = 5) 
ggplot(data = area_data) +
  
  geom_col(aes(x = category, y = shape_area, fill = category),
           position = position_dodge(width = 0.8)) +
 
   labs(x = "Land Use Categories", y = "Area Taken over by Sea Level Rise (km^2)", fill = NULL) +
  
  theme_classic() +
  
  scale_fill_manual(values = c("Agriculture" = "#D3FFBE",            
                               "Airports" = "#C5C5B9", 
                               "Beach/Marina" = "#FFEABE",             
                               "Commercial" = "#FFA77F", 
                               "Education" = "#E7D8F0", 
                               "Goverment" = "#CDAA66", 
                               "Health Care" = "#FBC4BB", 
                               "Industrial" = "#636363", 
                               "Military" = "#E3E0C4",
                               "Parks/Open Space" = "#C6E2B2",
                               "Residential" = "#FF73DF",
                               "Transportation" = "#999999", 
                               "Public Use" = "#FF7F7F")) +
  theme(
        axis.text.x = element_text(angle = 45, hjust = 1, color = "black"),
        axis.title.x = element_text(color = "black", size = 12, face = "bold"),                
        axis.text.y = element_text(color = "black"),
        axis.title.y = element_text(color = "black", 
                                         size = 10, face = "bold"),
        axis.text = element_text(color = "black", size = 10),
        plot.caption = element_text(hjust = -0.15, color = "black", 
                                    face = "bold", size = 14),               
        legend.title = element_text(size = 10, color = "black"),             
        legend.background = element_blank(),
        legend.box.background = element_rect(color = "black",
                                             size = 0.2)) +
  
scale_y_continuous(labels = scales::comma, limits = c(0, 280), 
                     breaks = scales::pretty_breaks(n = 6), 
                     expand = c(0,0))      
## Warning: The `size` argument of `element_rect()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

ggplot() +
  geom_col(data = area_top5, aes(x = category, y = shape_area, fill = category),
           position = position_dodge(width = 4.5)) +
  
  scale_fill_manual(values = c("Beach/Marina" = "#FFEABE",
                                "Military" = "#E3E0C4", 
                                "Parks/Open Space" = "#C6E2B2",
                                "Residential" = "#FF73DF",
                                "Transportation" = "#999999")) + 
 
   labs(x = "Top 5 Land Use Categories", 
       y = "Area Taken over by Sea Level Rise (km^2)", 
       fill = NULL) +
  
  theme_minimal() +
  coord_flip() +
  
  scale_y_continuous(labels = scales::comma, limits = c(0, 280), 
                     breaks = scales::pretty_breaks(n = 6), 
                     expand = c(0,0))