This material is heavily inspired (stolen?) from
Thomas Lin Pedersen’s, Software Engineer at RStudio, co-author of ggplot2, author of gganimate, ggplot2-workshop
Hadley Wickham (2016) Elegant graphics for data analysis, 2nd edition, ISSN 2197-5744
Further resources:
Main objective: Hands on ggplot
Schedule
What is ggplot2?
# install.packages("ggplot2")
library(ggplot2)
ggplot(faithful, aes(x=waiting, y=eruptions)) +
geom_point() +
labs(title="ggplot2 default")
Theory of data visualisation: how to draw information to convey a (non-misleading) message
Base R plotting
plot(x=faithful$waiting, y=faithful$eruptions, main="base r default")
plotly
library(plotly)
plot_ly(data = faithful, x = ~waiting, y = ~eruptions) %>%
layout(title="plotly default")
many other packages/attempts
ggplot extensions
ggplot is heavily inspired by the grammar of graphics. Grammar of graphics is a general approach to plotting by following the approach of Wilkinson L (2005) The grammar of graphics. Statistics and computing, 2nd edn. Springer, New York.
The grammar of graphics distinguishes several elements
All of these elements are somehow represented in r-package ggplot2.
A first basic plot:
ggplot(data = faithful,
mapping = aes(x = eruptions,y = waiting)) +
geom_point()
Which elements have been used?
Answer: All elements
Note the ‘+’ to combine different parts of the graphic (the layers)
# same plot - alternative specification
ggplot(data = faithful) +
geom_point(mapping = aes(x = waiting,y = eruptions),
colour= "blue")
# local (here) vs. global (above) aesthetics
Use ‘?geom_point’, ggplot2 cheatsheet, vignette(“ggplot2-specs”)
Look at g and g_built with the view function. Can you find size? It’s in the ‘data’ part of one of the objects.
Insights
g <- ggplot(data = faithful) +
geom_point(mapping = aes(x = eruptions,y = waiting, size=eruptions/waiting))
No output - result of ggplot() captured in object g. What’s in g? Can you find ‘size’?
Insight
df <- data.frame(x = c(1,2,5,4,5), y = c(9, 1, 9,3,4))
base <- ggplot(df, aes(x, y))
base + geom_point()
base + geom_line()
base + geom_path()
base + geom_polygon()
base + geom_rect()
base + geom_ribbon(aes(ymin=x,ymax=y))
base + geom_label(aes(label=paste0("(",x,",",y,")")))
faithful %>% slice(10)
## eruptions waiting
## 1 4.35 85
faithful[10,]
## eruptions waiting
## 10 4.35 85
ggplot(data = faithful) +
geom_point(mapping = aes(x = eruptions,y = waiting),
data = faithful %>% filter(eruptions > 3 & eruptions < 3.1), colour="red", size=10) +
geom_point(mapping = aes(x = eruptions,y = waiting))