Dennis Murphy, Ph.D.
November 4, 2014
This is the short form of the presentation, which will focus primarily on the examples in the accompanying script file. See the Github repo for the long form presentation and extended set of examples and exercises.
Two ways to produce a ggplot:
qplot()
ggplot()
We will only be concerned with the latter. For documentation
on qplot()
, see
http://ggplot2.org/book/qplot.pdf
aes()
function or set to a single valueggplot2 is defined as a “layered grammar of graphics”, which implies that layers are central to the package.
A layer consists of the following components of the grammar:
Sometimes, a component is implicit; for example, the typical default
position adjustment is identity
, which means no adjustment to the
computed position.
Usually, one stat or geom specification is sufficient because each stat has a default geom and vice versa.
One can specify different data frames for different layers (USEFUL!!)
layer(geom, geom_params, stat, stat_params, data, mapping, position)
It is possible to declare ggplot2
layers explicitly, but this is
rarely done in practice. The stat
, geom
and aes
functions do all
of the work that layer
could with fewer keystrokes.
A ggplot is typically generated by “adding” layers with the +
operator.
Each call to a geom_xxx()
or stat_xxx()
function generates a
new graphical layer.
Defined by invoking ggplot()
. When its arguments are specified,
Examples:
ggplot(data = DF,
mapping = aes(x, y, color = grp))
ggplot(data = DF)
ggplot()
The primary functions in ggplot2
have the general form
component_type()
. Some examples:
geom_point()
stat_contour()
coord_polar()
position_jitter()
annotation_raster()
facet_wrap()
scale_y_continuous()
Several types of transformations take place when constructing a ggplot, in the following order:
stat
functions)coord
functions)Coordinate transformations are capable of changing the shape of geoms as well as the shape of positional axes.
Each (complete) theme function defines about 35 properties, many of which are controlled by one of the following theme element functions:
element_text()
element_line()
element_rect()
The properties of a theme element can be set or redefined by one of
the above element_*()
functions. To erase the properties of a theme
element, use the special function element_blank()
.
The theming system was overhauled in version 0.9.2. Two important new features were introduced in the revised system:
Both of these features simplify code writing when defining a new theme function or modifying an existing theme function.