###################################################################
############# Module 5, Basics of data visualization ##############
###################################################################
## Lesson 9, Part 1: Customizing and saving figures (the basics):


###################################
##### UPLOAD DATA

# Load the turtle data again:
turtle_data <- read.csv("data/turtle_nesting_data_clean.csv")

turtle_mod <- turtle_data

turtle_mod$prop_surv <- turtle_data$live_hatchlings / turtle_data$clutch_size

table(turtle_mod$prop_surv)

is.na(turtle_mod$prop_surv)

turtle_mod <- turtle_mod[!is.na(turtle_mod$prop_surv), ]

turtle_mod <- turtle_mod[turtle_mod$prop_surv > 0,]

hist(turtle_mod$prop_surv)

turtle_mod <- turtle_mod[turtle_mod$prop_surv <= 1,]

hist(turtle_mod$prop_surv)

plot(prop_surv ~ nest_date, data=turtle_mod)

###################################
##### CUSTOMIZE PLOTS

plot(prop_surv ~ nest_date, data=turtle_mod,
     ylab = "",
     xlab = "",
     ylim = c(0,1),
     cex = 1.5,
     pch = 16,
     col = (year==2002) + 3)
title(main = list("Turtle Egg Survival", cex = 2, font = 2),
      ylab = list("Proportion of Eggs Surviving", cex=1.3),
      xlab = list("Nesting Date (Julian day)", cex=1.3))

legend(x="bottomright", legend=c("Year 2002", "Other years"),
       pch=16, col=c(4,3), pt.cex=1.5, text.font=2)

boxplot(prop_surv ~ year, data=turtle_mod,
        col="skyblue",
        ylab="",
        xlab="",
        horizontal=T)
title(main = list("Turtle Egg Survival", cex = 1.5, font = 2),
      ylab = list("Proportion of Eggs Surviving", cex=1.3),
      xlab = list("Year", cex=1.3))

?boxplot()



###################################
##### SUMMARY
# - more practice working with dataframes
# - aesthetic customizations to plots

