Licensed under Creative Commons Attribution CC BY 4.0 Last generated: 2023-11-14
Wilkinson, L. (2005) The grammar of graphics. Springer New York.
Wilkinson, L. (2005) The grammar of graphics. Springer New York.
Wilkinson, L. (2005) The grammar of graphics. Springer New York.
Graphics are collections of objects that follow a set of rules, a grammar, so that they behave consistently and flexibly.
The specification of the formal language is expressed in six statements:
Wilkinson, L. (2005) The grammar of graphics. Springer New York.
Graphics are collections of objects that follow a set of rules, a grammar, so that they behave consistently and flexibly.
The specification of the formal language is expressed in six statements:
These components link data to (visual) objects and specify a scene containing those.
An R package for producing statistical graphics
Underlying grammar based on the Grammar of Graphics (thus GG)
An R package for producing statistical graphics
Underlying grammar based on the Grammar of Graphics (thus GG)
Instead of being limited to sets of pre-defined graphics,
it allows to compose graphs by combining (adding, +
) components
An R package for producing statistical graphics
Underlying grammar based on the Grammar of Graphics (thus GG)
Instead of being limited to sets of pre-defined graphics,
it allows to compose graphs by combining (adding, +
) components
Simple set of core principles (+ some very few special cases)
Carefully chosen defaults
An R package for producing statistical graphics
Underlying grammar based on the Grammar of Graphics (thus GG)
Instead of being limited to sets of pre-defined graphics,
it allows to compose graphs by combining (adding, +
) components
Simple set of core principles (+ some very few special cases)
Carefully chosen defaults
Good for quick prototyping, designed to work iteratively
But also publication-quality graphics, with a comprehensive theming system
An R package for producing statistical graphics
Underlying grammar based on the Grammar of Graphics (thus GG)
Instead of being limited to sets of pre-defined graphics,
it allows to compose graphs by combining (adding, +
) components
Simple set of core principles (+ some very few special cases)
Carefully chosen defaults
Good for quick prototyping, designed to work iteratively
But also publication-quality graphics, with a comprehensive theming system
Lots of extensions!
country | continent | year | lifeExp | pop | gdpPercap |
---|---|---|---|---|---|
Mauritius | Africa | 1962 | 60.246 | 701016 | 2529.0675 |
Indonesia | Asia | 1957 | 39.918 | 90124000 | 858.9003 |
Italy | Europe | 1977 | 73.480 | 56059245 | 14255.9847 |
country | continent | year | lifeExp | pop | gdpPercap |
---|---|---|---|---|---|
Mauritius | Africa | 1962 | 60.246 | 701016 | 2529.0675 |
Indonesia | Asia | 1957 | 39.918 | 90124000 | 858.9003 |
Italy | Europe | 1977 | 73.480 | 56059245 | 14255.9847 |
country | continent | year | lifeExp | pop | gdpPercap |
---|---|---|---|---|---|
Mauritius | Africa | 1962 | 60.246 | 701016 | 2529.0675 |
Indonesia | Asia | 1957 | 39.918 | 90124000 | 858.9003 |
Italy | Europe | 1977 | 73.480 | 56059245 | 14255.9847 |
All plots are composed of data and mapping, the description of how data attributes are mapped to aesthetic attributes (channels).
Basic workflow:
ggplot(data) + # create the graphic object with the data aes(x=..., y=..., color=...) + # add the general mapping ... # add more components (geoms, scales, coords, facets, themes...)
There are five types of components:
There are five types of components:
There are five types of components:
A layer is a collection of geometric elements (points, lines...) and statistical transformations (binning, counting...).
A scale controls a channel, adds or modifies how attributes are mapped (position, color, shape, size...).
There are five types of components:
A layer is a collection of geometric elements (points, lines...) and statistical transformations (binning, counting...).
A scale controls a channel, adds or modifies how attributes are mapped (position, color, shape, size...).
A coordinate system describes how data coordinates are mapped to the plane of the graphic. It also provides axes and gridlines.
There are five types of components:
A layer is a collection of geometric elements (points, lines...) and statistical transformations (binning, counting...).
A scale controls a channel, adds or modifies how attributes are mapped (position, color, shape, size...).
A coordinate system describes how data coordinates are mapped to the plane of the graphic. It also provides axes and gridlines.
A facet specifies how to break up and display subsets of data as small multiples (AKA conditioning, latticing or tresllising).
There are five types of components:
A layer is a collection of geometric elements (points, lines...) and statistical transformations (binning, counting...).
A scale controls a channel, adds or modifies how attributes are mapped (position, color, shape, size...).
A coordinate system describes how data coordinates are mapped to the plane of the graphic. It also provides axes and gridlines.
A facet specifies how to break up and display subsets of data as small multiples (AKA conditioning, latticing or tresllising).
A theme controls the finer points of display to create attractive plots (background, fonts, guide aspect and positioning...).
Read the comprehensive guide on aesthetics.
Read the comprehensive guide on aesthetics.
From ?geom_point
(required in bold):
x
y
alpha
colour
fill
group
shape
size
stroke
From ?geom_line
(required in bold):
x
y
alpha
colour
group
linetype
size
Geom | Result | Details |
---|---|---|
geom_point() geom_text() geom_label() |
scatterplot | Understands shape .Helper for text. Helper for labels. |
geom_line() geom_path() geom_step() geom_function() |
line plot | Connects points from left to right, understands linetype .Connects points in order. Produces a stairstep plot. Connects points of a given function of x . |
geom_bar() geom_col() |
bar chart | stat="count" by default!Multiple bars are stacked by default. |
geom_area() |
area plot | Line plot filled from 0 to y . |
geom_polygon() |
Filled path. | |
geom_rect() geom_tile() geom_raster() |
Rectangle by xmin , xmax , ymin , ymax .Rectangle by center ( x , y ) and size (width , height ).Faster tiles with constant size. |
x
and y
, understand color
and size
.fill
ed.Geom | Result | Details |
---|---|---|
geom_jitter() geom_count() geom_bin_2d() geom_hex() |
geom_point() , but adds some jitter to each point.Maps the count of overlapping points to size .Maps the count of rectangles to fill .Same, but using hexagons. |
Geom | Result | Details |
---|---|---|
geom_pointrange() geom_linerange() geom_errorbar() geom_crossbar() |
Various ways of representing a vertical intervals defined by x , ymin and ymax . |
|
geom_ribbon() |
Special case of geom_area() with ymin too. |
Geom | Result | Details |
---|---|---|
geom_segment() geom_curve() geom_spoke() |
Straight line between points (x , y ) and (xend , yend ).Same, but curved line. Polar parametrization of geom_segment() . |
Geom | Result | Details |
---|---|---|
geom_histogram() geom_freqpoly() geom_dotplot() |
histogram | Distribution of a continuous variable by bins. To display the counts with lines instead. Histograms of stacked dots. |
geom_density() |
density plot | Smoothed version of the histogram. |
geom_rug() |
Draws ticks for marginal distributions. |
Geom | Result | Details |
---|---|---|
geom_boxplot() geom_violin() |
boxplot | Compact display of the distribution of a continuous variable. Mirrored density, displayed as a boxplot. |
Geom | Result | Details |
---|---|---|
geom_smooth() geom_quantile() |
Fits a model and draws a smoothing line. Fits a quantile regression and draws the quantiles. |
Geom | Result | Details |
---|---|---|
geom_contour() geom_contour_filled() geom_density_2d geom_density_2d_filled() |
contour plot | 2D contours of 3D surfaces of regular x , y .Filled version. 2D contours after computing the density. Filled version. |
Geom | Result | Details |
---|---|---|
geom_map() geom_sf() geom_sf_text() geom_sf_label() |
map | Old way to plot polygons as a map. Current recommended way via sf .Similar to geom_text() but for sf .Similar to geom_label() but for sf . |
ggplot(mpg, aes(displ, hwy)) + geom_point(stat="identity")
ggplot(mpg, aes(displ, hwy)) + stat_identity(geom="point")
ggplot(mpg, aes(hwy)) + geom_bar(stat="count")
ggplot(mpg, aes(hwy)) + stat_count(geom="bar")
ggplot(mpg, aes(displ, hwy)) + geom_smooth(stat="smooth")
ggplot(mpg, aes(displ, hwy)) + stat_smooth(geom="smooth")
A scale is a procedure that performs the mapping of data attributes into channels (position, color, size...):
sets the limits;
sets an optional transformation (without modifying the data);
sets a guide.
A scale is a procedure that performs the mapping of data attributes into channels (position, color, size...):
sets the limits;
sets an optional transformation (without modifying the data);
sets a guide.
A guide allows us to revert the procedure and recover the data:
an axis or a legend, depending on the channel;
has a name, breaks, labels...
Naming: scale_<aes>_<type>(<arguments>)
Element | Argument | Shortcut function |
---|---|---|
Title | name=... |
labs(x=..., y=..., color=..., ...) |
Limits | limits=... |
lims(x=..., y=..., color=..., ...) |
Breaks | breaks=... |
|
Labels | labels=... |
|
Guide | guide=... |
guides(x=..., y=..., color=..., ...) |
Transformation | trans=... |
coord_cartesian()
: default, no need to be specified
... although it is useful to set axes limits (via xlim
and ylim
arguments).
Position given by orthogonal distances, x
and y
, to an origin.
coord_cartesian()
: default, no need to be specified
... although it is useful to set axes limits (via xlim
and ylim
arguments).
Position given by orthogonal distances, x
and y
, to an origin.
Some helper functions:
coord_flip()
: helper to flip the axes.
coord_fixed()
: helper to fix the aspect ratio.
coord_trans()
: helper to transform the axes.
coord_polar()
: x
is the angle, y
is the radius (can be reverted).
coord_map()
: projections of the sphere into a plane.
mapproj
package.coord_sf()
: modern way to deal with maps via simple features
(from sf
package).
Guides (axes and legend)
Titles (title, subtitle and caption)
Guides (axes and legend)
Titles (title, subtitle and caption)
Text labels
Guides (axes and legend)
Titles (title, subtitle and caption)
Text labels
Reference lines
Guides (axes and legend)
Titles (title, subtitle and caption)
Text labels
Reference lines
Reference areas
Guides (axes and legend)
Titles (title, subtitle and caption)
Text labels
Reference lines
Reference areas
Direct labeling
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
s | Toggle scribble toolbox |
Esc | Back to slideshow |