###################################################################
############# Module 5, Basics of data visualization ##############
###################################################################
## Lesson 9, Part 2: Customizing and saving figures (text, margins, and saving figures):

# First load back the modified turtle data and one of the basic plots we created:
turtle_mod <- read.csv("data/turtle_nesting_data_mod.csv")
boxplot(prop_surv ~ (year), data=turtle_mod,
        col="skyblue", ylab="", xlab="", horizontal=F)
title(main = list("Turtle Egg Survival", cex = 1.5,
                  col = "black", font = 2),
      ylab = list("Proportion of Eggs Surviving", cex=1.3),
      xlab = list("Year", cex=1.3))

###################################
##### TEXT AND MARGINS
text(x = 10.5, y=0.05, "P = 0.0023", cex=1.1, font=2)

mtext("text in the margins", side=1, line=3, cex=1.1, adj=0, col="red")

par(mar = c(4,4,2,2))
boxplot(prop_surv ~ (year), data=turtle_mod,
        col="skyblue", ylab="", xlab="", horizontal=F,
        xaxt="n")
title(main = list("Turtle Egg Survival", cex = 1.5,
                  col = "black", font = 2),
      ylab = list("Proportion of Eggs Surviving", cex=1.3),
      xlab = list("Year", cex=1.3))

axis(side=1, at=c(1:length(unique(turtle_mod$year))), labels = sort(unique(turtle_mod$year)),
     las=2)


###################################
##### SAVING PLOTS

# exporting:
# https://nicercode.github.io/guides/plotting/

pdf("my_plot.pdf", width = 6, height = 4)

par(mar = c(4,4,2,2))
boxplot(prop_surv ~ (year), data=turtle_mod,
        col="skyblue", ylab="", xlab="", horizontal=F,
        xaxt="n")
title(main = list("Turtle Egg Survival", cex = 1.5,
                  col = "black", font = 2),
      ylab = list("Proportion of Eggs Surviving", cex=1.3),
      xlab = list("Year", cex=1.3))

axis(side=1, at=c(1:length(unique(turtle_mod$year))), labels = sort(unique(turtle_mod$year)),
     las=2)

dev.off()

quartz(w=5, h=4) # for macs
windows(w=5, h=4) # for windows OS


###################################
##### SUMMARY
# - adding text and more control over labels
# - adjusting margins
# - saving plots

