#Title: DWV Week 3.R
#Author: Jack Reilly
#Purpose: draw standard and regression lines. Learn to play with parameters of graphics.
#Requires: nes2000subset.csv
#Output: Figures

#Technical note: The NES subset deletes all individuals with missing data ("listwise deletion") leaving just over 1100 individuals in the sample.  It also deletes all individuals who voted for a third party

#Two concepts to visualization for the day:
#Visualization is a series of graphical *layers*
#This means you have to sequentially build your plot.
#All visualization deals with manipulating fundamental elements.
#Today, we'll talk about two elements: points and lines.

#Let's draw a line
#First, draw the plot area

#We'll start by resetting graphics devices, just in case anything is lingering around
graphics.off()

plot(x=0, type='n', xlim=c(-10,10), ylim=c(-10,10),
  xlab="X", ylab="Y", main="Drawing Some Lines")

#Next, the x and y axes, in gray
abline(h=0, v=0, col = "gray")

#The line formula in R is y=bx+a.  Let's create the line 3x+1 in blue
abline(a=1, b=3, col = "plum")

#What other colors can we use?  This displays all possible colors in R
colors()

#Let's redraw that line in "steelblue", increasing the line width "lwd" in the process
abline(a=1, b=3, col = "hotpink", lwd=90)

#Add text to line to label it, keeping it the same color as the original.  
#This command places text starting at the graph coordinates (x,y) - in this case, at (5,8)
text(5,8, "y=3x+1", col = "hotpink")

#We can add another line and label it, too
abline(a=-3, b=-.5, col = "firebrick3", lwd=3)

#How about making it dashed?
#And making our code a little easier to see?
plot(x=0, 
     type='n', 
     xlim=c(-10,10), 
     ylim=c(-10,10),
     xlab="This is the X axis", 
     ylab="This is the Y axis", 
     main="Drawing Some Lines")

abline(a=-3, 
       b=-.5, 
       col = "firebrick3", 
       lwd=3, 
       lty = "dashed")

text(-5,-2, "y=-.5x-3", col = "firebrick3")

#What if we want to save our plot?

#Two ways:
#We have to build it

#set your working directory
setwd("~/desktop/working/dwv/week 4")

#start your base plot
#Note, as soon as you start your base plot, it will appear in your filesystem!
#There just won't be anything in it.
graphics.off()
pdf("myplot1.pdf", width = 5, height = 4)

#let's add our plot
plot(x=0, 
     type='n', 
     xlim=c(-10,10), 
     ylim=c(-10,10),
     xlab="This is the X axis", 
     ylab="This is the Y axis", 
     main="Drawing Some Lines")

abline(a=-3, 
       b=-.5, 
       col = "firebrick3", 
       lwd=3, 
       lty = "dashed")

text(-5,-2, "y=-.5x-3", col = "firebrick3")

#once we finish our build, we close the graphics device, which actually writes it to the file
dev.off()

#alternately, we can just copy what's in the current graphical device
graphics.off()
plot(x=0, 
     type='n', 
     xlim=c(-10,10), 
     ylim=c(-10,10),
     xlab="This is the X axis", 
     ylab="This is the Y axis", 
     main="Drawing Some Lines")

abline(a=-3, 
       b=-.5, 
       col = "forestgreen", 
       lwd=3, 
       lty = "dashed")

text(-5,-2, "y=-.5x-3", col = "forestgreen")

dev.copy(png, "myplot2.png", width = 800, height = 600)
dev.off()


#Now, we'll do a bivariate linear model

nes<-read.csv("~/Library/CloudStorage/OneDrive-SyracuseUniversity/Shared Folders/Courses/Course Folders/PAI 600 - Fall 2025/3. Data/nes2000subset.csv")
#nes<-read.csv(file.choose())

#Model partisanship as a function of income
model <- lm(nes$partisanship~nes$income)
summary(model)

#Draw the resulting regression line, starting with the plot
plot(x=0, type='n', xlim=c(-10,10), ylim=c(-10,10), xlab="x", ylab="y", main="Drawing Some Lines")
abline(model)

#Ugly!  We can do better.
#Change the x and y axes to include just relevant variables, label the axes properly, and give it a good title

plot(x=0, type='n', xlim=c(-1,23), ylim=c(-.4,6.4), xlab="Income", ylab="Partisanship", main="Income is Associated with Partisanship")

#Insert axes at 0
abline(h=0, v=0, col = "gray")

#Now, draw the line, making it a little heavier to make it easier to see
abline(model, col="violetred", lwd=5)

#But!  We can still do better.  What if we want to have the points as well as the line?
#We can *layer* everything together
graphics.off()

plot(x=0, type='n', xlim=c(-10,10), ylim=c(-10,10), xlab="x", ylab="y", main="Drawing Some Lines")
abline(model)

#Ugly!  We can do better.
#Change the x and y axes to include just relevant variables, label the axes properly, and give it a good title

plot(x=0, type='n', xlim=c(-1,23), ylim=c(-.4,6.4), xlab="Income", ylab="Partisanship", main="Income is Associated with Partisanship")

#Insert axes at 0
abline(h=0, v=0, col = "gray")

#Now, we'll draw some points
points(y=nes$partisanship, x=nes$income)

#Oof.  Ugly.  Let's not forget to jitter ordinal data
points(y=jitter(nes$partisanship), x=jitter(nes$income))

#Better! Now add our line, and this time, let's tweak it a little to make itm more visible
abline(model, col="red", lwd=3)