STAT1003 – Statistical Techniques
Dr. Emi Tanaka
Australian National University
These slides are best viewed on a modern browser like Google Chrome on a desktop or laptop. Some interactive components may require some time to fully load.


One function
One complete plot type
The number of plots that can be drawn
The number of plot functions

ggplot2 R packageggplot2 R package is part of the tidyverse suite of R packagesggplot2 is widely used by the scientific community and even by news outlets (e.g. Financial Times and BBC)Wilkinson (2005) introduced “the grammar of graphics” as a paradigm to describe plots by combining a finite number of components.
ggplot2 R package (as part of his PhD project).plotnine), Julia (e.g., Gadfly.jl, VegaLite.jl), and Javascript (e.g. VegaLite).ggplot
data.frame
geom_histogram()geom_density()stat_ecdf()stat_qq()geom_boxplot()geom_violin()geom_jitter()faithful is a built-in data set in Rgeom layers in ggplot2stat layers in ggplot2ggplotggplot has five main components:
geom - the geometric object to use display the datastat - statistical transformation to use on the data datadata to be displayed in this layer (usually inherited)mapping - aesthetic mappings (usually inherited)position - position adjustment

y = stat(density) andy = ..density..Density is calculated as the count divided by the total number of observations and the bin width.
geom_point()geom_smooth()geom_bin2d()geom_hex()geom_line()
Palmer penguins

vignette("ggplot2-specs")
?geom_point).Common aesthetics include:
x and y
alpha
color
fill
size
Data variables:
species island bill_len bill_dep flipper_len body_mass sex year
geom_point()Make the following target plot:

shape
stroke vs size
stroke and fill is only for the “filled” shapes.color
linetype
linewidth
lineend
linejoin
aes, it assumes that it’s a data variable."dodgerblue" gets converted into a variable with one level and it gets colored by ggplot’s default color palette.Don’t put attributes inside aes()!
Make this target plot:

Use I() operator to mean “as-is” in aesthetic mapping.
Attributes should be defined in specific layers.
ggplot() but not attributes.
data.framegeom_ or stat_ functions) which describes what to renderx, y, color, fill, size, alpha, shape, linetype, linewidth, etc.
geom - the geometric object to use display the datastat - statistical transformation to use on the data datadata to be displayed in this layer (usually inherited)mapping - aesthetic mappings (usually inherited)position - position adjustmentggplot2 cheatsheet


STAT1003 – Statistical Techniques