Sandhya Kambhampati
June 4, 2016
“R is a language and environment for statistical computing and graphics”
Source: R-project.org
“The emphasis in ggplot2 is reducing the amount of thinking time by making it easier to go from the plot in your brain to the plot on the page.”
Wickham, 2012
Source: Hopper (2014)
install.packages('ggplot2')
library('ggplot2')
Make sure you have the most recent version of R to get the most recent version of ggplot2
data(Seatbelts)
s <- as.data.frame(Seatbelts)
ts <- data.frame(Year=floor(time(Seatbelts)),
Month=factor(cycle(Seatbelts),
labels=month.abb), Seatbelts)
head(ts)
qplot( data = ts,x= Year,y= DriversKilled, main= "Drivers Killed by Year")
ggplot(data = ts,
aes(x = Year,
y = DriversKilled)) +
geom_point() +
ggtitle("Drivers killed by Year")
qplot( data = ts,
x= Year,
y= DriversKilled,
main= "Drivers Killed by Year")
ggplot(data = ts,
aes(x = Year,
y = DriversKilled)) +
geom_point() +
ggtitle("Drivers killed by Year")
ggplot(data = ts,
aes(x = Year,
y = VanKilled)) +
geom_bar(stat = 'identity')
ggplot(data = ts,
aes(x = Year,
y = front)) +
geom_point() +
geom_line()
ggplot(data = ts,
aes(x = Year,
y = DriversKilled)) +
geom_point() +
scale_y_continuous(limits = c(50,200))
ggplot(data = ts,
aes(x = Year,
y = DriversKilled,
color = Month)) +
geom_point()
ggplot(data = ts,
aes(x = Year,
y = VanKilled)) +
geom_bar(stat = 'identity') +
theme(panel.background = element_blank())
Source: Hopper (2014)
“A Layered Grammar of Graphics” by Hadley Wickham
“A Simple Introduction to the Graphing Philosophy of ggplot2” by Tom Hopper
ggplot2 Cheat Sheet by RStudio
ggplot2 Documentation by Hadley Wickham & Winston Chang
Resources for Doing Data Journalism by Timo Grossenbacher
Shapes and Line Types in R by Winston Chang
ggplot(data = ts,
aes(x = Month,
y = DriversKilled)) +
geom_text(aes(label = Year))
facet_grid(vertical ~ horizontal)
facet_wrap(~ variable, nrow = ___, ncol = ___)
ggplot(data = ts,
aes(x = Year,
y = DriversKilled)) +
geom_line() +
facet_wrap(~ Month)
ggplot(data = ts,
aes(x = Year,
y = DriversKilled)) +
geom_point() +
stat_smooth(method = 'lm')