Exercises 4

dta <- read.table('C:/Users/user/Desktop/sat_gpa.txt', header = TRUE)
head(dta)
library(tidyverse)
## -- Attaching packages -------------------------------------- tidyverse 1.3.0 --
## √ ggplot2 3.3.0     √ purrr   0.3.3
## √ tibble  2.1.3     √ dplyr   0.8.5
## √ tidyr   1.0.2     √ stringr 1.4.0
## √ readr   1.3.1     √ forcats 0.5.0
## -- Conflicts ----------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(dplyr)
dta_yes <- dta %>% select(College, contains("Yes")) %>% mutate(sumbit="Yes")%>% rename(SAT=SAT_Yes, GPA=GPA_Yes)
dta_no <- dta %>% select(College, contains("No")) %>% mutate(sumbit="No")%>% rename(SAT=SAT_No, GPA=GPA_No)

dta1 <- merge(dta_yes,dta_no, all=TRUE)

plot.new()
par(oma=c(0,0,0,0))
par(mai=c(1, 1, 0, 1))

with(dta1, plot(GPA ~ SAT, type='n', xlim = c(1150, 1400),
     ylim = c(2.6, 3.4), xlab = "SAT(V+M)", ylab = "First Year GPA"))
with(dta_yes, points(GPA ~ SAT, pch = 1, cex = 2))
with(dta_no, points(GPA ~ SAT, pch = 19, cex = 2))
with(dta_yes, text(SAT, GPA, labels=College, cex=1,adj = c(-.25,-.1)))
with(dta[c(1:2,4:6),], 
     segments(x0=SAT_No,y0=GPA_No, x1=SAT_Yes, y1=GPA_Yes,
              lty=1, lwd=1, 
              col="black"))
with(dta[c(3),], 
     segments(x0=SAT_No,y0=GPA_No, x1=SAT_Yes, y1=GPA_Yes,
              lty=1, lwd=2, 
              col="black"))
legend("topleft", c("Submitted SAT Scores", "Did NOT Submit SAT Scores"), pch = c(1, 19), bty = "n")