###################################################################
############# Module 5, Basics of data visualization ##############
###################################################################
### EXCERCISES (Lessons 8 and 9)

# 1) Upload the data "island_soil_data_raw.csv" located in the "data" folder and explore the data a bit:
soil_data <- read.csv("data/island_soil_data_raw.csv")

head(soil_data)
nrow(soil_data)
soil_data

# 2) Make a simple scatterplot of of the relationship between Clay_perc and Sand_perc:
plot(Clay_perc ~ Sand_perc, data=soil_data)

# 3) Now clean up this plot to make it look nice by adjusting the axis names, making the points larger
# and filled in, and any other adjustments you'd like to make to increase the visual appeal and
# presentability of this figure:
plot(Clay_perc ~ Sand_perc, data=soil_data, xlab="Percent Sand", ylab="Percent Clay", pch=16, cex=1.5)
# Since they are both the same type of unit, I decided to make scale both axes with the same aspect ratio:
plot(Clay_perc ~ Sand_perc, data=soil_data, xlab="Percent Sand", ylab="Percent Clay", pch=16, cex=1.5, asp=1)

# 4) Upload the data "island_spatial_data_raw.csv" located in the "data" folder and explore the data a bit.
# View histograms of the the continuous variables in this new dataframe.
spatial_data <- read.csv("data/island_spatial_data_raw.csv")

head(spatial_data)
nrow(spatial_data)
str(spatial_data)

hist(spatial_data$AREA_HA)
hist(spatial_data$DIST_M)

# 5) Now add the two new columns of spatial data to our other dataframe of soil data.
# Hint, make sure that both dataframes are ordered by island to ensure that 
# the new columns you add are in the right order.
soil_data <- soil_data[order(soil_data$island),]
spatial_data <- spatial_data[order(spatial_data$ISLAND),]

island_data <- soil_data
island_data$area_ha <- spatial_data$AREA_HA
island_data$distance_m <- spatial_data$DIST_M

island_data

# 6) View a histogram of island areas to see if there is a good cutoff 
# between larger and smaller islands:
hist(island_data$area_ha)

# 7) It's difficult to tell from the histogram and since we only have
# 20 islands, just sort and then plot island area as a function of 1 to show them all
plot(sort(area_ha) ~ 1, data=island_data)

# 8) Do the same for island distance (isolation from the mainland shorline)
plot(sort(distance_m) ~ 1, data=island_data)

# 9) Add a new column indicating large islands as all with an area greater than 0.65 ha,
# and a new column indicating far islands as all with a distance greater than 350 m

island_data$big_island <- island_data$area_ha > 0.65
island_data$far_island <- island_data$distance_m > 350
island_data

# 10) Does there appear to be a difference in soil Clay percent between small and large islands?
# Use everything you know so far to visualize these questions and create presentable figures
# using both scatter and boxplots:

boxplot(Clay_perc ~ big_island, data=island_data)
plot(Clay_perc ~ area_ha, data=island_data)

boxplot(Clay_perc ~ big_island, data=island_data, xaxt="n",
        ylab="", xlab="")
title(main = list("Island Soil Texture Boxplot", cex = 1.5,
                  col = "black", font = 2),
      ylab = list("Clay %", cex=1.3))
axis(side = 1, at=c(1:2), labels = c("Small islands","Large islands"), cex.axis=1.2)

plot(Clay_perc ~ area_ha, data=island_data,
        ylab="", xlab="", cex=1.4, pch=16)
title(main = list("Island Soil Texture Scatter", cex = 1.5,
                  col = "black", font = 2),
      ylab = list("Clay %", cex=1.3),
      xlab = list("Island area (ha)", cex=1.3))

# 11) Now save these plots.
pdf("clay_boxplot.pdf", width=4.5, height=4)

boxplot(Clay_perc ~ big_island, data=island_data, xaxt="n",
        ylab="", xlab="")
title(main = list("Island Soil Texture Boxplot", cex = 1.5,
                  col = "black", font = 2),
      ylab = list("Clay %", cex=1.3))
axis(side = 1, at=c(1:2), labels = c("Small islands","Large islands"), cex.axis=1.1)

dev.off()

pdf("clay_scatter.pdf", width=4.5, height=4)

plot(Clay_perc ~ area_ha, data=island_data,
     ylab="", xlab="", cex=1.4, pch=16)
title(main = list("Island Soil Texture Scatter", cex = 1.5,
                  col = "black", font = 2),
      ylab = list("Clay %", cex=1.3),
      xlab = list("Island area (ha)", cex=1.3))

dev.off()

# That's it!

