###################################################################
############# Module 5, Basics of data visualization ##############
###################################################################
## Lesson 8, Part 2: Introduction to data visualization (jitters and lines):


###################################
##### JITTERS AND LINES
# These next data are based on surveying painted turtle nests 
# with daily observations from 1989 to 2006 and based on Schwanz et a. 2009.
# # http://esapubs.org/archive/ecol/E090/119/metadata.htm
# I also cleaned these up for the sake of saving some time
turtle_data <- read.csv("data/turtle_nesting_data_clean.csv")

head(turtle_data)

hist(turtle_data$veg_cover)

table(turtle_data$nest_predation)

names(turtle_data)

plot(jitter(nest_predation) ~ veg_cover, data=turtle_data, xlim=c(70,100))


plot(jitter(nest_survival) ~ jitter(year), data=turtle_data)

year <- turtle_data$year

nest_survival <- turtle_data$nest_survival

prop_surviving <- tapply(X = nest_survival, FUN = function(x) mean(x, na.rm=T), INDEX = year)

year_unique <- as.numeric(names(prop_surviving))

surv_data <- data.frame(years = year_unique, prop_surviving = prop_surviving)

plot(jitter(nest_survival) ~ jitter(year), data=turtle_data)
points(prop_surviving ~ years, data=surv_data, type="l", col="red")

###################################
##### SUMMARY
# To summarize we:
# - jitter hidden points
# - tapply function to apply a function within groups of your data
# - create lineplots
# - the points() function to add points or lines to an existing plot



