ggplot2
, which is also in the tidyverse package.ggplot2
code for plots will be available in the slides. Just search for ‘ggplot’ggplot2
density()
object out of it, then plot()
that thing!Ecdat
packageinstall.packages('Ecdat')
library(Ecdat)
data(MCAS)
plot(density(MCAS$totsc4))
plot(density(MCAS$totsc4),main='Massachusetts Test Scores',
xlab='Fourth Grade Test Scores')
hist()
hist(MCAS$totsc4)
freq=FALSE
, or change how many bins there are, or where they are, with breaks
hist(MCAS$totsc4,xlab="Fourth Grade Test Scores",
main="Test Score Histogram",freq=FALSE,breaks=50)
boxplot(MCAS$totsc4,main="Box Plot of 4th Grade Scores")
boxplot(select(MCAS,totsc4,totsc8),main="Box Plot of 4th Grade Scores")
table()
command, which shows us the whole distribution of a categorical variableEcdat
packagedata(Mathlevel)
table(Mathlevel$major)
##
## other eco oss ns hum
## 130 209 103 126 41
barplot(table(Mathlevel$major),main="Majors of Students in Advanced Microeconomics Class")
table()
in prop.table()
barplot(prop.table(table(Mathlevel$major)),main="Majors of Students in Advanced Microeconomics Class")
abline()
which adds a line with a given intercept (a) and slope (b), after we make our plot.abline(intercept,slope)
, or abline(h=horizontal)
, abline(v=vertical)
for horizontal or vertical numbershist(MCAS$totsc4)
passingscore <- 705
abline(0,1)
abline(v=passingscore,col='red')
abline(h=50)
hist(MCAS$totsc4)
passingscore <- 705
abline(v=passingscore,col='red')
plot(density(MCAS$totsc4))
abline(v=mean(MCAS$totsc4),col='red')
abline(v=median(MCAS$totsc4),col='blue')
lines()
function will, like abline()
, add a line to your graphplot(density(filter(MCAS,tchratio < median(tchratio))$totsc4),
col='red',main="Mass. Fourth-Grade Test Scores by Teacher Ratio")
lines(density(filter(MCAS,tchratio >= median(tchratio))$totsc4),col='blue')
Ecdata
, load it, and get the MCAS
datatotsc4
, and add a vertical green dashed (tough!) line at the medianbilingua
install.packages('Ecdata')
library(Ecdata)
data(MCAS)
plot(density(MCAS$totsc4),xlab="4th Grade Test Score")
abline(v=median(MCAS$totsc4),col='green',lty='dashed')
MCAS <- MCAS %>% mutate(nonzerobi = bilingua > 0)
barplot(prop.table(table(MCAS$nonzerobi)),main="Nonzero Spending Per Bilingual Student")
boxplot(MCAS$totday,main="Spending Per Pupil")
abline(h=mean(MCAS$totday))