#Line graphing

# Your data
years  <- c(2000, 2002, 2004, 2006, 2008, 2010)
values <- c(10, 9, 11, 14, 15, 20)

df <- data.frame(years, values)

# Base R
#We can just plot it
plot(years, values, type = "o", col = "blue",
     xlab = "Year", ylab = "Value",
     main = "Line Graph (Base R)")

#Alternately, we can build our plot in layers
# Step 1: create empty plot
plot(x = years, y = values,
     type = "n",               # "n" means no points or lines yet
     xlim = c(2000, 2010),     # set axis range
     ylim = c(0, 25),          # set y range
     xlab = "Year",
     ylab = "Value",
     main = "Base R Layered Plot")

# Step 2: add line
lines(years, values, col = "blue", lwd = 2)

# Step 3: add points
points(years, values, pch = 19, col = "red", cex = 1.5)

# Step 4: add text labels, if desired
text(years, values, labels = values, pos = 3, cex = 0.8)

#Lattice
#Lattice is a more opinionated library, based upon Cleveland (1993)
#Constrains some choices about visuals, and is less flexible than the other two
#But is more likely to result in nice default graphs

library(lattice)

#Standard usage in lattice:
# graph_type(formula, data =)
# Some types: 
# barchart, bwplot, cloud, contourplot	, densityplot, dotplot	
# histogram, levelplot, parallel, splom, stripplot, xyplot, wireframe

#Remember:
?lattice

xyplot(values ~ years, data = df,
       type = "o",
       xlab = "Year", ylab = "Value",
       main = "Line Graph (Lattice)")

# GGplot2 - the main tidyverse package
#(Yes, there was a ggplot, but it was a student project)

#GGplot is also a more opinionated sort of package
# This time, based around Leland Wilkinson's "Grammar of Graphics", coded by Wickham
# Grammar of graphics: all layered graphics can be described with seven things
# two most relevant: data, and then a system of mapping that data to geometry

#So first, we identify the data, and then, we identify the geometries:

library(ggplot2)

ggplot(df, aes(x = years, y = values)) +
  geom_line(color = "blue", linewidth = 1) +
  geom_point(color = "red", size = 3) +
  labs(title = "Line Graph (ggplot2)",
       x = "Year", y = "Value") +
  theme_minimal()