This is an R Markdown document to show how to create a beautiful graphical display of information.
First we need to import our data. Our data includes the Area under the curve for a ROC analysis investigating different behavioral scales as screeners across different grade levels (Chafouleas, Kilgus, Jaffrey, Riley-Tillman, Welsh, & Christ, 2013). Our Data file can be found here: https://www.dropbox.com/s/pwrg7n1dlqmwk6r/lab1.csv. Be sure to save the file somewhere easily accessible on your computer. To import the data, the function we use is read.csv.
You can name the data so that you can call the data more easily later on when writing your R script. In this case we named the data lab1 (but it just as well could be clock, or any other arbitrary word). The <- is a function in R to name the file.
lab1 <- read.csv("~/Documents/UMN/Spring2014/Stats2/Assignments/Lab 1/lab1.csv")
To create the graphical display with GGPlot, we first have to load the program to our library.
library("ggplot2")
Now, that we've loaded ggplot2 and our data, we're ready to create a plot. In this case we want to create a bar graph with error bars to show the confidence intervals.
ggplot is our function and works in layers. First, define the data with data= lab1. Define aesthetics of the plot using aes(x=, y= _)
Add layers to the graph with the geom_ function. geom_bar tells R we want a bar graph. We can define the position of the bars with position=“dodge” so that the variables are unstacked. We applued the function stat=“identity” to make it so that the height of the bars indicate the value of the variable and not the count of the data.
To add error bars, we first have to define the upper and lower limits of the confidence intervals. We named this function “limits”. The Ymax and Ymin are defined as two variables in the lab1 dataset. After we define “limits” we use the function geom_errorbar. You can set the width of the error bars and the position to line up with the bar graphs.
See our ggplot!
limits <- aes(ymax = lab1$upper, ymin = lab1$lower)
ggplot(data = lab1, aes(x = Grade.Level, fill = Scale, y = AUC)) + geom_bar(position = "dodge",
stat = "identity") + geom_errorbar(limits, width = 0.9, position = "dodge")