knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
ggplot2
to create our data visualizations.textline + stat_summary(fun = mean, geom = "point") + stat_summary(fun = mean, geom = "line", aes(group = Group)) + stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width = .2) + xlab("Measurement Time") + ylab("Mean Grammar Score") + cleanup + scale_color_manual(name = "Texting Option", labels = c("All the texts", "None of the texts"), values = c("Black", "Grey")) + scale_x_discrete(labels = c("Baseline", "Six Months"))
Graphs:
data
folder that is in the same folder as my Markdown file.rio
library can usually interpret these files without much work using the import()
function. library(rio) chickflick <- import("data/ChickFlick.sav") str(chickflick)
gender
and film
variables have embedded data from SPSS. table(chickflick$gender) table(chickflick$film)
chickflick$gender <- factor(chickflick$gender, #the variable you want to factor levels = c(1,2), #the information already in the data labels = c("Male", "Female")) #the labels for those levels table(chickflick$gender)
Wide datasets: rows are participants, and columns are variables.
Long datasets: rows are assessments, and columns are variables.
Each time point or repeated assessment gets a separate row, while each column still represents a variable.
library(reshape) #note: you could also use pivot_longer in tidyverse cricket <- import("data/Jiminy_Cricket.csv") head(cricket)
id = c("column", "column")
- constant variables you do not want to change. These will stay their own column but get repeated when necessary.measured = c("column", "column")
- dependent variables you want to combine into one column.longcricket <- melt(cricket, #name of dataset id = c("ID", "Strategy"), measured = c("Success_Pre", "Success_Post")) #you can actually leave measured blank head(longcricket)
variable
and value
, which are not very useful names.colnames(longcricket)[3:4] #just to figure out which ones colnames(longcricket)[3:4] <- c("Time", "Score")
Graphs should (Tufte, 2001):
knitr::include_graphics("pictures/graphs/badgraph.png")
plotly
). knitr::include_graphics("pictures/graphs/bettergraph.png")
knitr::include_graphics("pictures/graphs/deception.png")
ggplot2
is that it is very flexible and well documented!ggplot2
is that there's a lot going on. Hmisc
for your error bars. library(ggplot2)
#an example myGraph <- ggplot(dataset, aes(x_axis, y_axis, color = legend_var, fill = legend_var))
#an example part 2 myGraph + geom_bar() + geom_point() + xlab("X Axis Label") + ylab("Y Axis Label")
Histograms plot:
Histograms help us to identify:
Unusual scores
crickethist <- ggplot(data = cricket, #dataset aes(x = Success_Pre) #only define X axis ) crickethist
crickethist + geom_histogram()
crickethist + geom_histogram(binwidth = 1)
You can change the color:
crickethist + geom_histogram(binwidth = 1, color = 'purple', fill = 'magenta')
crickethist + geom_histogram(binwidth = 1, color = 'purple', fill = 'magenta') + xlab("Success Pre Test") + ylab("Frequency")
festival <- import("data/festival.csv") str(festival)
You can run the whole graph at once!
Create the plot object:
festivalhist <- ggplot(data = festival, aes(x = day1)) festivalhist + geom_histogram(binwidth = 1, color = 'blue') + xlab("Day 1 of Festival Hygiene") + ylab("Frequency") + theme_bw() #theme_classic() also good!
All graphs should have:
theme_bw()
and theme_classic()
are great, easy themes to make graphs presentable.cleanup <- theme(panel.grid.major = element_blank(), #no grid lines panel.grid.minor = element_blank(), #no grid lines panel.background = element_blank(), #no background axis.line.x = element_line(color = 'black'), #black x axis line axis.line.y = element_line(color = 'black'), #black y axis line legend.key = element_rect(fill = 'white'), #no legend background text = element_text(size = 15)) #bigger text size
festivalhist + geom_histogram(binwidth = 1, color = 'blue') + xlab("Day 1 of Festival Hygiene") + ylab("Frequency") + cleanup
exam <- import("data/Exam_Anxiety.csv") str(exam)
table(exam$Gender) exam$Gender <- factor(exam$Gender, levels = c(1,2), labels = c("Male", "Female")) table(exam$Gender)
scatter <- ggplot(exam, aes(Anxiety, Exam)) scatter + geom_point() + xlab("Anxiety Score") + ylab("Exam Score") + cleanup
scatter + geom_point()+ geom_smooth(method = 'lm', color = 'black', fill = 'blue') + xlab('Anxiety Score')+ ylab('Exam Score')+ cleanup
How to control the colors and fill with legends.
scatter2 <- ggplot(exam, aes(Anxiety, Exam, color = Gender, fill = Gender)) #why both? scatter2 + geom_point() + geom_smooth(method = "lm") + xlab("Anxiety Score") + ylab("Exam Score") + cleanup + scale_fill_manual(name = "Gender of Participant", labels = c("Men", "Women"), values = c("purple", "grey")) + scale_color_manual(name = "Gender of Participant", labels = c("Men", "Women"), values = c("purple", "grey10"))
GGally
for Multiple VisualizationGGally
has a plotting function that uses ggplot2
as a back end. ggpairs
allows you to create a scatterplot matrix for any numerical variables in a dataset. library(GGally) ggpairs(data = exam[ , -1], #no participant variable title = "Exam Anxiety, Scores, and Gender")
Error bars can be added to display the precision of the mean:
Is there such a thing as a 'chick flick'?
str(chickflick) #already fixed gender chickflick$film <- factor(chickflick$film, levels = c(1,2), labels = c("Bridget Jones", "Memento"))
stat_summary()
function:chickbar <- ggplot(chickflick, aes(film, arousal)) chickbar + stat_summary(fun = mean, geom = "bar", fill = "White", color = "Black") + cleanup
stat_summary()
:chickbar + stat_summary(fun = mean, geom = "bar", fill = "White", color = "Black") + stat_summary(fun.data = mean_cl_normal, geom = "errorbar", position = position_dodge(width = 0.90), width = 0.2) + cleanup
chickbar + stat_summary(fun = mean, geom = "bar", fill = "White", color = "Black") + stat_summary(fun.data = mean_cl_normal, geom = "errorbar", position = position_dodge(width = 0.90), width = 0.2) + xlab("Movie Watched by Participant") + ylab("Arousal Level") + cleanup + scale_x_discrete(labels = c("Girl Film", "Guy Film"))
chickbar2 <- ggplot(chickflick, aes(film, arousal, fill = gender)) chickbar2 + stat_summary(fun = mean, geom = "bar", position = "dodge") + stat_summary(fun.data = mean_cl_normal, geom = "errorbar", position = position_dodge(width = 0.90), width = .2) + xlab("Film Watched") + ylab("Arousal Level") + cleanup + scale_fill_manual(name = "Gender of Participant", labels = c("Boys", "Girls"), values = c("Gray30", "Gray"))
When to use a line graph:
hiccups <- import("data/Hiccups.csv") str(hiccups)
ggplot2
to use. longhiccups <- melt(hiccups, measured = c("Baseline", "Tongue", "Carotid", "Other")) str(longhiccups) colnames(longhiccups) <- c("Intervention", "Hiccups")
hiccupline <- ggplot(longhiccups, aes(Intervention, Hiccups)) hiccupline + stat_summary(fun = mean, ##adds the points geom = "point") + stat_summary(fun = mean, ##adds the line geom = "line", aes(group=1)) + ##necessary for mapping line to dots stat_summary(fun.data = mean_cl_normal, ##adds the error bars geom = "errorbar", width = .2) + xlab("Intervention Type") + ylab("Number of Hiccups") + cleanup
texting <- import("data/Texting.xlsx") str(texting)
Two issues to clean up:
texting$Group <- factor(texting$Group, levels = c(1,2), labels = c("Texting Allowed", "No Texting Allowed")) longtexting <- melt(texting, id = c("Group"), measured = c("Baseline", "Six_months")) str(longtexting) colnames(longtexting) <- c("Group", "Time", "Grammar_Score")
textline <- ggplot(longtexting, aes(Time, Grammar_Score, color = Group)) textline + stat_summary(fun = mean, geom = "point") + stat_summary(fun = mean, geom = "line", aes(group = Group)) + #Group is the variable name stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width = .2) + xlab("Measurement Time") + ylab("Mean Grammar Score") + cleanup + scale_color_manual(name = "Texting Option", labels = c("All the texts", "None of the texts"), values = c("Black", "Grey")) + scale_x_discrete(labels = c("Baseline", "Six Months"))
In this lecture, you have learned:
factor()
and melt()
ggplot2
structure and how to get startedAdd the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.