coo <- kah %>% 
  group_by(disposition, zone) %>% 
  filter(disposition == "COO") %>% 
  add_count(disposition) %>% 
  mutate(avgCOO = mean(n))

coo <- coo %>% 
  group_by(survey_month, zone) %>% 
  mutate(sqm = n_distinct(site_id)*10,
         coosqm= n() / sqm
  ) %>% 
  ungroup()

levels(coo$zone) <- c("Control Zone", "Restoration Zone")
  
COO_year = coo %>% 
  ungroup() %>% 
  count(survey_month, zone) %>% 
  mutate(survey_month = factor(month.abb[survey_month], levels = month.abb)) %>% 
  ggplot(aes(x = survey_month, y = n, fill = zone)) +
  geom_col(position = "dodge") +
  scale_fill_manual(values = zoneCols) +
  labs(x = "Month", y = "COO Count", fill = "Zone")
coosqmPlot = coo %>% 
  ungroup() %>% 
  mutate(survey_month = factor(month.abb[survey_month], levels = month.abb)) %>% #makes the survey month turn into words on the plot
  ggplot(aes(x = survey_month, y = coosqm, fill = zone)) + #these are the values the plot will use
  geom_col(position = "dodge") + #makes the plot a bar graph
  geom_text(aes(label = round(coosqm, 2)), 
            position = position_dodge(width = 0.9),
            vjust = -0.5, size = 3) + # this adds the numbers above each of the bars
  scale_fill_manual(values = zoneCols) + #colors the bars with our colors in the setup chunk
  labs(x = "Month", y = "# of COO per square meter", fill = "Zone") + #gives the bar graph labels
  scale_y_continuous(limits = c(-0.01, 1.2),
                     breaks = seq(0, 1.2, by = 0.25)) #changes the bounds of the graph
coo_z2 <- coo %>% 
  filter(zone == "Control Zone")

coo_z5 <- coo %>% 
  filter(zone == "Restoration Zone")

spCOO_z2 = coo_z2 %>% 
  ungroup() %>% 
  count(survey_month, live_tissue_code) %>% 
  mutate(survey_month = factor(month.abb[survey_month], levels = month.abb))  %>% 
  ggplot(aes(x = survey_month, y = n, color = live_tissue_code)) +
  geom_point(size = 4) +
  scale_color_manual(values = healthCols, labels = healthLabs) + 
  labs(x = "Month", y = "# of COO", color = "Live Tissue Code")

spCOO_z5 = coo_z5 %>% 
  ungroup() %>% 
  count(survey_month, live_tissue_code) %>% 
  mutate(survey_month = factor(month.abb[survey_month], levels = month.abb))  %>% 
  ggplot(aes(x = survey_month, y = n, color = live_tissue_code)) +
  geom_point(size = 4) +
  scale_color_manual(values = healthCols, labels = healthLabs) + 
  labs(x = "Month", y = "# of COO", color = "Live Tissue Code")
kah_clean <- kah %>% 
  drop_na(health, size_cm)

health <- kah_clean %>% 
  mutate(health = factor(health, levels = 5:0)) %>% 
  ggplot(aes(x = health, y = size_cm, fill = health)) +
  geom_boxplot() +
  labs(x = "Health Score", y = "Size (cm)") + 
  scale_y_continuous(limits = c(-5, 150),
                     breaks = seq(0, 150, by = 10))

kruskal.test(size_cm ~ factor(health), data = kah_clean) #with a p-value that is lower than 0.05, this tells us that size significantly differs across the amount of health problems
## 
##  Kruskal-Wallis rank sum test
## 
## data:  size_cm by factor(health)
## Kruskal-Wallis chi-squared = 584.17, df = 5, p-value < 2.2e-16
kah_clean %>%
  dunn_test(size_cm ~ health, p.adjust.method = "bonferroni") #this tells us that generally the smaller the coral, the healthier it is. And as the corals get larger in size, they develop more health problems.
## # A tibble: 15 × 9
##    .y.     group1 group2    n1    n2 statistic        p    p.adj p.adj.signif
##  * <chr>   <chr>  <chr>  <int> <int>     <dbl>    <dbl>    <dbl> <chr>       
##  1 size_cm 0      1       2583  1860   13.4    6.49e-41 9.73e-40 ****        
##  2 size_cm 0      2       2583   978   20.9    5.95e-97 8.93e-96 ****        
##  3 size_cm 0      3       2583   264   13.3    1.20e-40 1.79e-39 ****        
##  4 size_cm 0      4       2583    50    6.32   2.68e-10 4.02e- 9 ****        
##  5 size_cm 0      5       2583     1    0.0572 9.54e- 1 1   e+ 0 ns          
##  6 size_cm 1      2       1860   978    9.55   1.31e-21 1.97e-20 ****        
##  7 size_cm 1      3       1860   264    6.92   4.47e-12 6.70e-11 ****        
##  8 size_cm 1      4       1860    50    3.45   5.59e- 4 8.39e- 3 **          
##  9 size_cm 1      5       1860     1   -0.350  7.26e- 1 1   e+ 0 ns          
## 10 size_cm 2      3        978   264    1.13   2.60e- 1 1   e+ 0 ns          
## 11 size_cm 2      4        978    50    0.809  4.18e- 1 1   e+ 0 ns          
## 12 size_cm 2      5        978     1   -0.727  4.67e- 1 1   e+ 0 ns          
## 13 size_cm 3      4        264    50    0.255  7.99e- 1 1   e+ 0 ns          
## 14 size_cm 3      5        264     1   -0.804  4.21e- 1 1   e+ 0 ns          
## 15 size_cm 4      5         50     1   -0.836  4.03e- 1 1   e+ 0 ns
kah %>% 
  drop_na(outplantable_substrate) %>% 
  leaflet() %>% 
  addProviderTiles(providers$Esri.WorldImagery) %>% 
  addPolygons(
    lng = c(-155.96863, -155.96794, -155.9681, -155.96871),  
    lat = c(19.58052, 19.58043, 19.57979, 19.57986 ), 
    color = "#B084CC", fill = FALSE, weight = 4,
    label = "Control Zone"
  ) %>% 
  addPolygons(
    lng = c(-155.96871, -155.96893, -155.96829, -155.9681),  
    lat = c(19.57986, 19.57923, 19.57913, 19.57979),       
    color = "#190933", fill = FALSE, weight = 4,
    label = "Restoration Zone"
  ) %>% 
  addHeatmap(
    lng = ~long, lat = ~lat,
    intensity = ~outplantable_substrate,
    blur = 25, max = 95, radius = 15
  )
pal <- colorNumeric("YlOrRd", domain = kah$outplantable_substrate, na.color = "transparent")

kah %>% 
  drop_na(outplantable_substrate) %>% 
  leaflet() %>% 
  addProviderTiles(providers$Esri.WorldImagery) %>% 
  addCircleMarkers(
    lng = ~long, lat = ~lat,
    color = ~pal(outplantable_substrate),
    radius = 5, stroke = FALSE, fillOpacity = 0.8,
    popup = ~paste("Coordinates:", lat, long, "<br>",
                   "Outplantable Substrate:", outplantable_substrate, "%")
   ) %>% 
  addLegend(pal = pal, values = ~outplantable_substrate,
            title = "Outplantable<br>Substrate (%)")