---
title: "Figures"
author: "Tera Letzring"
date: "September 2017"
output: html_document
---

```{r setup, include=FALSE}

knitr::opts_chunk$set(echo = TRUE)
library(ggplot2)
```

```{r set wd and read in data}

#set the working directory (wd) to the folder that contains the script file, and read in the data
x <- getwd()
setwd(x)

SDdata = read.table("schooldays.csv", header=T, sep=",")  
head(SDdata)
```

```{r frequency distribution}

#to get a list of colors
colors()

attach(SDdata)

#Barplot for one variable
barplot(table(absent))   

###Barplot for two variables with axis labels and a legend

#Create the graph with a legend. Look at frequency table to make sure the legend is correct
barplot (table(gender, race), beside = TRUE, xlab = "Race", ylab = "Frequency", col = c("gray90", "gray60"))
legend ("topright",   legend = c("Female", "Male"), fill = c("gray90", "gray60"))  #this has to be run in combination with the previous command to create the barplot
```

```{r barplots with ggplot2 package}

#create frequency bar graph: variable after aes will be on x-axis, + theme_bw() changes the background to white
ggplot(SDdata, aes(school)) +
    geom_bar() + theme_bw()

#create bar graph paneled by row using ~. after paneling variable name: variable after facet_grid is the panel-by variable
ggplot(SDdata, aes(school)) +
   geom_bar() + facet_grid(race~.)

#create bar graph of counts paneled by column using .~ before paneling variable name
ggplot(SDdata, aes(school)) +
   geom_bar() + facet_grid(.~race)
```

```{r histograms and boxplot}

#create histograms for continuous variables with white background
ggplot(SDdata, aes(absent)) +
   geom_histogram() + theme_bw()

#create histograms with designated bin width with white background and modified labels
ggplot(SDdata, aes(absent)) +
   geom_histogram(binwidth = 5) +
   theme_bw() +
   labs(x="Days Absent", y = "Frequency")

#create histogram with a normal curve
ggplot(SDdata, aes(absent)) +
  geom_histogram(aes(y= ..density..), color = "black", fill="grey", binwidth = 5) +      #make black lines around grey bars
   labs(x="Days Absent", y = "Density")  +
   theme_classic()  +                             
   stat_function(fun=dnorm, args=list(mean=mean(absent), sd=sd(absent)), colour="black", size=1)

#boxplots
ggplot(SDdata, aes(race, absent)) +
   geom_boxplot() +
   labs(x="Race", y="Days Absent") + theme_bw() 

#error bar plots
ggplot (SDdata, aes (school, absent)) +  
  theme_classic() + 
  stat_summary(fun.y = "mean", geom = "bar", fill="grey60", color="black" ) +       
   stat_summary(fun.data = "mean_cl_normal",   
   geom = "errorbar", width = .2, size = 1) + 
   labs(x="School Year", y="Days Absent")

#error bar plots for 2 variables
ggplot(SDdata, aes(school, absent, fill=gender)) +
   stat_summary(fun.y=mean, geom="bar", position="dodge") +   
   stat_summary(fun.data=mean_cl_normal, geom="errorbar", position = position_dodge(width=.9), width=.2)  + #width=.2 for 20% of width of bar
   labs(x="School Year", y="Days Absent", fill="Gender") + 
   scale_fill_grey(start = 0, end = .7) + theme_classic()           #for black and grey bars and white background
```
