This Tutorial has been thrown together a little hastily and is therefore not very well organised - sorry! Graphical features are demonstrated either via tables of properties or as clickable graphics that reveal the required R code. Click on a graphic to reveal/toggle the source code or to navigate to an expanded section.
This tutorial is intended to be viewed sequentially. It begins with the basic ggplot framework and then progressively builds up more and more features as default elements are gradually replaced to yeild more customized graphics.
Having said that, I am going to start with a sort of showcase of graphics which should act as quick navigation to entire sections devoted to the broad series of graphs related to each of the featured graphics. I have intentionally titled each graph according to the main feature it encapsulates rather than any specific functions that are used to produce the features as often a single graphic requires a combination of features and thus functions. Furthermore, the grammar of graphics specifications are sufficiently unfamiliar to many that the relationships between the types of graphical features a researcher wishes to produce and the specific syntax required to achieve the desired result can be difficult to recognise.
Each graphic is intended to encapsulate a broad series of related graph types.
The Grammar of Graphics was first introduced/presented by Wilkinson and Wills (2006) as a new graphics philosophy that laid down a series of rules to govern the production of quantitative graphics. Essentially the proposed graphics infrastructure considers a graphic as comprising a plot (defined by a coordinate system, scales and panelling) over which one or more data layers are applied.
ggplot() +# required geom_*( # requireddata =<DATA>, # required - <DATA> is a data framemapping =aes(<MAPPING>), # required - map variables to scalesstat =<STAT>, # optional - map variables to geomsposition =<POSITION>) +# optional - adjustments to overlapping geoms coord_*() +# optional - specify coordinate system scale_*() +# optional - visual appearence of scales facet_*() +# optional - subplots theme_*() # optional - overal appearence
Each layer is defined as:
the data - a data frame
mapping specifications that establish the visual aesthetics (colour, line type and thickness, shapes etc) of each variable
statistical methods that determine how the data rows should be summarised (stat)
geometric instructions (geom) on how each summary should be represented (bar, line, point etc)
positional mechanism for dealing with overlapping data (position)
The visual aspects of all the graphical features are then governed by themes.
Following a very short example, the next section will largely concentrate on describing each of the above graphical components. Having then established the workings of these components, we can then put them together to yield specific graphics.
Hadley Wickham’s interpretation of these principals in an R context is implimented via the ggplot2package. In addition the following packages are also commonly used alongside ggplot so as to expand on the flexibility etc.
grid
gridExtra
scales
patchwork
tidyverse - which is actually a collection of packages that make up the tidyverse ecosystem.
To help illustrate graphical routines and techniques, this tutorial will make use of a number of data sets - most of which are distributed with base R or else one of the tidyverse _packages. The first of these motivating datasets is a built in data set (BOD) that records temporal changes (days) in biochemical oxygen demand (mg/l).
The biochemical oxygen demand (BOD) data set comprises of just two variables (demand: a numeric vector representing biochemical oxygen demand (mg/l) and Time: a numeric vector representing the time associated with the demand measurement). It was originally published in a master thesis in the late 1960’s and has since become a standard example data set on which to demonstrate various statistical techniques.
The CO2 data set represents the CO₂ uptake (uptake: μmol/m²) of twelve individual Echinochloa crus-galli plants (Plant) from two locations (Type) and two temperature treatments (Treatment) in response to a range of ambient carbon dioxide concentration (conc: ml/l).
This is a purely ficticious and fabricated example that is intended to provide a very small example data set.
df <-data.frame(x =c(3, 1, 5), y =c(2, 4, 6), z =c('a', 'b', 'c'))
x
y
z
3
2
a
1
4
b
5
6
c
The iris data set is a famous example data set that comprises the measurements (cm) of sepal and petal dimensions for three species of iris (Iris setosa, versicolor, and virginica).
The warpbreaks dataset comprises of the number of warp breaks per loom for two different types of wool under three different tensions.
breaks - the number of warp breaks
wool - the type of wool (A or B)
tension - the level of tension (L: low, M: medium, H: high)
head(warpbreaks)
breaks wool tension
1 26 A L
2 30 A L
3 54 A L
4 25 A L
5 70 A L
6 52 A L
glimpse(warpbreaks)
Rows: 54
Columns: 3
$ breaks <dbl> 26, 30, 54, 25, 70, 52, 51, 26, 67, 18, 21, 29, 17, 12, 18, 35…
$ wool <fct> A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,…
$ tension <fct> L, L, L, L, L, L, L, L, L, M, M, M, M, M, M, M, M, M, H, H, H,…
The following very simple graphic will be used to illustrate the general ggplot template outlined above by explicitly stating many of the default specifications. It will use a cartesian coordinate system, continuous axes scales, a single facet (panel) and then define a single layer with a dataframe (BOD), with red points, identity (no summarising) statistic visualised as a point geometric.
p <-ggplot() +# initialise ggplot layer(data = BOD, # data.framemapping =aes(y = demand, x = Time), # define x and y variablesstat ="identity", # use raw input datageom ="point", # plot data as pointsposition ="identity", # how to handle overlap dataparams =list(na.rm =TRUE), # additional params for statshow.legend =FALSE# whether include a legend )+layer(data = BOD, # data.framemapping =aes(y = demand, x = Time), # define x and y variablesstat ="identity", # use raw input datageom ="line", # plot data as a lineposition ="identity", # how to handle overlap dataparams =list(na.rm =TRUE), # additional params for statshow.legend =FALSE# whether include a legend ) +coord_cartesian() +# cartesian coordinatesscale_x_continuous() +# continuous x axisscale_y_continuous() # continuous y axisp # print the plot
Conveniently, much of the default specifications can be omitted. Hence the above can also be entered as:
p <-ggplot(data = BOD, # initialise ggplot map =aes(x = Time, y = demand)) +# define x and y variablesgeom_point() +# add pointsgeom_line() # add linesp # print the plot
Note the following important features of the grammar of graphics as implemented in R:
the order in which each of the major components in the first code snippet were added is unimportant. They each add additional information to the overall graphical object. The object itself is evaluated as a whole when it is printed. For example, scales can be defined before layers.
Explore
The following both alter the x axis to a log (base 10) scale. Note how both expressions yield the same outcome.
p <-ggplot(data = BOD, map =aes(x = Time, y = demand)) +geom_point() +geom_line() +scale_x_log10()p
p <-ggplot(data = BOD, map =aes(x = Time, y = demand)) +scale_x_log10() +geom_point() +geom_line() p
multiple layers are laid down in the order that they appear in the statement. In the example above, the lines are drawn over the top of the points.
Explore
The following define points and lines in different orders. Note how the points appear under the line in the left hand figure and over the line in the right hand figure.
p <-ggplot(data = BOD, map =aes(x = Time, y = demand)) +geom_point(color ='red', size=5) +geom_line() p
p <-ggplot(data = BOD, map =aes(x = Time, y = demand)) +geom_line() +geom_point(color ='red', size=5) p
layers can be defined either with the layersfunction, or more conveniently (as illustrated in the shorter, second code snippet), via a geom_* function or stat_*function.
Explore
The following both specify a single layer (line). The left hand figure uses the geom_line _function with the stat = 'identityargument and the right hand figure uses the stat_identityfunction with the geom = 'line'argument. Note, identity just means multiple by 1 (i.e. do nothing). The default stat for geom_line is identity and therefore it is not necessary to provide this argument - in this case it was purely used for illustrative purposes.
p <-ggplot(data = BOD, map =aes(x = Time, y = demand)) +geom_line(stat ='identity')p
p <-ggplot(data = BOD, map =aes(x = Time, y = demand)) +stat_identity(geom ='line')p
the data and mapping used by geom_* and stats_*functions can inherit mapping aesthetics from the main ggplot() function.
Explore
The following figures both generate a plot with both points and a line. In the left hand side syntax, geom_point and geom_line inherit the data and aesthetic mappings from the ggplotfunction. In the right hand side syntax, the two geoms specify the data and aesthetic mappings themselves. Whilst in this case it does lead to code duplication, in other cases, it does permit different sources of data to be integrated together into a single graphic.
p <-ggplot(data = BOD, map =aes(x = Time, y = demand)) +geom_line() +geom_point()p
p <-ggplot() +geom_line(data = BOD, map =aes(x = Time, y = demand)) +geom_point(data = BOD,map =aes(x = Time, y = demand))p
the ggplot()function along with the other building functions work together to create a set of instructions that determine how a graph is to be constructed. In the examples above, these instructions are stored in a variable (which we arbitrarily assigned the name p). The graph is not actually generated until the print method is called on this object. Recall that in R, entering the name of an object is the same as running the print function on this object.
Explore
In the following figures, the plotting instructions will be built up in multiple stages.
the first stage of both examples stores the ggplot initialisation (including specification of the data and aesthetic mapping).
the second stage of the left hand syntax adds instructions for including two data layers (line and points). Finally, the left hand syntax then uses prints the full set of plotting instructions (which enacts the generation of the plot).
the second stage of the right hand syntax adds instructions for including a single layer (line) and in the final stage, instructions for including a point layer are added to the instruction set sent to print.
note, unlike for the left hand side syntax, the stored set of instructions on the right hand side do not contain the instructions for including the points layer - they are only added at the time of printing
p <-ggplot(data = BOD, map =aes(x = Time, y = demand)) p <- p +geom_line() +geom_point()p
p <-ggplot(data = BOD, map =aes(x = Time, y = demand)) p <- p +geom_line()p +geom_point()
In all of the examples so far, you might have noticed that we stored the plotting instructions in an arbitrary variable (p) prior to printing (generating the plot). Although it is not necessary to store the instructions (we can just accumulate the instructions and print in a single expression), storing sets of instructions do allow the instruction set to be duplicated or altered. This is particularly useful when you have a need to produce two similar and complex plots.
Since this is a tutorial, it will endeavour to focus on a very small set of concepts at any one time. To that end, most of the examples in a section will feature slight variations of a common snippet of code. Typically, there will be a single common code block that initialises a fairly generic plot followed by a series of modifications/additions that highlight different plotting routines or options.
In an attempt to illustrate the use of ggplot for elegant graphics, we will drill down into each of the plot and layer specifications.
2 Layers (geoms and stats)
2.0.1 Overview
Although the geoms and thus layers are amongst the last features to be constructed by the system, the data and aesthetic features of the data impact on how the coordinate system, scales and panelling work. Therefore, we will explore the geoms first.
Geometric objects (geoms) are visual representations of observations. For example, there is a geom to represent points based on a set of x,y coordinates. All graphics need at least one geom and each geom is mapped to its own layer. Multiple geoms can be added to a graphic and the order that they are added to the expression determines the order that their respective layer is constructed.
When a ggplot expression is being evaluated, geoms are coupled together with a stat_ function. This function is responsible for generating data appropriate for the geom. For example, the stat_boxplot is responsible for generating the quantiles, whiskers and outliers for the geom_boxplot function.
In addition to certain specific stat_ functions, all geoms can be coupled to a stat_identity function. In mathematical contexts, identity functions map each element to themselves - this essentially means that each element passes straight through the identity function unaltered. Coupling a geom to an identity function is useful when the characteristics of the data that you wish to represent are present in the data frame. For example, your dataframe may contain the x,y coordinates for a series of points and you wish for them to be used unaltered as the x,y coordinates on the graph. Moreover, your dataframe may contain pre-calculated information about the quantiles, whiskers and outliers and you wish these to be used in the construction of a boxplot (rather than have the internals of ggplot perform the calculations on raw data).
Since geom_ and stats_ functions are coupled together, a geometric representation can be expressed from either a geom_ function OR a stats_ function. That is, you either:
specify a geom_ function that itself calls a stat_ function to provide the data for the geom function.
It does not really make any difference which way around you do this. For the remainder of this tutorial, we will directly engage the geom_ function for all examples.
The geom_ functions all have numerous arguments, many of which are common to all geoms_.
data - the data frame containing the data. Typically this is inherited from the ggplot function.
mapping - the aesthetic mapping instructions. Through the aesthetic mapping the aesthetic visual characteristics of the geometric features can be controlled (such as colour, point sizes, shapes etc). The aesthetic mapping can be inherited from the ggplot function. Common aesthetic features (mapped via a aes function) include:
alpha - transparency
colour - colour of the geometric features
fill - fill colour of geometric features
linetype - type of lines used in geometric features (dotted, dashed, etc)
size - size of geometric features such as points or text
shape - shape of geometric features such as points
weight - weightings of values
stat - the stat_ function coupled to the geom_ function
position - the position adjustment for overlapping objects
identity - leave objects were they are:
dodge - shift objects to the side to prevent overlapping
stack - stack objects on top of each other
fill - stack objects on top of each other and standardize each group to equal height
show.legend - whether a legend should be included
inherit.aes - whether to override any aesthetics from the ggplotfunction
The above characteristics that can be aesthetically mapped to data variables, can alternatively be set directly. For example, in the left hand code snippet below, the colour of the points is determined by the value of the specified variable. However, in the right hand code snippet, the colour of the points is set to be red. Notice, that in this later instance, the colour is specified outside of the aesthetic (aes) function.
The above figure (left side) illustrated the use of aesthetics to highlight different groups (or series) within the data (Type in that case). For some geoms it is important that they be able to operate on a single series of data to avoid strange artefacts. The next two figures will include a layer of lines. In the left hand figure, the lines are drawn across a single series (group) yielding a nonsense pattern. This is addressed in the right hand figure which specifies that separate lines should be drawn for each Plant (each plant is its own series or group).
In the above right, the series are defined by the group aesthetic. Alternatively, we could have mapped Plant to one of the other aesthetics (such as colour, linetype etc), in which case, each line would have a different visual appearance and a legend would have also been included.
Currently, there are a large number of available geoms_ and stat_ functions within the ggplot system. This tutorial is still a work in progress and therefore does not include all of them - I have focused on the more commonly used ones.
2.1 Primitive geoms
Primitive geoms are simple plotting shapes that typically represent direct mapping of data variables to the shapes - that is, they do not require specific statfunctions. Hence, all primitive geoms use stat_identity. Nevertheless, it is possible to have the shapes mapped via alternative stats functions is appropriate (see points).
Although this might seem pointless, it can be useful for forcing axes scales to conform to a particular format - since axes scales are determined by the first layer (which can be blank) defined in the sequence of expressions.
To help illustrate this, I will introduce a fabricated data set comprising the length (mm) of 10 day old frog tadpoles incubated at three different temperatures (Low, Medium, High).
Now imagine you wish to produce a scatterplot (with length mapped to the y-axis and day mapped to the x-axis) to explore these data. Since although temp is categorical, it is ordered, we would also like to plot a line representing the overall trend in tadpole length in relation to temperature. Doing so would introduce one of two problems:
lines can only be plotted when both x and y are mapped to continuous variables
in order to plot a line, we would need to convert temperature into a numeric variable in some way, however doing so would mean that the axes labels loose their meaning.
Using a geom_blank allows us to define a line and maintain useful axes labels. The second and third examples below will illustrate the problem and solution respectively.
Feature
geom
stat
position
Aesthetic parameters / Notes
Example plot
Blank layer
_blank
_identity
identity
x,y
ggplot(data=tadpole, aes(y = length, x = temp)) +geom_blank()
Blank layer
_blank
_summary
identity
x,y
ggplot(data=tadpole, aes(y = length, x =as.numeric(temp))) +geom_line(stat ='summary', fun = mean)
Blank layer
_blank
_summary
identity
x,y
ggplot(data=tadpole, aes(y = length, x = temp))+geom_blank() +geom_line(aes(x =as.numeric(temp)),stat ='summary', fun = mean)
geom_point draws points (scatterplot). Typically the stat used is stat_identity as we wish to use the values in two continuous vectors as the coordinates of each point.
The following list describes the mapping aesthetic properties associated with geom_point. The entries in bold are compulsory.
Show attributes
Parameter
geom_point
aesthetics
x - variable to map to the x-axis
✔
y - variable to map to the y-axis
✔
group - plot separate series without aesthetic differences
✔
alpha - transparency
✔
colour - colour of the points/lines
✔
fill - inner colour of points/shapes
✔
linetype - type of lines used to construct points/lines
✔
size - thickness of the line
✔
shape - the plotting symbol/character
✔
weight - weightings of values
✔
Feature
geom
stat
position
Aesthetic parameters / Notes
Example plot
points layer
_point
_identity
identity
x,y
geom_point forms the basis of various plots such as scatterplots, maps and others
ggplot(data=df, aes(y = y, x = x)) +geom_point()
means points layer
_point
_identity
identity
x,y,fun
plots points based on the values provided
ggplot(data=CO2, aes(x = conc, y = uptake)) +geom_point()
means points layer
_point
_summary
identity
x,y,fun
plots the result of the specified summary function
ggplot(data=CO2, aes(x = conc, y = uptake)) +geom_point(stat ="summary", fun = mean)
The plotting symbols are specified by either a number (index of a plotting symbol - see below) or a single character (printed literally).
The following list describes the mapping aesthetic properties associated with geom_text. The entries in bold are compulsory.
Show attributes
Parameter
geom_text
- x - variable to map to the x-axis
✔
- y - variable to map to the y-axis
✔
- label - text to use as labels
✔
- group - plot separate series without aesthetic differences
✔
- alpha - transparency
✔
- colour - colour of the points/lines
✔
- fill - inner colour of points/shapes
✔
- linetype - type of lines used to construct points/lines
✔
- shape - symbol shape for points
✔
- size - size of symbol
✔
- family - font family
✔
- fontface - bold, italic, normal etc
✔
- hjust - horizontal justification
✔
- vjust - vertical justification
✔
additional parameters
- parse - whether to parse labels into expressions (to include special characters)
FALSE
- nudge_x - horizontal adjustments to label positions
0
- nudge_y - vertical adjustments to label positions
0
- check_overlap - whether to plot text that overlaps other text in layer
FALSE
Feature
geom
stat
position
Aesthetic parameters / Notes
Example plot
Text layer
_text
_identity
identity
x,y,label
Text on a plot - useful for depicting the location of observations
ggplot(data=df, aes(y = y, x = x)) +geom_text(aes(label = z))
Text layer
_text
_identity
identity
x,y,label
Text on a plot - useful for depicting the location of observations
ggplot(data=CO2, aes(y = uptake, x = conc)) +geom_text(aes(label = Treatment))
Text layer
_text
_identity
identity
x,y,label
Text on a plot - useful for depicting the location of observations
Horizontal (hjust) and vertical (vjust) text justification controls are often a source of confusion and this is further exacerbated when combined with angle control. The following excellent demonstration from here provides a visual aid to understanding the use of these controls.
geom_path draws paths (line plots). Paths order the coordinates according to the order in the data frame (c.f. geom_line and geom_step)
Show attributes
Parameter
geom_path
- x - variable to map to the x-axis
✔
- y - variable to map to the y-axis
✔
- group - plot separate series without aesthetic differences
✔
- alpha - transparency
✔
- colour - colour of the points/lines
✔
- fill - inner colour of points/shapes
✔
- linetype - type of lines used to construct points/lines
✔
- shape - symbol shape for points
✔
- size - size of symbol
✔
additional parameters
- lineend - line end style (round, butt, squate)
‘butt’
- linejoin - line join style (round, mitre, bevel)
‘round’
- linemitre - line mitre limit
10
- arrow - arrow specification (grid::arrow())
NULL
Feature
geom
stat
position
Aesthetic parameters / Notes
Example plot
paths layer
_path
_identity
identity
x,y
geom_path draws lines connecting coordinates in the order present in the data frame
ggplot(data=df, aes(y = y, x = x)) +geom_path()
The simple line types available are highlighted in the following figure:
geom_polygon draws polygons with the coordinates ordered according to the order in the data frame.
Show attributes
Parameter
geom_polygon
- x - variable to map to the x-axis
✔
- y - variable to map to the y-axis
✔
- group - plot separate series without aesthetic differences
✔
- alpha - transparency
✔
- colour - colour of the points/lines
✔
- fill - inner colour of points/shapes
✔
- linetype - type of lines used to construct points/lines
✔
- shape - symbol shape for points
✔
- size - size of symbol
✔
additional parameters
- rule - determines how holes in polygons are treated
‘evenodd’
Feature
geom
stat
position
Aesthetic parameters / Notes
Example plot
polygon layer
_polygon
_identity
identity
x,y
geom_polygon draws polygons using coordinates in the order present in the data frame
ggplot(data=df, aes(y = y, x = x)) +geom_polygon()
geom_area draws areas under curves with the coordinates ordered according to the order in the data frame.
Show attributes
Parameter
geom_area
- x - variable to map to the x-axis
✔
- y - variable to map to the y-axis
✔
- group - plot separate series without aesthetic differences
✔
- alpha - transparency
✔
- colour - colour of the points/lines
✔
- fill - inner colour of points/shapes
✔
- linetype - type of lines used to construct points/lines
✔
- shape - symbol shape for points
✔
- size - size of symbol
✔
additional parameters
- outline.type - determines the type of outline to draw around area
‘both’
Feature
geom
stat
position
Aesthetic parameters / Notes
Example plot
area layer
_area
_identity
identity
x,y
geom_area draws areas under a curve using coordinates in the order present in the data frame
ggplot(data=df, aes(y = y, x = x)) +geom_area()
geom_ribbon draws ribbons (polygons) based on upper (max) and lower (min) levels of y associated with each level of x and are typically used to represent uncertainty in trends.
Show attributes
Parameter
geom_ribbon
- x - variable to map to the x-axis
✔
- y - variable to map to the y-axis
✔
- group - plot separate series without aesthetic differences
✔
- alpha - transparency
✔
- colour - colour of the points/lines
✔
- fill - inner colour of points/shapes
✔
- linetype - type of lines used to construct points/lines
✔
- shape - symbol shape for points
✔
- size - size of symbol
✔
additional parameters
- outline.type - determines the type of outline to draw around area
‘both’
Feature
geom
stat
position
Aesthetic parameters / Notes
Example plot
ribbon layer
_ribbon
_identity
identity
x,y
geom_ribbon draws ribbons on a plot - useful for depicting uncertainty (confidence/credibility) intervals
ggplot(data=df, aes(ymin = y -1, ymax = y +1, x = x)) +geom_ribbon()
ribbon layer
_ribbon
_identity
identity
x,y
geom_ribbon draws ribbons on a plot - useful for depicting uncertainty (confidence/credibility) intervals
BOD.lm <-lm(demand ~ Time, data = BOD)newdata <-with(BOD, data.frame(Time =seq(min(Time), max(Time),length =100)))newdata <- newdata %>%cbind(predict(BOD.lm, newdata = newdata,interval ='confidence'))ggplot(data=newdata) +geom_ribbon(aes(x = Time, ymin = lwr, ymax = upr))
ribbon layer
_ribbon
_identity
identity
x,y
geom_ribbon draws ribbons on a plot - useful for depicting uncertainty (confidence/credibility) intervals
geom_boxplot constructs boxplots. The values of the various elements of the boxplot (quantiles, whiskers etc) are calculated by its main pairing function (stat_boxplot). The following list describes the mapping aesthetic properties associated with geom_boxplot. The entries in bold are compulsory. Note that boxplots are usually specified via the geom_boxplotfunction which will engage the stat_boxplot to calculate the quantiles, whiskers and outliers. Therefore, confusingly, when calling geom_boxplot, the compulsory parameters are actually those required by stat_boxplot (unless you indicated to use stat_identity).
Show attributes
Parameter
geom_boxplot
stat_boxplot
aesthetics
x - variable to map to the x-axis
✔
✔
y - variable to map to the other axis
✔
✔
lower - value of the lower box line (25th percentile)
✔
middle - value of the middle box line (50th percentile)
✔
upper - value of the upper box line (75th percentile)
✔
ymin - value of lower whisker
✔
ymax - value of upper whisker
✔
group - plot separate series without aesthetic differences
✔
✔
alpha - transparency
✔
✔
colour - colour of the points/lines
✔
✔
fill - inner colour of points/shapes
✔
✔
linetype - type of lines used to construct points/lines
✔
✔
size - thickness of the line
✔
✔
weight - weightings of values
✔
✔
additional parameters
outlier.colour - color of symbols for outliers
NULL
NULL
outlier.fill - fill of symbols for outliers
NULL
NULL
outlier.shape - shape of symbols for outliers
NULL
NULL
outlier.size - size of symbols for outliers
NULL
NULL
outlier.stroke - colour of lines in symbols for outliers
NULL
NULL
outlier.alpha - transparency of symbols for outliers
NULL
NULL
notch - whether to notch the boxplots
FALSE
FALSE
notchwidth - width of notch
0.5
0.5
Computed variables
lower - value of the lower box line (25th percentile)
✔
middle - value of the middle box line (50th percentile)
✔
upper - value of the upper box line (75th percentile)
geom_density constructs smooth density distributions from continuous vectors. The actual smoothed densities are calculated by its main pairing function (stat_density). The following list describes the mapping aesthetic properties associated with geom_density and stat_density. The entries in bold are compulsory. Note that density plots are usually specified via the geom_densityfunction which will engage the stat_density. Therefore, confusingly, when calling geom_density, the compulsory paramaters are actually those required by stat_density (unless you indicated to use stat_identity).
Show attributes
Parameter
geom_density
stat_density
aesthetics
x - variable to map to the x-axis
✔
✔
group - plot separate series without aesthetic differences
✔
✔
alpha - transparency
✔
✔
colour - colour of the points/lines
✔
✔
fill - inner colour of points/shapes
✔
✔
linetype - type of lines used to construct points/lines
✔
✔
size - thickness of the line
✔
✔
weight - weightings of values
✔
✔
additional parameters
bw - bandwidth smoothing (either sd of kernel or name of function)
“nrd0”
“nrd0”
adjust - multiplicate bandwidth adjustment
1
1
kernel - density() kernel to use
“gaussian”
“gaussian”
n - number of equ-spaced points to estimate density
geom_violin constructs violin plots. Violin plots are a blend on boxplot and density plots in that they are a density plot mirrored across a central axis and displayed similarly to a boxplot. Since they are derived from density plots, geom_violin and its stat (stat_ydensity) have most of the same parameters as geom_density/stat_density. Violin plots are a useful way to present continuous distributions that have greater granularity than boxplots.
Show attributes
Parameter
geom_violin
stat_ydensity
aesthetics
x - variable to map to the x-axis
✔
✔
group - plot separate series without aesthetic differences
✔
✔
alpha - transparency
✔
✔
colour - colour of the points/lines
✔
✔
fill - inner colour of points/shapes
✔
✔
linetype - type of lines used to construct points/lines
✔
✔
size - thickness of the line
✔
✔
weight - weightings of values
✔
✔
additional parameters
bw - bandwidth smoothing (either sd of kernel or name of function)
“nrd0”
“nrd0”
adjust - multiplicate bandwidth adjustment
1
1
kernel - density() kernel to use
“gaussian”
“gaussian”
n - number of equ-spaced points to estimate density
512
512
trim - whether to trim the range of data
FALSE
FALSE
orientation - which axis (x or y) to operate on
NA
NA
Computed variables
density - density of points in bin
✔
count - density * number of points
✔
scaled density - density scaled to max of 1
✔
ndensity - density scaled to max of 1
✔
Feature
geom
stat
position
Aesthetic parameters / Notes
Example plot
violin
_violin
_ydensity
dodge
x
plot of quantiles, whiskers and outliers.
ggplot(diamonds) +geom_violin(aes(y = carat, x = carat), orientation ='x')
violin
_violin
_ydensity
dodge
y
plot of quantiles, whiskers and outliers.
ggplot(diamonds) +geom_violin(aes(y = carat, x = carat), orientation ='y')
conditional violin
_violin
_ydensity
dodge
y,x
plot of quantiles, whiskers and outliers.
ggplot(diamonds) +geom_violin(aes(y = carat, x = cut))
geom_qq constructs quantile-quantile (QQ) plots. QQ plots illustrate the quantiles of the sample distribution against the quantiles expected for the theoretical distribution. A straight line implies that the sample distribution matches the theoretical distribution well. Deviations (typically at the tails) imply a lack of match. To help assess deviations from linearity, the geom_qq_line/stat_qq_linefunction depicts the ideal match as a straight line for comparison.
Show attributes
Parameter
geom_qq
stat_qq
aesthetics
sample - variable to map to the x-axis
✔
✔
group - plot separate series without aesthetic differences
✔
✔
alpha - transparency
✔
✔
colour - colour of the points/lines
✔
✔
fill - inner colour of points/shapes
✔
✔
linetype - type of lines used to construct points/lines
✔
✔
size - thickness of the line
✔
✔
weight - weightings of values
✔
✔
additional parameters
distribution - function that calculates quantiles for a distribution
stats::qnorm
stats::qnorm
dparams - additional parameters for the distribution
this example will explore a Gamma theoretical distribution (rather than the default Gaussian). When specifying an alternative distribution, it may be necessary to supply certain distribution parameters. In this case, the appropriate shape parameter for the Gamma distribution is required. This is first estimated via the fitdistr()function from the MASS _package`.
geom_bar constructs barcharts. By default, the bins of each bar along with the associated bar heights are calculated by the stats_count function. The following list describes the mapping aesthetic properties associated with geom_bar and stats_count. The entries in bold are compulsory.
Show attributes
Parameter
geom_bar
stat_count
aesthetics
x or y - variable to map to the x/y-axis
✔
✔
group - plot separate series without aesthetic differences
✔
✔
alpha - transparency
✔
✔
colour - colour of the points/lines
✔
✔
fill - inner colour of points/shapes
✔
✔
linetype - type of lines used to construct points/lines
✔
✔
size - thickness of the line
✔
✔
weight - weightings of values
✔
✔
additional parameters
width - width of the bars
NULL
NULL
orientation - which axis (x or y) to operate on
NA
NA
Computed variables
count - number of points to bin
✔
prop - groupwise proportion
✔
Feature
geom
stat
position
Aesthetic parameters / Notes
Example plot
Bar plot
_bar
_count
stack
sample
ggplot(diamonds, aes(x = cut)) +geom_bar()
Stacked bar plot
_bar
_count
stack
sample
by default a viridis colour blind safe palette is applied.
ggplot(diamonds, aes(x = cut, fill = clarity)) +geom_bar()
Conditional bar plot
_bar
_count
dodge2
sample
by default a viridis colour blind safe palette is applied.
ggplot(diamonds, aes(x = cut, fill = clarity)) +geom_bar(position ="dodge2")
geom_dotplot draws dotplots of continuous data after binning the data. This geom is unusual in that it does not have an associated stat. Instead, it calls its own density function. Dotplots are really only useful for small datasets. The reason for this is that the counts in each bin are represented by circles (the size of which is determined by the bin width) and thus if the counts are high, the stack of circles will extend beyond the y-axis (unless the stack ratio is altered). Moreover, the y-axis scale is meaningless.
The following list describes the mapping aesthetic properties associated with geom_dotplot. The entries in bold are compulsory.
Show attributes
Parameter
geom_histogram
aesthetics
x - variable to map to the x-axis
✔
group - plot separate series without aesthetic differences
✔
alpha - transparency
✔
colour - colour of the points/lines
✔
fill - inner colour of points/shapes
✔
linetype - type of lines used to construct points/lines
✔
size - thickness of the line
✔
weight - weightings of values
✔
additional parameters
binwidth - (maximum) bin width
NULL
binaxis - the axis to bin along
“x”
method - method for binning
“dotdensity”
binpositions - method for determining the position of dots
“bygroup”
stackdir - which direction to stack dots
“up”
stackratio - how close to stack dots
1
dotsize - diameter of dot relative to binwidth
1
stackgroups - whether to stack dots across groups
FALSE
origin - origin of first bin
NULL
right - should bin intervals be closed on the right (a, b] or not [a, b)
TRUE
width - when binaxis is “y”, the spacing of dot stacks for dodging
The ggpairs()function is not actually part of the ggplot2package. Rather it is part of its own package (GGally). Nevertheless, this graphic, in which all pairs of variables are plotted in a matrix of panels can be a very useful way to explore the individual and relational characteristics of multiple variables simultaneously.
Show attributes
Parameter
geom_histogram
aesthetics
group - plot separate series without aesthetic differences
✔
alpha - transparency
✔
colour - colour of the points/lines
✔
fill - inner colour of points/shapes
✔
linetype - type of lines used to construct points/lines
✔
size - thickness of the line
✔
weight - weightings of values
✔
additional parameters
columns - which columns to include
1:ncol(data)
title,xlab,ylab - titles
NULL
upper,lower - functions for plots to appear in off diagonal panels
…
diag - function for plots to appear in diagonal panels
…
axisLabels - whether and how to display axis labels
c(“show”, “internal”, “none”)
columnLabels - label names to display
colnames(data[columns])
labeller - facet labeller function
“label_value”
switch - which (if any) facet labels to switch position
NULL
showStrips - whether to display all strips (panel banners)
NULL
legend - whether or position of legend
NULL
cardinality_threshold - max number of factor levels permitted
15
progress - whether to show a progress bar while computing (if >15 panels)
geom_line draws lines joining coordinates. Typically the stat used is stat_identity as we wish to use the values in two continuous vectors as the coordinates of each line segment. geom_line differs from geom_path in that the former first orders the data according to the x-axis.
Show attributes
Parameter
geom_line
aesthetics
x - variable to map to the x-axis
✔
y - variable to map to the other axis
✔
group - plot separate series without aesthetic differences
✔
alpha - transparency
✔
colour - colour of the points/lines
✔
fill - inner colour of points/shapes
✔
linetype - type of lines used to construct points/lines
✔
size - thickness of the line
✔
weight - weightings of values
✔
additional parameters
Computed variables
Feature
geom
stat
position
Aesthetic parameters / Notes
Example plot
line plot
_line
_identity
identity
x,y
scatterplot on only the first level of Plant.
ggplot(filter(CO2, Plant ==first(Plant))) +geom_line(aes(x = conc, y = uptake))
ggplot(data = CO2, aes(x = conc, y = uptake)) +geom_line(stat ="summary", fun = mean)
line plot
_line
_identity
identity
x,y
scatterplot of mean across all Plants.
ggplot(data = CO2, aes(x = conc, y = uptake)) +geom_line(stat ="summary", fun = mean, aes(color = Type))
geom_smooth draws smooths lines (and 95% confidence intervals) through data clouds. Typically the stat used is stat_smooth which in turn engages one of the available smoothing methods (e.g. lm, glm, gam, loess or rlm).
Show attributes
Parameter
geom_smooth
geom_smooth
aesthetics
x - variable to map to the x-axis
✔
✔
y - variable to map to the other axis
✔
✔
group - plot separate series without aesthetic differences
✔
✔
alpha - transparency
✔
✔
colour - colour of the points/lines
✔
✔
fill - inner colour of points/shapes
✔
✔
linetype - type of lines used to construct points/lines
✔
✔
size - thickness of the line
✔
✔
weight - weightings of values
✔
✔
additional parameters
method - smoothing method (modelling function)
NULL
NULL
formula - formula to use in smoothing function
NULL
NULL
se - whether to display confidence intervals
TRUE
TRUE
n - number of points at which to evaluate the smoother
80
span - degree of smoothing for loess smoother
0.75
fullrange - whether the fit should span full range (or just data)
FALSE
method.args - additional arguments passed on to modelling function
list()
Computed variables
y or x - the predicted value
✔
ymin or xmin - lower confidence interval
✔
ymax or xmax - upper confidence interval
✔
se - standard error
✔
Feature
geom
stat
position
Aesthetic parameters / Notes
Example plot
line plot
_line
_identity
identity
x,y
scatterplot on only the first level of Plant.
ggplot(filter(CO2, Plant ==first(Plant))) +geom_smooth(aes(x = conc, y = uptake))
scatterplot of each Plant level coloured separately.
ggplot(data = BOD, aes(x = Time, y = demand)) +geom_smooth(method ="lm", formula ="y ~ x")
line plot
_line
_identity
identity
x,y
scatterplot of each Plant level coloured separately.
ggplot(data = BOD, aes(x = Time, y = demand)) +geom_smooth(method ="lm", formula ="y ~ x")
geom_tile constructs heat maps given x,y coordinates and a z value to associate with the fill of each tile. Note, the data must be a complete grid.
Show attributes
Parameter
geom_tile
aesthetics
x - variable to map to the x-axis
✔
y - variable to map to the other axis
✔
group - plot separate series without aesthetic differences
✔
alpha - transparency
✔
colour - colour of the points/lines
✔
fill - inner colour of points/shapes
✔
linetype - type of lines used to construct points/lines
✔
size - thickness of the line
✔
weight - weightings of values
✔
additional parameters
linejoin - line join style
“mitre”
Computed variables
Feature
geom
stat
position
Aesthetic parameters / Notes
Example plot
tile plot
_tile
_identity
identity
x,y
heatmap of 2d density.
ggplot(faithfuld, aes(y = eruptions, x = waiting)) +geom_tile(aes(fill = density))
geom_raster is similar to geom_tile in that it constructs heat maps given x,y coordinates and a z value to associate with the fill of each tile. However, unlike geom_tile, a full grid is not required as geom_raster is able to interpolate over the grid. The interpolation can also smooth out the grid surface. This interpolation does make geom_raster slower than geom_tile.
Show attributes
Parameter
geom_raster
aesthetics
x - variable to map to the x-axis
✔
y - variable to map to the other axis
✔
group - plot separate series without aesthetic differences
✔
alpha - transparency
✔
colour - colour of the points/lines
✔
fill - inner colour of points/shapes
✔
linetype - type of lines used to construct points/lines
✔
size - thickness of the line
✔
weight - weightings of values
✔
additional parameters
hjust - line join style
0.5
vjust - line join style
0.5
interpolate - line join style
FALSE
Computed variables
Feature
geom
stat
position
Aesthetic parameters / Notes
Example plot
raster plot
_raster
_identity
identity
x,y
heatmap of 2d density.
ggplot(faithfuld, aes(y = eruptions, x = waiting)) +geom_raster(aes(fill = density))
geom_contour constructs contour maps given x,y coordinates and a z value from which to calculate each contour.
Show attributes
Parameter
geom_contour
stat_contour
aesthetics
x - variable to map to the x-axis
✔
✔
y - variable to map to the y axis
✔
✔
z - variable to map to the z axis
✔
✔
group - plot separate series without aesthetic differences
✔
✔
alpha - transparency
✔
✔
colour - colour of the points/lines
✔
✔
fill - inner colour of points/shapes
✔
✔
linetype - type of lines used to construct points/lines
✔
✔
size - thickness of the line
✔
✔
weight - weightings of values
✔
✔
additional parameters
bins - number of contour bins
NULL
NULL
binwidth - width of contour bins
NULL
NULL
breaks - numer of contour bins (alternative to bins)
NULL
NULL
lineend - line end style
“butt”
“butt”
linejoin - line join style
“round”
“round”
linemitre - line mitre style
10
10
Computed variables
level - bin boundaries
✔
level_low, level_high, level_mid - bin boundaries per band
✔
nlevel - height of contour, scaled to max of 1
✔
piece - contour piece
✔
Feature
geom
stat
position
Aesthetic parameters / Notes
Example plot
contour plot
_contour
_identity
identity
x,y
contour plot of 2d density in relation to eruption and waiting times.
ggplot(faithfuld, aes(y = eruptions, x = waiting)) +geom_contour(aes(z = density))
geom_contour constructs contour maps given x,y coordinates and a z value from which to calculate each contour.
Show attributes
Parameter
geom_contour_filled
stat_contour_filled
aesthetics
x - variable to map to the x-axis
✔
✔
y - variable to map to the y axis
✔
✔
z - variable to map to the z axis
✔
✔
group - plot separate series without aesthetic differences
✔
✔
alpha - transparency
✔
✔
colour - colour of the points/lines
✔
✔
fill - inner colour of points/shapes
✔
✔
linetype - type of lines used to construct points/lines
✔
✔
size - thickness of the line
✔
✔
weight - weightings of values
✔
✔
additional parameters
bins - number of contour bins
NULL
NULL
binwidth - width of contours
NULL
NULL
breaks - sets contour breaks (alternative to bins)
NULL
NULL
lineend - line end style
“butt”
“butt”
linejoin - line join style
“round”
“round”
linemitre - line mitre style
10
10
Computed variables
level - bin boundaries
✔
nlevel - height of filled_contour, scaled to max of 1
✔
piece - filled_contour piece
✔
Feature
geom
stat
position
Aesthetic parameters / Notes
Example plot
filled_contour plot
_filled_contour
_identity
identity
x,y
filled contour plot of 2d density in relation to eruption and waiting times.
ggplot(faithfuld, aes(y = eruptions, x = waiting)) +geom_contour_filled(aes(z = density))
geom_vline and geom_hline draw vertical and horizontal lines respectively. These are particularly useful for:
marking cross-hairs in ordination plots
providing extended guides along either axes.
geom_abline is used for adding linear regression lines in the form of y = bx + a where b is the slope and a is the intercept (this is where the ab in abline comes from).
Show attributes
Parameter
geom_vline
geom_hline
geom_abline
aesthetics
xintercept - x-axis coordinate crossed by vline
✔
yintercept - y-axis coordinate crossed by hline
✔
intercept - y-axis coordinate crossed by abline
✔
slope - slope (gradient) of abline
✔
group - plot separate series without aesthetic differences
✔
alpha - transparency
✔
colour - colour of the points/lines
✔
fill - inner colour of points/shapes
✔
linetype - type of lines used to construct points/lines
geom_segment draws segments (separate lines) joining pairs of coordinates. These can be useful to represent vectors (e.g. wind speed and direction on a map) or movement, differences etc (particularly if given an arrow head).
Show attributes
Parameter
geom_segment
aesthetics
x - x-axis coordinates for the start of lines
✔
y - y-axis coordinates for the start of lines
✔
xend - x-axis coordinates for the end of lines
✔
yend - y-axis coordinates for the end of lines
✔
group - plot separate series without aesthetic differences
✔
alpha - transparency
✔
colour - colour of the points/lines
✔
fill - inner colour of points/shapes
✔
linetype - type of lines used to construct points/lines
✔
size - thickness of the line
✔
weight - weightings of values
✔
additional parameters
arrow - specification of arrow heads
NULL
arrow.fill - fill colour of arrow head
NULL
lineend - style of line end
“butt”
linejoin - style of line join
“round”
Computed variables
Feature
geom
stat
position
Aesthetic parameters / Notes
Example plot
line segements
_segement
_identity
identity
x,y
BOD.lm <-lm(demand ~ Time, data = BOD)BOD$fitted <-fitted(BOD.lm)BOD$resid <-resid(BOD.lm)ggplot(BOD)+geom_segment(aes(x=Time,y=demand, xend=Time,yend=fitted),arrow =arrow(length=unit(0.5, "cm"), ends ="first"))
expand - whether to add a small expansion to the axes to ensure geoms and axes do not overlapp
TRUE
expand - whether or not to provide a warning when the default coordinate system is being replaced
FALSE
clip - whether or not to clip plotting to within the plotting margins (“on”) or to extend into the margins
“on”
Feature
coord
Notes
Example plot
Cartesian coordinates
_cartesian
ggplot(data = BOD, aes(x = Time, y = demand)) +geom_line() +coord_cartesian()
Show attributes
Parameter
geom_segment
theta - map angle to either ‘x’ or ‘y’
‘x’
start - offset (applied clockwise by default) from 12 o’clock in radians
0
direction - which direction (‘clockwise’: 1 or ‘anticlockwise’: -1)
1
clip - whether or not to clip plotting to within the plotting margins (“on”) or to extend into the margins
“on”
Feature
coord
Notes
Example plot
Polar coordinates
_polar
ggplot(data = BOD, aes(x = Time, y = demand)) +geom_line() +coord_polar()
Show attributes
Parameter
geom_segment
xlim - limits for the x axis
NULL
ylim - limits for the y axis
NULL
expand - whether to add a small expansion to the axes to ensure geoms and axes do not overlapp
TRUE
expand - whether or not to provide a warning when the default coordinate system is being replaced
FALSE
clip - whether or not to clip plotting to within the plotting margins (“on”) or to extend into the margins
“on”
Feature
coord
Notes
Example plot
Flip coordinates
_flip
ggplot(data = BOD, aes(x = Time, y = demand)) +geom_line() +coord_flip()
Show attributes
Parameter
geom_segment
ratio - aspect ratio (y/x)
1
xlim - limits for the x axis
NULL
ylim - limits for the y axis
NULL
expand - whether to add a small expansion to the axes to ensure geoms and axes do not overlapp
TRUE
expand - whether or not to provide a warning when the default coordinate system is being replaced
FALSE
clip - whether or not to clip plotting to within the plotting margins (“on”) or to extend into the margins
“on”
Feature
coord
Notes
Example plot
Fixed coordinates
_fixed
ggplot(data = BOD, aes(x = Time, y = demand)) +geom_line() +coord_fixed()
Fixed ratio of coordinates
_fixed
ggplot(data = BOD, aes(x = Time, y = demand)) +geom_line() +coord_fixed(ratio =0.5)
Show attributes
Parameter
geom_segment
x - the transformation to apply to the x-axis
“identity”
y - the transformation to apply to the y-axis
“identity”
xlim - limits for the x axis
NULL
ylim - limits for the y axis
NULL
expand - whether or not to provide a warning when the default coordinate system is being replaced
FALSE
clip - whether or not to clip plotting to within the plotting margins (“on”) or to extend into the margins
“on”
Feature
coord
Notes
Example plot
Transformed coordinates
_trans
ggplot(data = BOD, aes(x = Time, y = demand)) +geom_line() +coord_trans(x ="log10")
Trans ratio of coordinates
_trans
ggplot(data = BOD, aes(x = Time, y = demand)) +geom_line() +coord_trans(x = scales::exp_trans(2))
3.2 Altering axes scales via the coordinate system
3.2.1 Zooming
Modifying scales with coords_ affects the zoom on the graph. That is, it defines the extent and nature of the axes coordinates. By contrast, altering limits via scale_ routines will alter the scope of data included in a manner analogous to operating on a subset of the data.
To illustrate this, lets produce a linear smoother for the BOD data. To help us appreciate the differences between coords_ and scale_ when altering one axis (x in this case), we will ensure that all each plot has the same range of the other axis (y) and that the aspect ratio for the axes are all the same.
g <-ggplot(data = BOD, aes(x = Time, y = demand)) +geom_smooth(method ="lm") +geom_point()
Show default plot
g +coord_fixed(ratio =0.1, ylim =c(-5, 35))
Scale via coords scale
g +coord_fixed(ratio =0.1,ylim =c(-5, 35),xlim =c(2, 6))
Scale via scale scale
g +coord_fixed(ratio =0.1,ylim =c(-5, 35)) +scale_x_continuous(limits =c(2, 6))
Notice that the left hand figure (that restricts the x-axis scale) simply zooms in on the plot thereby cutting some of the data off. By contrast, scale_ (right hand figure) removes data that are outside the range and thus also alters the output of the smoother (linear model).
3.2.2 Geom re-scaling
In addition to altering the zoom of the axes, axes (coordinate system) scales can be transformed to other scales via the coord_transfunction. Such transformations of the coordinate system take place after statistics have been calculated and geoms derived. Therefore the shape of geoms are altered.
To illustrate the difference between coord_trans and scale_, lets create a fabricated data set of 50 points in which y is an exponential function of x (with noise).
set.seed(1)n<-50dat <-data.frame(x =exp((1:n+rnorm(n,sd=2))/10), y =1:n+rnorm(n,sd=2))g <-ggplot(data = dat, aes(x = x, y = y))
Linear scales
coord_trans
scales_
g +geom_point()
g +geom_point() +coord_trans(x="log10")
g +geom_point() +scale_x_continuous(trans="log10")
g +geom_point() +geom_smooth(method="lm")
g +geom_point() +geom_smooth(method="lm") +coord_trans(x="log10")
g +geom_point() +geom_smooth(method="lm") +scale_x_continuous(trans="log10")
In the above, the log10transformer function was applied to either the coordinates or the axes scales. More information about this and other transformer functions is provided in the scales section.
4 Scales
The idea of scales is that you present the plotting engine with data or characteristics in one scale and use the various scale_functions to convert those data into another scale. In the grammar of graphics, scales are synonymous for units of data, colors, shapes, sizes etc of plotting features and the axes and guides (legends) provide a visual cue for what the scales are. For example:
you might include data that ranges from 10 to 20 units, yet you wish to produce a plot that uses a restricted range of 12-16.
you have presented grouped data (data with multiple trends) and instructed the graphing engine to assign different colour codes to each trend. You can then define a colour scale to adjust the exact colours rendered.
similarly, you might have indicated how plotting symbol shape and size are to be distinguished in your data set. You can then assign scales that define the exact shapes and symbol sizes rendered.
Technically, scales determine how attributes of the data are mapped into aesthetic geom properties. The majority of geom’s (geometric objects) have the following aesthetic properties:
Parameter
x - the x position (coordinates) of the geom
y - the y position (coordinates) of the geom
size - size of the geom (e.g. the size of points, thickness of lines)
shape - the shape of the geom
linetype - the type of line to use for the geom’s outline (solid, dashed, etc)
colour - the colour of the geom’s outline
fill - the colour of the geom’s fill (inside colour)
alpha - the transparency of the geom (0 = transparent, through to 1 = opaque)
In turn, each of these properties are mapped to a scale - the defaults of which are automatically selected according to what is appropriate for the sort of data. For example, if the variable mapped to an axis is continuous, the a continuous scale is the default. If the variable mapped to an axis is categorical, then a discrete scale is the default. And so on…
The following table describes the typical scale associated with each of the major variable types:
Variable type
Scale
numeric - continuous data
_continuous - data mapped onto a continuous scale
numeric - continuous data
_log10 - data mapped onto a log (base 10) continuous scale
numeric - continuous data
_sqrt - data mapped onto a sqrt continuous scale
numeric - continuous data
_reverse - data mapped onto a reverse continuous scale
numeric - continuous data
_binned - data mapped onto a binned (discretized) scale
factor - categorical data
_discrete - data mapped onto a categorical scale
date - date data
_date - data mapped according to dates
POSIXct - date/time data
_datetime - data mapped according to date/time
POSIXct - date/time data
_time - data mapped according to date/time
Some properties, such as colour also have additional scales that are specific to the characteristic. The scales effect not only the characteristics of the geoms, they also effect the guides (legends) that accompany the geoms.
Scaling functions comprise the prefix scale_, followed by the name of an aesthetic property and suffixed by the type of scale. Hence a function to manually define a colour scale would be scale_colour_manual.
name - a title applied to the scale. In the case of scales for x and y (the x,y coordinate of geoms), the name is the axis title. For all other scales, the name is the title of the guide (legend).
Explore
Feature
Notes
Example plot
Simple axis title
Provide a more meaningful axis title for the x-axis
More elaborate titles that include special characters and mathematical notations are supported via expression()’s. For more info on plotting mathematical expressions, run demo(plotmath).
Note the use of the ~ character everywhere that a space would appear. That is because an expression cannot have spaces.
breaks - the increments on the guide. For scale_x_ and scale_y_, breaks are the axis tick locations. For all other scales, the breaks indicate the increments of the characteristic in the legend (e.g. how many point shapes are featured in the legend).
Explore
By default, pretty breaks will be used for continuous (including dates) data. It is possible to define your own breaks.
For discrete data, breaks should return a vector of values that correspond to categorical levels (in the order of the levels). In the following example, only one of the Treatment groups has a tick mark.
labels - the labels given to the increments on the guide. For scale_x_ and scale_y_, labels are the axis tick labels. For all other scales, the labels are the labels given to items in the legend.
Explore
The scalespackage has a number of label formatting functions. These all start with label_
Feature
Notes
Example plot
User defined tick labels
Format x-axis tick labels to include comma’s every multiple of 1000
Format y-axis labels to include percentage sign. By default, the label_percentfunction also assumes that the data mapped to the scale are proportions (that is the values are between 0 and 1). So by default, the function will multiple (scale) the values by 100. In this case, since the y-axis values are all between 0 and 100, we indicate that they should be multiplied by 1 (e.g. no change).
Format y-axis labels to include percentage sign. On this occasion we will derive a new variable to map to the y-axis. These values are in the range from 0 to 1, so we do want to scale (multiply) by 100. We will also adjust the accuracy (number to round to). In this case, we will indicate that it should round to the nearest 1 (whole number).
CO2 <- CO2 %>%mutate(percent = uptake/100)ggplot(data = CO2, aes(x = conc, y = percent)) +geom_point() +scale_y_continuous(labels = scales::label_percent(accuracy =1))
User defined tick labels
Format the x-axis ticks to append ‘.spp’ after each Type and make each tick mark italic. To apply any mathematical notation (including italic formatting) we must use a label formatter that parses expressions. We will define the format of the labels first in the dataset.
CO2 <- CO2 %>%mutate(species =paste0('italic(', Type, '.spp)'))ggplot(data = CO2, aes(x = species, y = uptake)) +geom_point() +scale_x_discrete(labels = scales::label_parse())
This can also include categorical data - in this case, we are adding an additional category. Although this category does not appear in the data, it may appear in other data for which you are producing similar plots and you wish to maintain the same legends and colour palettes.
There is also a general log transform that allows you to indicate the logarithmic base. When using the log_transfunction, it is a good idea to also use a related function to control the position of tick mark breaks.
Add a secondary y-axis and alter its scale. Note, by design, ggplot only permits secondary axes that are derived from the primary axis (are pure re-scale) - it is not possible (without unsupported effort) to represent an additional variable.
The scale_size_ scales control the size of geoms (such as the size of points).
Show attributes
Parameter
_size
_size_area
_size_binned
range - range (min, max) of symbol sizes
c(1, 6)
c(1,6)
max_size - maximum size of symbols
6
n.breaks - approximate number of major breaks
NULL
nice.breaks - whether to use nice breaks
TRUE
Feature
scale
Notes
Example plot
Scale size
_size
Scale the size according to a continuous variable. The size of the symbols will be proportional to the value in the variable and the guide (legend) will provide an indication of the symbol sizes correspondingto a set of values.
Scale the size according to a continuous variable that has been binned. In this case, as there are five bins, the symbols will be of one of five sizes (rather than a continuum).
Colour (or color) is used to represent the colour of lines and solid symbols (e.g. the outer color of shapes). By contrast, fill is used to represent the filling (inner) colour of shapes.
Colors can be specified by either:
a name of one of the 657 named colors
Color names
The names of all the available colors can be obtained via:
colors()
Below is a visual representation of those colours and their names (although you are going to need exceptional eye sight to read the names ;)).
hexedecimal codes (e.g. #FF0000 is ‘red’). This can also be an eight digit code in which case the last two digits represent alpha (e.g. #FF000050 is 50% transparent red).
RGB color codes
a colour palette
Palettes
colorspace::hcl_palettes(plot =TRUE, n =5)
RColorBrewer::display.brewer.all()
Show attributes
Parameter
default
default
_continuous
_binned
type - the type of colour scale
‘gradient’
(‘gradient’, ‘viridis’)
_gradient
gradient2
low - colour of low end of gradient
‘#132B43’
muted(“red”)
high - colour of high end of gradient
‘#56B1F7’
muted(“blue”
space - colour space
‘Lab’
‘Lab’
mid - colour of mid point in gradient
‘white’
midpoint - data value associated with middle of gradient
0
_gradientn
colours - vector of colours
values - position associated with eaah colour
_brewer
_distiller
type - type of color scale (‘div’, ‘seq’ or ‘qual’)
‘seq’
‘seq’
palette - name or number of colorbrewer palette
1
1
direction - order of colours from the palette
1
-1
Feature
scale
Notes
Example plot
Scale colour type
_colour_gradient
Manually define a colour scale that gradually transitions from red to blue.
ggplot(data = CO2, aes(x = conc, y = uptake)) +geom_point(aes(colour = uptake)) +scale_colour_gradient(low ='red', high ='blue')
Scale colour type
_colour_gradient2
Manually define a colour scale that gradually transitions from red to blue via a white middle.
ggplot(data = CO2, aes(x = conc, y = uptake)) +geom_point(aes(colour = uptake)) +scale_colour_gradient2(low ='red', mid ="white", high ='blue',midpoint =25)
Scale colour type
_colour_gradientn
Manually define a colour scale that gradually transitions along a pre-defined colour palette.
Alpha (transparency) determined manually for a categorical variable(s).
CO2 <- CO2 %>%mutate(cUptake =cut(uptake,breaks =c(0,15,30,46),labels =c("red", "#00AA00", 1)))ggplot(data = CO2, aes(x = conc, y = uptake)) +geom_point(aes(alpha = cUptake)) +scale_alpha_manual(values =c(0.3, 0.6, 0.95))
Scale alpha type
_alpha_identity
Alpha (transparency) determined manually for a categorical variable(s).
CO2 <- CO2 %>%mutate(cUptake =cut(uptake,breaks =c(0,15,30,46),labels =c(0.3, 0.6, 0.95)),cUptake =as.numeric(as.character(cUptake)))ggplot(data = CO2, aes(x = conc, y = uptake)) +geom_point(aes(alpha = cUptake)) +scale_alpha_identity()
5 Facets (panels)
Faceting splits the data up into a matrix of panels on the basis of one or more categorical vectors. Since facets display subsets of the data, they are very useful for examining trends in hierarchical designs. There are two faceting function, that reflect two alternative approaches:
facet_wrap(~cell) - creates a set of panels based on a factor and wraps the panels into a 2-d matrix. cell represents a categorical vector or set of categorical vectors
facet_wrap(row~column) - creates a set of panels based on a factor and wraps the panels into a 2-d matrix. row and column represents the categorical vectors used to define the rows and columns of the matrix respectively
Wrapped and gridded facets share a number of parameters:
facet - variables defining rows/columns of the grid. This can be either a formula or vector. Note, this is not deprecated for facet_grid
nrow - number of grid rows
ncol - number of grid columns
scales - one of “fixed” (default, axes the same across all panels), “free” (axes unique per panel), “free_x” (x-axes free), “free-y” (y-axes free)
Explore
g <-ggplot(data = CO2, aes(x = conc, y = uptake)) +geom_point()
Fixed axes scales
Free axes scales
g +facet_wrap(~Plant, scales='fixed')
g +facet_wrap(~Plant, scales='free')
as.table - if TRUE (default), arranged from top left to bottom right, otherwise, from bottom left to top right
drop - whether to drop factor combinations that lack data (default TRUE)
shrink - whether to shrink axes scales to match applied statistics (TRUE, default) or raw data (FALSE)
labeller - a function that determines the format of facet (strip) labels (default, ‘label_value’)
Explore
g <-ggplot(data = CO2, aes(x = conc, y = uptake)) +geom_point()
Default labelling
Append variable name to labels
g +facet_wrap(~Plant,labeller ='label_value')
g +facet_wrap(~Plant,labeller ='label_both')
Wrap long labels
Add special characters/formatting to labels
CO2 <- CO2 %>%mutate(Species =paste0('Plant from ', Type))g <-ggplot(data = CO2,aes(x = conc, y = uptake)) +geom_point()g +facet_wrap(~Species,labeller =label_wrap_gen(5))
CO2 <- CO2 %>%mutate(species =paste0('italic(', Type,'.spp)'))g <-ggplot(data = CO2,aes(x = conc, y = uptake)) +geom_point()g +facet_wrap(~species,labeller ='label_parsed')
switch - alternative positions for the panel strips (‘x’: move top labels to bottom, ‘y’: move right labels to left, ‘both’: move both top and right labels to bottom and left)
dir - direction in which to wrap panels (‘h’: horizontally’, ‘v’: vertically)
Explore
g <-ggplot(data = CO2, aes(x = conc, y = uptake)) +geom_point()
Wrap panels horizontally
Wrap panels vertically
g +facet_wrap(~Plant)
g +facet_wrap(~Plant, dir ='v')
strip.position - position of the panel label (‘top’,‘bottom’, ‘left’, ‘right’)
Explore
g <-ggplot(data = CO2, aes(x = conc, y = uptake)) +geom_point()
Along with these pre-fabricated themes, it is possible to create your own theme. This is done via the theme() function. Non-data themable elements comprise of either a line, rectangle or text. Therefore, they can all be modified via one of the following functions:
element_blank() - remove the element
element_line() - set the properties of a line
element_rect() - set the properties of a rectangle
element_text() - set the properties of text
The spacing around elements is defined by margin(). The sizes of elements can be directly supplied or defined relative to the size of the parent element via the rel() function.
Show attributes
Parameter
_line
_rect
_text
inherit.blank -
FALSE
FALSE
FALSE
fill - element fill colour
NULL
colour - line/border colour
NULL
NULL
NULL
size - line/border line size (mm) or font size (pt)
NULL
NULL
NULL
linetype - line type (1:8, name or hex string)
NULL
NULL
lineend - line end style (round, butt, square)
NULL
arrow - arrow specification
NULL
family - font family
NULL
face - font face (plain, italic, bold, bold.italic)
NULL
hjust - horizontal justification (in [0, 1])
NULL
vjust - vertical justification (in [0, 1])
NULL
angle - angle (in [0, 360])
NULL
lineheight - height of a line of text
NULL
margin - spacing around text (see margin())
NULL
debug - if TRUE draws a rectangle behind text and a point at anchor
NULL
To get an appreciation of the theme elements controlled by a theme, enter the associated function at the command prompt (note, most themes build upon the default ‘grey’ theme):
---title: Grammar of graphics (ggplot2 and friends)author: "Murray Logan"date: "`r format(Sys.time(), '%d %B, %Y')`"format: html: toc: true toc-float: true page-layout: full number-sections: true number-depth: 3 embed-resources: true code-fold: false code-tools: true code-summary: "Show the code" code-line-numbers: true code-block-border-left: "#ccc" code-copy: true highlight-style: atom-one theme: [default, ../resources/tut-style.scss] css: ../resources/tut-style.csscrossref: fig-title: '**Figure**' fig-labels: arabic tbl-title: '**Table**' tbl-labels: arabicengine: knitrbibliography: ../resources/references.biboutput_dir: "docs"---```{r setup, include=FALSE,warning=FALSE, message=FALSE}knitr::opts_chunk$set(echo = TRUE,warning=FALSE, message=FALSE, cache = TRUE, comment = "")options(tinytex.engine = 'xelatex')cleanRmdInput <- function(x) {#x <- gsub("```\\{r","```markdown\n`r ''```\\{r",x)x <- gsub("^```$","`` `",x) # the Makefile will then change this back to ``` after pandocx}library(tidyverse)library(pander)FIG_PATH <- "05_grammar_of_graphics_files/figure-html/"if (!dir.exists(FIG_PATH)) dir.create(FIG_PATH)``````{r blank_template, echo=FALSE}blank_theme <- theme_bw() + theme(plot.margin = margin(0, unit = 'cm'), axis.title = element_blank(), axis.text = element_blank())```This Tutorial has been thrown together a little hastily and istherefore not very well organised - sorry! Graphical features aredemonstrated either via tables of properties or as clickable graphicsthat reveal the required R code. Click on a graphic to reveal/togglethe source code or to navigate to an expanded section.This tutorial is intended to be viewed sequentially. It begins withthe basic ggplot framework and then progressively builds up more andmore features as default elements are gradually replaced to yeild morecustomized graphics.Having said that, I am going to start with a sort of _showcase_ ofgraphics which should act as quick navigation to entire sectionsdevoted to the broad series of graphs related to each of the featuredgraphics. I have intentionally titled each graph according to themain feature it encapsulates rather than any specific functions thatare used to produce the features as often a single graphic requires acombination of features and thus functions. Furthermore, the grammarof graphics specifications are sufficiently unfamiliar to many thatthe relationships between the types of graphical features a researcherwishes to produce and the specific syntax required to achieve thedesired result can be difficult to recognise.Each graphic is intended to encapsulate a broad series of relatedgraph types.::: {.panel-tabset .tabset-faded}## Primatives<div class = "gallery">[Blank![](05_grammar_of_graphics_files/figure-html/ggplotBlank-1.png){class="thumb"}](#Gblank)[Points![](05_grammar_of_graphics_files/figure-html/ggplotPoint-1.png){class="thumb"}](#Gpoints) [Text![](05_grammar_of_graphics_files/figure-html/ggplotText-1.png){class="thumb"}](#Gtext)[Paths![](05_grammar_of_graphics_files/figure-html/ggplotPath-1.png){class="thumb"}](#Gpaths)[Polygons![](05_grammar_of_graphics_files/figure-html/ggplotPolygon-1.png){class="thumb"}](#Gpolygons)[Area![](05_grammar_of_graphics_files/figure-html/ggplotArea-1.png){class="thumb"}](#Gareas)[Ribbons![](05_grammar_of_graphics_files/figure-html/ggplotRibbons-1.png){class="thumb"}](#Gribbons)</div>## Visualising distributions<div class = "gallery">[Boxplots![](05_grammar_of_graphics_files/figure-html/ggplotBoxplot-1.png){class="thumb"}](#Gboxplots)[Boxplots![](05_grammar_of_graphics_files/figure-html/ggplotBoxplotP-1.png){class="thumb"}](#Gboxplots)[Histograms![](05_grammar_of_graphics_files/figure-html/ggplotHistogram-1.png){class="thumb"}](#Ghistograms)[Histograms![](05_grammar_of_graphics_files/figure-html/ggplotHistogramP-1.png){class="thumb"}](#Ghistograms)[Density plots![](05_grammar_of_graphics_files/figure-html/ggplotDensity-1.png){class="thumb"}](#Gdensity-plots)[Density plots![](05_grammar_of_graphics_files/figure-html/ggplotDensityP-1.png){class="thumb"}](#Gdensity-plots)[Violin plots![](05_grammar_of_graphics_files/figure-html/ggplotViolin-1.png){class="thumb"}](#Gviolin-plots)[Violin plots![](05_grammar_of_graphics_files/figure-html/ggplotViolinP-1.png){class="thumb"}](#Gviolin-plots)[QQ plots![](05_grammar_of_graphics_files/figure-html/ggplotQQ-1.png){class="thumb"}](#Gqq-plots)[QQ plots![](05_grammar_of_graphics_files/figure-html/ggplotQQP-1.png){class="thumb"}](#Gqq-plots)[Bar plots![](05_grammar_of_graphics_files/figure-html/ggplotBar-1.png){class="thumb"}](#Gbar-plots)[Bar plots![](05_grammar_of_graphics_files/figure-html/ggplotBarS-1.png){class="thumb"}](#Gbar-plots)[Dotplots![](05_grammar_of_graphics_files/figure-html/ggplotDot-1.png){class="thumb"}](#Gdot-plots)[Dotplots![](05_grammar_of_graphics_files/figure-html/ggplotDotP-1.png){class="thumb"}](#Gdot-plots)[SPLOM![](05_grammar_of_graphics_files/figure-html/ggplotScatterplotMatrix-1.png){class="thumb"}](#Gscatterplot-matrix)</div>## Visualising trends<div class = "gallery">[Scatterplots![](05_grammar_of_graphics_files/figure-html/ggplotScatterplot-1.png){class="thumb"}](#Gscatterplots)[Line plots![](05_grammar_of_graphics_files/figure-html/ggplotLine-1.png){class="thumb"}](#Gline-plots)[Smoothers![](05_grammar_of_graphics_files/figure-html/ggplotSmooth-1.png){class="thumb"}](#Gsmoother-plots)[Tiles![](05_grammar_of_graphics_files/figure-html/ggplotTile-1.png){class="thumb"}](#Gtiles)[Rasters![](05_grammar_of_graphics_files/figure-html/ggplotRaster-1.png){class="thumb"}](#Grasters)[Contours![](05_grammar_of_graphics_files/figure-html/ggplotContour-1.png){class="thumb"}](#Gcontours)[Filled contour![](05_grammar_of_graphics_files/figure-html/ggplotFilledContour-1.png){class="thumb"}](#Gfilled-contour)</div>## Uncertainty<div class = "gallery">[Error bars![](05_grammar_of_graphics_files/figure-html/ggplotErrorbar-1.png){class="thumb"}](#Gerror-bars)[Lineranges![](05_grammar_of_graphics_files/figure-html/ggplotLinerange-1.png){class="thumb"}](#Gline-ranges)[Pointranges![](05_grammar_of_graphics_files/figure-html/ggplotPointrange-1.png){class="thumb"}](#Gpoint-ranges)[Ribbons![](05_grammar_of_graphics_files/figure-html/ggplotRibbon-1.png){class="thumb"}](#Gribbons)</div>## Other features<div class = "gallery">[Straight lines![](05_grammar_of_graphics_files/figure-html/ggplotVline-1.png){class="thumb"}](#Gstraight-lines)[Segments![](05_grammar_of_graphics_files/figure-html/ggplotSegment-1.png){class="thumb"}](#Gsegments)[Text![](05_grammar_of_graphics_files/figure-html/ggplotText-1.png){class="thumb"}](#Gtext)</div>:::# The Grammar of Graphics The Grammar of Graphics was first introduced/presented by Wilkinsonand Wills (2006) as a new graphics philosophy that laid down a seriesof rules to govern the production of quantitative graphics.Essentially the proposed graphics infrastructure considers a graphicas comprising a plot (defined by a <a href ="#coordinate-system">coordinate system</a>, <a>scales</a> and<a>panelling</a>) over which one or more data <ahref="#layers-geoms-and-stats">layers</a> are applied.```{r plot1, echo=FALSE}dat <- data.frame(V1 = seq(1,4, length=6), V2 = c(1,2.1,2.6,2.3,3.1,3.9), V3 = letters[c(1,1,1,2,2,2)] )g <- ggplot() + geom_point(data=dat, aes(x=V1,y=V2,color=V3, size=V2)) + coord_equal() + theme_classic()ggsave(filename=paste0(FIG_PATH,"plot1.png"), g, width=3.2, height=2.6, create.dir = TRUE)``````{tikz}%| label: tikz_basics %| engine: tikz%| echo: false%| cache: true%| class: tikz%| engine-opts:%| template: "../resources/tikz-standalone.tex"%\tikzstyle{Title} = [font={\fontspec[Scale=2]{CompleteinHim}}]\tikzstyle{Title} = [font={\fontspec[Scale=1.5]{ArchitectsDaughter-Regular}}]\newcommand{\fplus}[1][black!20]{ \tikz\draw[#1,line width=0.5em] (0,0) -- (1,0)(0.5,0.5) -- (0.5,-0.5);}\newcommand{\fequal}[1][black!20]{ \tikz\draw[#1,line width=0.5em] (0,0.2) -- (1,0.2)(0,-0.2) -- (1,-0.2);}\tikzset{ table/.style={ matrix of nodes, nodes in empty cells, row sep=-\pgflinewidth, column sep=-\pgflinewidth, font=\ttfamily, nodes={ rectangle, draw=white, align=center, fill=black!20 }, minimum height=1.5em, text depth=0.5ex, text height=2ex, nodes in empty cells, column 1/.style={ nodes={text width=2em,font=\bfseries} }, column 2/.style={ nodes={text width=2em} }, column 3/.style={ nodes={text width=2em} }, column 5/.style={ nodes={ text width=4em, fill=blue!40 } }, row 1/.style={ nodes={ fill=black!40, text=white, font=\large\tt\bfseries } } }}\begin{tikzpicture}\matrix (first) [table,text width=4em]{V1&V2 &V3 \\ & & \\ & & \\ & & \\ & & \\ & & \\ & & \\ }; \draw[->,line width = 3pt,color=orange] (first-2-1.center) -- ($(first-2-1.center) +(4,0)$) node [circle,fill=black,inner sep=0pt,minimum size=9pt, right=5pt] {};\draw[->,line width = 3pt,color=orange] (first-3-1.center) -- ($(first-3-1.center) +(4,0)$) node [circle,fill=black,inner sep=0pt,minimum size=9pt, right=5pt] {};\draw[->,line width = 3pt,color=orange] (first-4-1.center) -- ($(first-4-1.center) +(4,0)$) node [circle,fill=black,inner sep=0pt,minimum size=9pt, right=5pt] {};\draw[->,line width = 3pt,color=orange] (first-5-1.center) -- ($(first-5-1.center) +(4,0)$) node [circle,fill=black,inner sep=0pt,minimum size=9pt, right=5pt] {};\draw[->,line width = 3pt,color=orange] (first-6-1.center) -- ($(first-6-1.center) +(4,0)$) node [circle,fill=black,inner sep=0pt,minimum size=9pt, right=5pt] {};\draw[->,line width = 3pt,color=orange] (first-7-1.center) -- ($(first-7-1.center) +(4,0)$) node [circle,fill=black,inner sep=0pt,minimum size=9pt, right=5pt] {};\node [Title,above=0.25cm of first.north,anchor=south] (Data) {Data};\node [Title,above right=0.0cm and 3cm of first.north,anchor=south] (geom) {geom};\node [Title,above right=-1.0cm and 2cm of first.north,anchor=south] (stat) {stat};\draw [ ultra thick, decoration={ brace, mirror, raise=0.25cm, amplitude = 10pt }, decorate] (first.south west) -- ($(geom.east|-first.south)$) node [pos=0.5,anchor=north,yshift=-0.55cm, Title] {layer}; \matrix (second) [table,text width=4em, right=4.0cm of first.south east, anchor=south west,style = { every even column/.style={ nodes={text width=2em,font=\bfseries} },every odd column/.style={ nodes={text width=2em,font=\bfseries} },every even row/.style={ nodes={ fill=white, draw=black!20, text=black!20, font=\large\tt\bfseries } },every odd row/.style={ nodes={ fill=white, draw=black!20, text=black!20, font=\large\tt\bfseries } }}]{ & & & \\ & & &\\ & & & \\ & & & \\ & & & \\ & & & \\ }; \node [Title,above=0.6cm of second.north,anchor=south] (Data) {\parbox[c][][t]{5cm}{\centering{Coordinate system}}};\node [left=0.4cm of second.west, anchor=east] {\fplus}; \node [right=0.4cm of second.east, anchor=west] {\fplus}; \node [right=2cm of second.east, anchor=west, Title] (Scales) {\parbox[c][][t]{2cm}{\centering{Scales\\[2em]theme}}};\draw[black] (second-6-1.south west) -- (second-6-4.south east);\draw[black] (second-6-1.south west) -- (second-1-1.north west);% \matrix (third) [table,text width=4em, right=2cm of second.south east, anchor=south west,% style = {% every even column/.style={% nodes={text width=2em,font=\bfseries}% },% every odd column/.style={% nodes={text width=2em,font=\bfseries}% },% every even row/.style={% nodes={% fill=white,% draw=black!20,% text=black!20,% font=\large\tt\bfseries% }% },% every odd row/.style={% nodes={% fill=white,% draw=black!20,% text=black!20,% font=\large\tt\bfseries% }% }% }% ]{% & & & \\ % & & &\\ % & & & \\ % & & & \\ % & & & \\ % & & & \\ % }; % \draw[black] (third-6-1.south west) -- (third-6-4.south east);% \draw[black] (third-6-1.south west) -- (third-1-1.north west);% \node [circle,fill=black,draw=black,inner sep=0pt,minimum size=9pt] at ($(third.south west) +(1,1)$) {};% \node [circle,fill=black,draw=black,inner sep=0pt,minimum size=9pt] at ($(third.south west) +(1.6,2.1)$) {};% \node [circle,fill=black,draw=black,inner sep=0pt,minimum size=9pt] at ($(third.south west) +(2,2.6)$) {};% \node [circle,fill=black,draw=black,inner sep=0pt,minimum size=9pt] at ($(third.south west) +(2.8,2.3)$) {};% \node [circle,fill=black,draw=black,inner sep=0pt,minimum size=9pt] at ($(third.south west) +(3.3,3.1)$) {};% \node [circle,fill=black,draw=black,inner sep=0pt,minimum size=9pt] at ($(third.south west) +(4.0,3.9)$) {};\node [right=0.3cm of Scales.east, anchor=west] {\fequal}; \node [above right=0.5cm and 6cm of second.north east, anchor=north west] (third) {\includegraphics[width=6cm]{05_grammar_of_graphics_files/figure-html/plot1.png}};\node [Title,above=0.25cm of third.north,anchor=south] (Plot) {Plot};\end{tikzpicture}``````{r ggplotTemplate, eval=FALSE}ggplot() + # required geom_*( # required data = <DATA>, # required - <DATA> is a data frame mapping = aes(<MAPPING>), # required - map variables to scales stat = <STAT>, # optional - map variables to geoms position = <POSITION>) + # optional - adjustments to overlapping geoms coord_*() + # optional - specify coordinate system scale_*() + # optional - visual appearence of scales facet_*() + # optional - subplots theme_*() # optional - overal appearence```Each layer is defined as: - the data - a data frame- <a>mapping</a> specifications that establish the visual aesthetics (colour, line type and thickness, shapes etc) of each variable- statistical methods that determine how the data rows should be summarised (<a>stat</a>)- geometric instructions (<a>geom</a>) on how each summary should be represented (bar, line, point etc)- positional mechanism for dealing with overlapping data (<a>position</a>)The visual aspects of all the graphical features are then governed by<a>themes</a>.Following a very short example, the next section will largelyconcentrate on describing each of the above graphical components.Having then established the workings of these components, we can thenput them together to yield specific graphics.Hadley Wickham's interpretation of these principals in an R context isimplimented via the `ggplot2` _package_. In addition thefollowing packages are also commonly used alongside _ggplot_ so as toexpand on the flexibility etc.- `grid`- `gridExtra`- `scales`- `patchwork`- `tidyverse` - which is actually a collection of packages that make up the _tidyverse_ ecosystem.```{r libraries, cache = FALSE, echo=FALSE, message=FALSE, warning=FALSE}library(ggplot2)library(dplyr)library(tidyr)library(GGally)library(colorspace)```To help illustrate graphical routines and techniques, this tutorialwill make use of a number of data sets - most of which are distributedwith base R or else one of the `tidyverse` _packages. The first ofthese motivating datasets is a built in data set (`BOD`) that recordstemporal changes (days) in biochemical oxygen demand (mg/l).::: {.panel-tabset}## BODThe biochemical oxygen demand (BOD) data set comprises of just twovariables (`demand`: a numeric vector representing biochemical oxygendemand (mg/l) and `Time`: a numeric vector representing the timeassociated with the demand measurement). It was originally publishedin a master thesis in the late 1960's and has since become a standardexample data set on which to demonstrate various statisticaltechniques.:::: {style='display:flex; place-content:space-between;'}::: {style='width:49%;'}```{r BOD1, results='markup', paged.print=FALSE}head(BOD)```:::::: {style='width:49%;'}```{r BOD2}glimpse(BOD)```:::::::## CO2The CO2 data set represents the CO₂ uptake (`uptake`: μmol/m²) oftwelve individual _Echinochloa crus-galli_ plants (`Plant`) from twolocations (`Type`) and two temperature treatments (`Treatment`) inresponse to a range of ambient carbon dioxide concentration (`conc`:ml/l).```{r CO2, results='markup', paged.print=FALSE}head(CO2)``````{r CO22, results='markup', paged.print=FALSE}glimpse(CO2)```## DiamondsThe diamonds data set comprises a set of attributes from a largenumber (~54,000) of diamonds. The most relevant attributes are:- `carat` - weight of the diamond- `cut` - quality of the cut- `clarity` - standard measurement of how clear the diamond is (on a clarity scale)- `price` - price in $US```{r Diamonds, results='markup', paged.print=FALSE}head(diamonds)``````{r Diamonds2, results='markup', paged.print=FALSE}glimpse(diamonds)```## dfThis is a purely ficticious and fabricated example that is intended toprovide a very small example data set.```{r data}df <- data.frame(x = c(3, 1, 5), y = c(2, 4, 6), z = c('a', 'b', 'c'))``````{r dataTable, echo=FALSE}knitr::kable(df,table.attr = "class=\'paperTable\'", format = "html")#paged.print = FALSE```## IrisThe iris data set is a famous example data set that comprises themeasurements (cm) of sepal and petal dimensions for three species ofiris (_Iris setosa_, _versicolor_, and _virginica_).- `Sepal.Length` - length (cm) of sepals- `Sepal.Width` - width (cm) of sepals- `Petal.Length` - length (cm) of petals- `Petal.Width` - width (cm) of petals- `Species` - iris species```{r Iris, results='markup', paged.print=FALSE}head(iris)``````{r Iris2, results='markup', paged.print=FALSE}glimpse(iris)```## Faithfuld2d density estimate of Old Faithful data- `eruptions` - eruption time (mins)- `waiting` - waiting time until next eruption (mins) - `density` - 2d density estimate```{r Faithfuld, results='markup', paged.print=FALSE}head(faithfuld)``````{r Faithfuld2, results='markup', paged.print=FALSE}glimpse(faithfuld)```## WarpbreaksThe warpbreaks dataset comprises of the number of warp breaks per loomfor two different types of wool under three different tensions.- `breaks` - the number of warp breaks- `wool` - the type of wool (A or B) - `tension` - the level of tension (L: low, M: medium, H: high)```{r Warpbreaks, results='markup', paged.print=FALSE}head(warpbreaks)``````{r Warpbreaks2, results='markup', paged.print=FALSE}glimpse(warpbreaks)```:::The following very simple graphic will be used to illustrate thegeneral ggplot template outlined above by explicitly stating many ofthe default specifications. It will use a cartesian coordinatesystem, continuous axes scales, a single facet (panel) and then definea single layer with a dataframe (`BOD`), with red points, identity (nosummarising) statistic visualised as a point geometric.:::: {style='display:flex;'}::: {style='width:72%;'}```{r template1, eval=FALSE}p <- ggplot() + # initialise ggplot layer(data = BOD, # data.frame mapping = aes(y = demand, x = Time), # define x and y variables stat = "identity", # use raw input data geom = "point", # plot data as points position = "identity", # how to handle overlap data params = list(na.rm = TRUE), # additional params for stat show.legend = FALSE # whether include a legend )+ layer(data = BOD, # data.frame mapping = aes(y = demand, x = Time), # define x and y variables stat = "identity", # use raw input data geom = "line", # plot data as a line position = "identity", # how to handle overlap data params = list(na.rm = TRUE), # additional params for stat show.legend = FALSE # whether include a legend ) + coord_cartesian() + # cartesian coordinates scale_x_continuous() + # continuous x axis scale_y_continuous() # continuous y axisp # print the plot```:::::: {style='width:28%;'}```{r template1, eval=TRUE, echo=FALSE, fig.width=4, fig.height = 4, out.width=250}```:::::::Conveniently, much of the default specifications can be omitted.Hence the above can also be entered as::::: {style='display:flex;'}::: {style='width:72%;'}```{r template2, eval=FALSE}p <- ggplot(data = BOD, # initialise ggplot map = aes(x = Time, y = demand)) + # define x and y variables geom_point() + # add points geom_line() # add linesp # print the plot```:::::: {style='width:28%;'}```{r template2, eval=TRUE, echo=FALSE, fig.width=4, fig.height = 4, out.width=250}```:::::::Note the following important features of the grammar of graphics asimplemented in R:- the order in which each of the major components in the first code snippet were added is unimportant. They each add additional information to the overall graphical object. The object itself is evaluated as a whole when it is printed. For example, scales can be defined before layers. <details><summary>Explore</summary> The following both alter the x axis to a log (base 10) scale. Note how both expressions yield the same outcome. :::: {style='display:flex; place-content: space-between;'} ::: {style='width:49%;'}```{r ExOrder1, fig.width=4, fig.height = 3} p <- ggplot(data = BOD, map = aes(x = Time, y = demand)) + geom_point() + geom_line() + scale_x_log10() p ``` ::: ::: {style='width:49%;'}```{r ExOrder2, fig.width=4, fig.height = 3} p <- ggplot(data = BOD, map = aes(x = Time, y = demand)) + scale_x_log10() + geom_point() + geom_line() p ``` ::: :::: </details>- multiple layers are laid down in the order that they appear in the statement. In the example above, the lines are drawn over the top of the points. <details><summary>Explore</summary> The following define points and lines in different orders. Note how the points appear under the line in the left hand figure and over the line in the right hand figure. :::: {style='display:flex; place-content: space-between;'} ::: {style='width:49%;'}```{r ExLayers1, fig.width=4, fig.height = 3} p <- ggplot(data = BOD, map = aes(x = Time, y = demand)) + geom_point(color = 'red', size=5) + geom_line() p ``` ::: ::: {style='width:49%;'}```{r ExLayers2, fig.width=4, fig.height = 3} p <- ggplot(data = BOD, map = aes(x = Time, y = demand)) + geom_line() + geom_point(color = 'red', size=5) p ``` ::: :::: </details>- layers can be defined either with the `layers` _function_, or more conveniently (as illustrated in the shorter, second code snippet), via a `geom_*` function or `stat_*` _function_. <details><summary>Explore</summary> The following both specify a single layer (line). The left hand figure uses the `geom_line` _function with the `stat = 'identity` _argument_ and the right hand figure uses the `stat_identity` _function_ with the `geom = 'line'` _argument_. Note, _identity_ just means multiple by 1 (i.e. do nothing). The default `stat` for `geom_line` is `identity` and therefore it is not necessary to provide this argument - in this case it was purely used for illustrative purposes. :::: {style='display:flex; place-content: space-between;'} ::: {style='width:49%;'}```{r ExLayers3, fig.width=4, fig.height = 3} p <- ggplot(data = BOD, map = aes(x = Time, y = demand)) + geom_line(stat = 'identity') p ``` ::: ::: {style='width:49%;'}```{r ExLayers4, fig.width=4, fig.height = 3} p <- ggplot(data = BOD, map = aes(x = Time, y = demand)) + stat_identity(geom = 'line') p ``` ::: :::: </details>- the data and mapping used by `geom_*` and `stats_*` _functions_ can inherit mapping aesthetics from the main `ggplot()` function. <details><summary>Explore</summary> The following figures both generate a plot with both points and a line. In the left hand side syntax, `geom_point` and `geom_line` inherit the data and aesthetic mappings from the `ggplot` _function_. In the right hand side syntax, the two _geoms_ specify the data and aesthetic mappings themselves. Whilst in this case it does lead to code duplication, in other cases, it does permit different sources of data to be integrated together into a single graphic. :::: {style='display:flex; place-content: space-between;'} ::: {style='width:49%;'}```{r ExStat1, fig.width=4, fig.height = 3} p <- ggplot(data = BOD, map = aes(x = Time, y = demand)) + geom_line() + geom_point() p ``` ::: ::: {style='width:49%;'}```{r ExStat2, fig.width=4, fig.height = 3} p <- ggplot() + geom_line(data = BOD, map = aes(x = Time, y = demand)) + geom_point(data = BOD, map = aes(x = Time, y = demand)) p ``` ::: :::: </details>- the `ggplot()` _function_ along with the other building _functions_ work together to create a set of instructions that determine how a graph is to be constructed. In the examples above, these instructions are stored in a variable (which we arbitrarily assigned the name `p`). The graph is not actually generated until the`print` method is called on this object. Recall that in R, entering the name of an object is the same as running the `print` function on this object. <details><summary>Explore</summary> In the following figures, the plotting instructions will be built up in multiple stages. - the first stage of both examples stores the ggplot initialisation (including specification of the data and aesthetic mapping). - the second stage of the left hand syntax adds instructions for including two data layers (line and points). Finally, the left hand syntax then uses prints the full set of plotting instructions (which enacts the generation of the plot). - the second stage of the right hand syntax adds instructions for including a single layer (line) and in the final stage, instructions for including a point layer are added to the instruction set sent to `print`. - note, unlike for the left hand side syntax, the stored set of instructions on the right hand side do not contain the instructions for including the points layer - they are only added at the time of printing :::: {style='display:flex; place-content: space-between;'} ::: {style='width:49%;'}```{r ExProgressive1, fig.width=4, fig.height = 3} p <- ggplot(data = BOD, map = aes(x = Time, y = demand)) p <- p + geom_line() + geom_point() p ``` ::: ::: {style='width:49%;'}```{r ExProgressive2, fig.width=4, fig.height = 3} p <- ggplot(data = BOD, map = aes(x = Time, y = demand)) p <- p + geom_line() p + geom_point() ``` ::: :::: </details>In all of the examples so far, you might have noticed that we storedthe plotting instructions in an arbitrary variable (`p`) prior toprinting (generating the plot). Although it is not necessary to storethe instructions (we can just accumulate the instructions and print ina single expression), storing sets of instructions do allow theinstruction set to be duplicated or altered. This is particularlyuseful when you have a need to produce two similar and complex plots.Since this is a tutorial, it will endeavour to focus on a very smallset of concepts at any one time. To that end, most of the examples ina section will feature slight variations of a common snippet of code.Typically, there will be a single common code block that initialises afairly generic plot followed by a series of modifications/additionsthat highlight different plotting routines or options.In an attempt to illustrate the use of ggplot for elegant graphics, wewill drill down into each of the plot and layer specifications.# Layers (geoms and stats)### OverviewAlthough the geoms and thus layers are amongst the last features to beconstructed by the system, the data and aesthetic features of the dataimpact on how the coordinate system, scales and panelling work.Therefore, we will explore the geoms first.Geometric objects (**geoms**) are visual representations ofobservations. For example, there is a geom to represent points basedon a set of x,y coordinates. All graphics need at least one geom andeach geom is mapped to its own layer. Multiple geoms can be added toa graphic and the order that they are added to the expressiondetermines the order that their respective layer is constructed.When a `ggplot` expression is being evaluated, geoms are coupledtogether with a **stat_** function. This function is responsible forgenerating data appropriate for the geom. For example, the`stat_boxplot` is responsible for generating the quantiles, whiskersand outliers for the `geom_boxplot` function.In addition to certain specific `stat_` functions, all geoms can becoupled to a `stat_identity` function. In mathematical contexts,identity functions map each element to themselves - this essentiallymeans that each element passes straight through the identity functionunaltered. Coupling a geom to an identity function is useful when thecharacteristics of the data that you wish to represent are present inthe data frame. For example, your dataframe may contain the x,ycoordinates for a series of points and you wish for them to be usedunaltered as the x,y coordinates on the graph. Moreover, yourdataframe may contain pre-calculated information about the quantiles,whiskers and outliers and you wish these to be used in theconstruction of a boxplot (rather than have the internals of ggplotperform the calculations on raw data). Since `geom_` and `stats_` functions are coupled together, a geometricrepresentation can be expressed from either a `geom_` function OR a`stats_` function. That is, you either:- specify a `geom_` function that itself calls a `stat_` function to provide the data for the `geom` function.```{r plotGgplotGeomSmoothA, cache=TRUE, tidy=TRUE, echo=TRUE, eval=FALSE,fig.width=4, fig.height=4, out.width=150, out.height=150, warning=FALSE, message=FALSE} ggplot(CO2)+geom_smooth(aes(x=conc,y=uptake), stat="smooth") ```- specify a `stat_` function that itself calls a `geom_` function to visually represent the data..```{r plotGgplotGeomSmoothB, cache=TRUE, tidy=TRUE, echo=TRUE, eval=FALSE,fig.width=4, fig.height=4, out.width=150, out.height=150, warning=FALSE, message=FALSE} ggplot(CO2)+stat_smooth(aes(x=conc,y=uptake), geom="smooth") ```It does not really make any difference which way around you do this.For the remainder of this tutorial, we will directly engage the`geom_` function for all examples.The <samp>geom_</samp> functions all have numerous arguments, many ofwhich are common to all <samp>geoms_</samp>.- `data` - the data frame containing the data. Typically this is inherited from the `ggplot` function.- `mapping` - the aesthetic mapping instructions. Through the aesthetic mapping the aesthetic visual characteristics of the geometric features can be controlled (such as colour, point sizes, shapes etc). The aesthetic mapping can be inherited from the`ggplot` function. Common aesthetic features (mapped via a `aes` function) include: - `alpha` - transparency - `colour` - colour of the geometric features - `fill` - fill colour of geometric features - `linetype` - type of lines used in geometric features (dotted, dashed, etc) - `size` - size of geometric features such as points or text - `shape` - shape of geometric features such as points - `weight` - weightings of values- `stat` - the `stat_` function coupled to the `geom_` function- `position` - the position adjustment for overlapping objects - `identity` - leave objects were they are: - `dodge` - shift objects to the side to prevent overlapping - `stack` - stack objects on top of each other - `fill` - stack objects on top of each other and standardize each group to equal height- `show.legend` - whether a legend should be included- `inherit.aes` - whether to override any aesthetics from the `ggplot` _function_The above characteristics that can be aesthetically mapped to datavariables, can alternatively be set directly. For example, in theleft hand code snippet below, the colour of the points is determinedby the value of the specified variable. However, in the right handcode snippet, the colour of the points is set to be red. Notice, thatin this later instance, the colour is specified _outside_ of theaesthetic (`aes`) _function_.:::: {style='display:flex; place-content: space-between;'}::: {style='width:49%;'}```{r ExColour1, fig.width=4, fig.height = 3}ggplot(data = CO2, map = aes(x = conc, y = uptake)) + geom_point(aes(colour = Type)) ```:::::: {style='width:49%;'}```{r ExColour2, fig.width=4, fig.height = 3}ggplot(data = CO2, map = aes(x = conc, y = uptake)) + geom_point(colour = "red") ```:::::::The above figure (left side) illustrated the use of aesthetics tohighlight different groups (or _series_) within the data (`Type` inthat case). For some _geoms_ it is important that they be able tooperate on a single series of data to avoid strange artefacts. Thenext two figures will include a layer of lines. In the left handfigure, the lines are drawn across a single series (group) yielding anonsense pattern. This is addressed in the right hand figure whichspecifies that separate lines should be drawn for each `Plant` (eachplant is its own series or _group_).:::: {style='display:flex; place-content: space-between;'}::: {style='width:49%;'}```{r ExGroup1, fig.width=4, fig.height = 3}ggplot(data = CO2, map = aes(x = conc, y = uptake)) + geom_line() ```:::::: {style='width:49%;'}```{r ExGroup2, fig.width=4, fig.height = 3}ggplot(data = CO2, map = aes(x = conc, y = uptake)) + geom_line(aes(group = Plant)) ```:::::::In the above right, the series are defined by the _group_ aesthetic.Alternatively, we could have mapped `Plant` to one of the otheraesthetics (such as `colour`, `linetype` etc), in which case, eachline would have a different visual appearance and a legend would havealso been included.Currently, there are a large number of available `geoms_` and `stat_`functions within the ggplot system. This tutorial is still a work inprogress and therefore does not include all of them - I have focusedon the more commonly used ones.## Primitive geoms Primitive _geoms_ are simple plotting shapes that typically representdirect mapping of data variables to the shapes - that is, they do notrequire specific _stat_ _functions_. Hence, all primitive _geoms_ use_stat_identity_. Nevertheless, it is possible to have the shapesmapped via alternative _stats_ functions is appropriate ([seepoints](#Gpoints)).::: {.panel-tabset}### Blank ![](05_grammar_of_graphics_files/figure-html/ggplotBlank-s-1.png){class="thumb-s" #Gblank} Although this might seem pointless, it can be useful for forcing axesscales to conform to a particular format - since axes scales aredetermined by the first layer (which can be blank) defined in thesequence of expressions. To help illustrate this, I will introduce a fabricated data setcomprising the length (mm) of 10 day old frog tadpoles incubated atthree different temperatures (Low, Medium, High).:::: {style='display:flex; place-content: space-between;'}::: {style='width:49%;'}```{r dataFrogs}tadpole <- tribble( ~length, ~temp, 2.1, "Low", 2.0, "Low", 1.8, "Low", 2.2, "Medium", 2.3, "Medium", 2.3, "Medium", 2.5, "High", 2.7, "High", 2.8, "High", ) %>% mutate(temp = factor(temp))```:::::: {style='width:49%;'}Now imagine you wish to produce a scatterplot (with `length` mapped tothe y-axis and `day` mapped to the x-axis) to explore these data.Since although `temp` is categorical, it is ordered, we would alsolike to plot a line representing the overall trend in tadpole lengthin relation to temperature. Doing so would introduce one of two problems:- lines can only be plotted when both x and y are mapped to continuous variables- in order to plot a line, we would need to convert temperature into a numeric variable in some way, however doing so would mean that the axes labels loose their meaning.::: ::::Using a `geom_blank` allows us to define a line and maintain usefulaxes labels. The second and third examples below will illustrate theproblem and solution respectively.```{r ggplotBlank-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(data = tadpole, aes(y = length, x = temp)) + geom_blank() + blank_theme```<table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>geom</th><th align = 'left'>stat</th><th align = 'left'>position</th><th align = 'left'>Aesthetic parameters / Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>Blank layer</td><td align = 'left'>`_blank`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br></td><td align = 'left' rowspan = 2>```{r ggplotBlank, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data=tadpole, aes(y = length, x = temp)) + geom_blank()```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotBlank, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>Blank layer</td><td align = 'left'>`_blank`</td><td align = 'left'>`_summary`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br></td><td align = 'left' rowspan = 2>```{r ggplotBlank1, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data=tadpole, aes(y = length, x = as.numeric(temp))) + geom_line(stat = 'summary', fun = mean) ```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotBlank1, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>Blank layer</td><td align = 'left'>`_blank`</td><td align = 'left'>`_summary`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br></td><td align = 'left' rowspan = 2>```{r ggplotBlank2, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data=tadpole, aes(y = length, x = temp))+ geom_blank() + geom_line(aes(x = as.numeric(temp)), stat = 'summary', fun = mean) ```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotBlank2, eval=FALSE}```</td></tr></tbody></table>### Points ![](05_grammar_of_graphics_files/figure-html/ggplotPoint-s-1.png){class="thumb-s" #Gpoints}```{r ggplotPoint-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(data = df, aes(y = y, x = x)) + geom_point(size=8) + blank_theme````geom_point` draws points (scatterplot). Typically the stat used is`stat_identity` as we wish to use the values in two continuous vectorsas the coordinates of each point.The following list describes the mapping aesthetic propertiesassociated with `geom_point`. The entries in bold are compulsory.<details><summary>Show attributes</summary>| Parameter | `geom_point` ||---------------------------------------------------------------+--------------|| _aesthetics_ | || **`x`** - variable to map to the x-axis | ✔ || **`y`** - variable to map to the y-axis | ✔ || `group` - plot separate series without aesthetic differences | ✔ || `alpha` - transparency | ✔ || `colour` - colour of the points/lines | ✔ || `fill` - inner colour of points/shapes | ✔ || `linetype` - type of lines used to construct points/lines | ✔ || `size` - thickness of the line | ✔ || `shape` - the plotting symbol/character | ✔ || `weight` - weightings of values | ✔ |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>geom</th><th align = 'left'>stat</th><th align = 'left'>position</th><th align = 'left'>Aesthetic parameters / Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>points layer</td><td align = 'left'>`_point`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br>`geom_point` forms the basis of various plots such as scatterplots, maps and others</td><td align = 'left' rowspan = 2>```{r ggplotPoint, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data=df, aes(y = y, x = x)) + geom_point()```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotPoint, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>means points layer</td><td align = 'left'>`_point`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y,fun<br><br>plots points based on the values provided</td><td align = 'left' rowspan = 2>```{r ggplotPoint1, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data=CO2, aes(x = conc, y = uptake)) + geom_point()```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotPoint1, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>means points layer</td><td align = 'left'>`_point`</td><td align = 'left'>`_summary`</td><td align = 'left'>identity</td><td align = 'left'>x,y,fun<br><br>plots the result of the specified summary function</td><td align = 'left' rowspan = 2>```{r ggplotPoint2, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data=CO2, aes(x = conc, y = uptake)) + geom_point(stat = "summary", fun = mean)```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotPoint2, eval=FALSE}```</td></tr></tbody></table>The plotting symbols are specified by either a number (index of aplotting symbol - see below) or a single character (printedliterally).```{r ggplotPoint3, echo = FALSE, eval=TRUE, fig.height = 3, fig.width = 9}pch_table <- expand.grid(x=0:6, y = 0:4) %>% mutate(shape = 0:(n()-1)) %>% filter(shape < 26)ggplot(data = pch_table, aes(x = x, y = y, shape = shape)) + geom_blank(data = NULL, aes(x=0, y = -0.2)) + geom_point(size = 5, fill = 'red', stroke = 2) + geom_text(aes(label = shape, y = y + 0.4)) + scale_shape_identity() + coord_cartesian() + theme(axis.title = element_blank(), axis.text = element_blank(), axis.ticks = element_blank())```### Text ![](05_grammar_of_graphics_files/figure-html/ggplotText-s-1.png){class="thumb-s" #Gtext} ```{r ggplotText-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(data = df, aes(y = y, x = x)) + geom_text(aes(label = z), size=15) + blank_theme```The following list describes the mapping aesthetic propertiesassociated with `geom_text`. The entries in bold are compulsory.<details><summary>Show attributes</summary>| Parameter | `geom_text` ||--------------------------------------------------------------------------------------|-------------|| - **`x`** - variable to map to the x-axis | ✔ || - **`y`** - variable to map to the y-axis | ✔ || - **`label`** - text to use as labels | ✔ || - `group` - plot separate series without aesthetic differences | ✔ || - `alpha` - transparency | ✔ || - `colour` - colour of the points/lines | ✔ || - `fill` - inner colour of points/shapes | ✔ || - `linetype` - type of lines used to construct points/lines | ✔ || - `shape` - symbol shape for points | ✔ || - `size` - size of symbol | ✔ || - `family` - font family | ✔ || - `fontface` - bold, italic, normal etc | ✔ || - `hjust` - horizontal justification | ✔ || - `vjust` - vertical justification | ✔ || | || _additional parameters_ | || - `parse` - whether to parse labels into expressions (to include special characters) | FALSE || - `nudge_x` - horizontal adjustments to label positions | 0 || - `nudge_y` - vertical adjustments to label positions | 0 || - `check_overlap` - whether to plot text that overlaps other text in layer | FALSE |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>geom</th><th align = 'left'>stat</th><th align = 'left'>position</th><th align = 'left'>Aesthetic parameters / Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>Text layer</td><td align = 'left'>`_text`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y,label<br><br>Text on a plot - useful for depicting the location of observations</td><td align = 'left' rowspan = 2>```{r ggplotText, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data=df, aes(y = y, x = x)) + geom_text(aes(label = z))```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotText, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>Text layer</td><td align = 'left'>`_text`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y,label<br><br>Text on a plot - useful for depicting the location of observations</td><td align = 'left' rowspan = 2>```{r ggplotText1, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data=CO2, aes(y = uptake, x = conc)) + geom_text(aes(label = Treatment))```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotText1, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>Text layer</td><td align = 'left'>`_text`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y,label<br><br>Text on a plot - useful for depicting the location of observations</td><td align = 'left' rowspan = 2>```{r ggplotText2, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data=CO2, aes(y = uptake, x = conc)) + geom_text(aes(label = toupper(substr(Treatment, 1, 1))))```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotText2, eval=FALSE}```</td></tr></tbody></table>Horizontal (`hjust`) and vertical (`vjust`) text justificationcontrols are often a source of confusion and this is furtherexacerbated when combined with `angle` control. The followingexcellent demonstration from[here](https://stackoverflow.com/questions/7263849/what-do-hjust-and-vjust-do-when-making-a-plot-using-ggplot)provides a visual aid to understanding the use of these controls.```{r ggplotText3, eval=TRUE, fig.height = 3, fig.width = 9}td <- expand.grid( hjust=c(0, 0.5, 1), vjust=c(0, 0.5, 1), angle=c(0, 45, 90), text="text")ggplot(td, aes(x=hjust, y=vjust)) + geom_point() + geom_text(aes(label=text, angle=angle, hjust=hjust, vjust=vjust)) + facet_grid(~angle) + scale_x_continuous(breaks=c(0, 0.5, 1), expand=c(0, 0.2)) + scale_y_continuous(breaks=c(0, 0.5, 1), expand=c(0, 0.2))```### Paths ![](05_grammar_of_graphics_files/figure-html/ggplotPath-s-1.png){class="thumb-s" #Gpaths}```{r ggplotPath-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(data = df, aes(y = y, x = x)) + geom_path() + blank_theme````geom_path` draws paths (line plots). Paths order the coordinatesaccording to the order in the data frame (c.f. `geom_line` and`geom_step`)<details><summary>Show attributes</summary>| Parameter | `geom_path` ||----------------------------------------------------------------|-------------|| - **`x`** - variable to map to the x-axis | ✔ || - **`y`** - variable to map to the y-axis | ✔ || - `group` - plot separate series without aesthetic differences | ✔ || - `alpha` - transparency | ✔ || - `colour` - colour of the points/lines | ✔ || - `fill` - inner colour of points/shapes | ✔ || - `linetype` - type of lines used to construct points/lines | ✔ || - `shape` - symbol shape for points | ✔ || - `size` - size of symbol | ✔ || | || _additional parameters_ | || - `lineend` - line end style (round, butt, squate) | 'butt' || - `linejoin` - line join style (round, mitre, bevel) | 'round' || - `linemitre` - line mitre limit | 10 || - `arrow` - arrow specification (`grid::arrow()`) | NULL |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>geom</th><th align = 'left'>stat</th><th align = 'left'>position</th><th align = 'left'>Aesthetic parameters / Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>paths layer</td><td align = 'left'>`_path`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br>`geom_path` draws lines connecting coordinates in the order present in the data frame</td><td align = 'left' rowspan = 2>```{r ggplotPath, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data=df, aes(y = y, x = x)) + geom_path()```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotPath, eval=FALSE}```</td></tr></tbody></table>The simple line types available are highlighted in the followingfigure:```{r ggplotPatht3, echo = FALSE, eval=TRUE, fig.height = 3, fig.width = 9}linetype_table <- data.frame(y = 0:6, linetype = c('blank', 'solid', 'dashed', 'dotted', 'dotdash', 'longdash', 'twodash')) %>% mutate(linetype = factor(linetype, levels = unique(linetype)))ggplot(data = linetype_table, aes(x = 1, y = y, linetype = linetype)) + geom_blank(data = NULL, aes(x=0, y = -0.2)) + geom_segment(aes(x=0, xend = 4, yend = y), show.legend = FALSE) + geom_text(aes(label = linetype, y = y + 0.4, x= 0), hjust = 0) + scale_linetype_identity() + coord_cartesian() + theme(axis.title = element_blank(), axis.text = element_blank(), axis.ticks = element_blank())```### Polygons ![](05_grammar_of_graphics_files/figure-html/ggplotPolygon-s-1.png){class="thumb-s" #Gpolygons}```{r ggplotPolygon-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(data = df, aes(y = y, x = x)) + geom_polygon() + blank_theme````geom_polygon` draws polygons with the coordinates orderedaccording to the order in the data frame.<details><summary>Show attributes</summary>| Parameter | `geom_polygon` ||----------------------------------------------------------------|----------------|| - **`x`** - variable to map to the x-axis | ✔ || - **`y`** - variable to map to the y-axis | ✔ || - `group` - plot separate series without aesthetic differences | ✔ || - `alpha` - transparency | ✔ || - `colour` - colour of the points/lines | ✔ || - `fill` - inner colour of points/shapes | ✔ || - `linetype` - type of lines used to construct points/lines | ✔ || - `shape` - symbol shape for points | ✔ || - `size` - size of symbol | ✔ || | || _additional parameters_ | || - `rule` - determines how holes in polygons are treated | 'evenodd' |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>geom</th><th align = 'left'>stat</th><th align = 'left'>position</th><th align = 'left'>Aesthetic parameters / Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>polygon layer</td><td align = 'left'>`_polygon`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br>`geom_polygon` draws polygons using coordinates in the order present in the data frame</td><td align = 'left' rowspan = 2>```{r ggplotPolygon, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data=df, aes(y = y, x = x)) + geom_polygon()```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotPolygon, eval=FALSE}```</td></tr></tbody></table>### Areas ![](05_grammar_of_graphics_files/figure-html/ggplotArea-s-1.png){class="thumb-s" #Gareas}```{r ggplotArea-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(data = df, aes(y = y, x = x)) + geom_area() + blank_theme````geom_area` draws areas under curves with the coordinates orderedaccording to the order in the data frame.<details><summary>Show attributes</summary>| Parameter | `geom_area` ||-----------------------------------------------------------------------|-------------|| - **`x`** - variable to map to the x-axis | ✔ || - **`y`** - variable to map to the y-axis | ✔ || - `group` - plot separate series without aesthetic differences | ✔ || - `alpha` - transparency | ✔ || - `colour` - colour of the points/lines | ✔ || - `fill` - inner colour of points/shapes | ✔ || - `linetype` - type of lines used to construct points/lines | ✔ || - `shape` - symbol shape for points | ✔ || - `size` - size of symbol | ✔ || | || _additional parameters_ | || - `outline.type` - determines the type of outline to draw around area | 'both' |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>geom</th><th align = 'left'>stat</th><th align = 'left'>position</th><th align = 'left'>Aesthetic parameters / Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>area layer</td><td align = 'left'>`_area`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br>`geom_area` draws areas under a curve using coordinates in the order present in the data frame</td><td align = 'left' rowspan = 2>```{r ggplotArea, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data=df, aes(y = y, x = x)) + geom_area()```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotArea, eval=FALSE}```</td></tr></tbody></table>### Ribbons ![](05_grammar_of_graphics_files/figure-html/ggplotRibbon-s-1.png){class="thumb-s" #Gribbons}```{r ggplotRibbon-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(data = df, aes(y = y, x = x)) + geom_ribbon(aes(ymin = y - 1, ymax = y + 1)) + blank_theme``````{r ggplotRibbons, include=FALSE, echo=FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(data = df, aes(y = y, x = x)) + geom_ribbon(aes(ymin = y - 1, ymax = y + 1))````geom_ribbon` draws ribbons (polygons) based on upper (max) and lower(min) levels of y associated with each level of x and are typicallyused to represent uncertainty in trends.<details><summary>Show attributes</summary>| Parameter | `geom_ribbon` ||-----------------------------------------------------------------------|---------------|| - **`x`** - variable to map to the x-axis | ✔ || - **`y`** - variable to map to the y-axis | ✔ || - `group` - plot separate series without aesthetic differences | ✔ || - `alpha` - transparency | ✔ || - `colour` - colour of the points/lines | ✔ || - `fill` - inner colour of points/shapes | ✔ || - `linetype` - type of lines used to construct points/lines | ✔ || - `shape` - symbol shape for points | ✔ || - `size` - size of symbol | ✔ || | || _additional parameters_ | || - `outline.type` - determines the type of outline to draw around area | 'both' |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>geom</th><th align = 'left'>stat</th><th align = 'left'>position</th><th align = 'left'>Aesthetic parameters / Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>ribbon layer</td><td align = 'left'>`_ribbon`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br>`geom_ribbon` draws ribbons on a plot - useful for depicting uncertainty (confidence/credibility) intervals</td><td align = 'left' rowspan = 2>```{r ggplotRibbon, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data=df, aes(ymin = y -1, ymax = y + 1, x = x)) + geom_ribbon()```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotRibbon, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>ribbon layer</td><td align = 'left'>`_ribbon`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br>`geom_ribbon` draws ribbons on a plot - useful for depicting uncertainty (confidence/credibility) intervals</td><td align = 'left' rowspan = 2>```{r ggplotRibbon1, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}BOD.lm <- lm(demand ~ Time, data = BOD)newdata <- with(BOD, data.frame(Time = seq(min(Time), max(Time), length = 100)))newdata <- newdata %>% cbind(predict(BOD.lm, newdata = newdata, interval = 'confidence'))ggplot(data=newdata) + geom_ribbon(aes(x = Time, ymin = lwr, ymax = upr))```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotRibbon1, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>ribbon layer</td><td align = 'left'>`_ribbon`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br>`geom_ribbon` draws ribbons on a plot - useful for depicting uncertainty (confidence/credibility) intervals</td><td align = 'left' rowspan = 2>```{r ggplotRibbon2, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}BOD.lm <- lm(demand ~ Time, data = BOD)newdata <- with(BOD, data.frame(Time = seq(min(Time), max(Time), length = 100)))newdata <- newdata %>% cbind(predict(BOD.lm, newdata = newdata, interval = 'confidence'))ggplot(data=newdata, aes(x = Time)) + geom_ribbon(aes(x = Time, ymin = lwr, ymax = upr), fill='orange') + geom_line(aes(y = fit)) + geom_point(data = BOD, aes(y=demand))```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotRibbon2, eval=FALSE}```</td></tr></tbody></table>:::## Visualising distributions ::: {.panel-tabset}### Boxplots<br><br> ![](05_grammar_of_graphics_files/figure-html/ggplotBoxplot-s-1.png){class="thumb-s" #Gboxplots}`geom_boxplot` constructs boxplots. The values of the various elementsof the boxplot (quantiles, whiskers etc) are calculated by its mainpairing function (`stat_boxplot`). The following list describes themapping aesthetic properties associated with `geom_boxplot`. Theentries in bold are compulsory. Note that boxplots are usuallyspecified via the `geom_boxplot` _function_ which will engage the`stat_boxplot` to calculate the quantiles, whiskers andoutliers. Therefore, confusingly, when calling `geom_boxplot`, thecompulsory parameters are actually those required by `stat_boxplot`(unless you indicated to use `stat_identity`).```{r ggplotBoxplot-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(data = diamonds, aes(x = carat)) + geom_boxplot() + blank_theme``````{r ggplotBoxplotP, include=FALSE, echo=FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(diamonds) + geom_boxplot(aes(x = carat, y = cut))```<details><summary>Show attributes</summary>| Parameter | `geom_boxplot` | `stat_boxplot` ||---------------------------------------------------------------|----------------|----------------|| _aesthetics_ | | || **`x`** - variable to map to the x-axis | ✔ | ✔ || `y` - variable to map to the other axis | ✔ | ✔ || **`lower`** - value of the lower box line (25th percentile) | ✔ | || **`middle`** - value of the middle box line (50th percentile) | ✔ | || **`upper`** - value of the upper box line (75th percentile) | ✔ | || **`ymin`** - value of lower whisker | ✔ | || **`ymax`** - value of upper whisker | ✔ | || `group` - plot separate series without aesthetic differences | ✔ | ✔ || `alpha` - transparency | ✔ | ✔ || `colour` - colour of the points/lines | ✔ | ✔ || `fill` - inner colour of points/shapes | ✔ | ✔ || `linetype` - type of lines used to construct points/lines | ✔ | ✔ || `size` - thickness of the line | ✔ | ✔ || `weight` - weightings of values | ✔ | ✔ || | | || _additional parameters_ | | || `outlier.colour` - color of symbols for outliers | NULL | NULL || `outlier.fill` - fill of symbols for outliers | NULL | NULL || `outlier.shape` - shape of symbols for outliers | NULL | NULL || `outlier.size` - size of symbols for outliers | NULL | NULL || `outlier.stroke` - colour of lines in symbols for outliers | NULL | NULL || `outlier.alpha` - transparency of symbols for outliers | NULL | NULL || `notch` - whether to notch the boxplots | FALSE | FALSE || `notchwidth` - width of notch | 0.5 | 0.5 || | | || _Computed variables_ | | || **`lower`** - value of the lower box line (25th percentile) | ✔ | || **`middle`** - value of the middle box line (50th percentile) | ✔ | || **`upper`** - value of the upper box line (75th percentile) | ✔ | || **`ymin`** - value of lower whisker | ✔ | || **`ymax`** - value of upper whisker | ✔ | |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>geom</th><th align = 'left'>stat</th><th align = 'left'>position</th><th align = 'left'>Aesthetic parameters / Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>boxplot</td><td align = 'left'>`_boxplot`</td><td align = 'left'>`_boxplot`</td><td align = 'left'>dodge</td><td align = 'left'>x<br><br>plot of quantiles, whiskers and outliers.</td><td align = 'left' rowspan = 2>```{r ggplotBoxplot, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data=diamonds, aes(x = carat)) + geom_boxplot()```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotBoxplot, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>boxplot</td><td align = 'left'>`_boxplot`</td><td align = 'left'>`_boxplot`</td><td align = 'left'>dodge</td><td align = 'left'>y<br><br>plot of quantiles, whiskers and outliers.</td><td align = 'left' rowspan = 2>```{r ggplotBoxplot1, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data=diamonds, aes(y = carat)) + geom_boxplot()```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotBoxplot1, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>conditional boxplot</td><td align = 'left'>`_boxplot`</td><td align = 'left'>`_boxplot`</td><td align = 'left'>dodge</td><td align = 'left'>y,x<br><br>plot of quantiles, whiskers and outliers.</td><td align = 'left' rowspan = 2>```{r ggplotBoxplot2, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data=diamonds, aes(y = carat, x = cut)) + geom_boxplot()```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotBoxplot2, eval=FALSE}```</td></tr></tbody></table>### Histograms<br><br> ![](05_grammar_of_graphics_files/figure-html/ggplotHistogram-s-1.png){class="thumb-s" #Ghistograms}```{r ggplotHistogram-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(data = diamonds, aes(x = carat)) + geom_histogram() + blank_theme````geom_histogram` draws histograms of continuous data after binning the data,The following list describes the mapping aesthetic propertiesassociated with `geom_histogram`. The entries in bold are compulsory.<details><summary>Show attributes</summary>| Parameter | `geom_histogram` | `stat_bin` ||--------------------------------------------------------------|------------------|-------------------|| _aesthetics_ | | || **`x`** - variable to map to the x-axis | ✔ | ✔ || `group` - plot separate series without aesthetic differences | ✔ | ✔ || `alpha` - transparency | ✔ | ✔ || `colour` - colour of the points/lines | ✔ | ✔ || `fill` - inner colour of points/shapes | ✔ | ✔ || `linetype` - type of lines used to construct points/lines | ✔ | ✔ || `size` - thickness of the line | ✔ | ✔ || `weight` - weightings of values | ✔ | ✔ || | | || _additional parameters_ | | || `binwidth` - width of the bins | NULL | NULL || `bins` - number of bins | NULL | NULL || `breaks` - vector of bin boundaries | | NULL || `center` - bin position specifier | | NULL || `boundary` - bin position specifier | | NULL || `closed` - which bin edge is included | | c('right','left') || `pad` - whether to include empty bins at either end of x | | FALSE || `orientation` - which axis (x or y) to operate on | NA | NA || | | || _Computed variables_ | | || `count` - number of points in bin | ✔ | || `density` - density of points in bin | ✔ | || `ncount` - counts scaled to max of 1 | ✔ | || `ndensity` - density scaled to max of 1 | ✔ | |: {.primary .bordered .sm .paramsTable}</details><!--:::: {.plainTable}| aesthetics | additional arguments | Computed variables ||--------------------------------------------------------------|---------------------------------------------------|-----------------------------------------|| **`x`** - variable to map to the x-axis | `binwidth` - width of the bins | `count` - number of points in bin || `group` - plot separate series without aesthetic differences | `bins` - number of bins | `density` - density of points in bin || `alpha` - transparency | `orientation` - which axis (x or y) to operate on | `ncount` - counts scaled to max of 1 || `colour` - colour of the points/lines | | `ndensity` - density scaled to max of 1 || `fill` - inner colour of points/shapes | | || `linetype` - type of lines used to construct points/lines | | || `size` - thickness of the line | | || `weight` - weightings of values | | |::::--><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>geom</th><th align = 'left'>stat</th><th align = 'left'>position</th><th align = 'left'>Aesthetic parameters / Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>histogram layer</td><td align = 'left'>`_histogram`</td><td align = 'left'>`_bin`</td><td align = 'left'>stack</td><td align = 'left'>x or y<br><br>`geom_histogram` bins continuous data and uses the number of cases in each bin as the height of a set of rectangles.<br><br>Computed variables:<br></td><td align = 'left' rowspan = 2>```{r ggplotHistogram, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data = diamonds, aes(x = carat)) + geom_histogram()```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotHistogram, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>histogram layer</td><td align = 'left'>`_histogram`</td><td align = 'left'>`_bin`</td><td align = 'left'>stack</td><td align = 'left'>x or y<br><br>the granularity of the histogram can be altered by changing the binwidth.</td><td align = 'left' rowspan = 2>```{r ggplotHistogram1a, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data = diamonds, aes(x = carat)) + geom_histogram(binwidth = 0.05)```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotHistogram1a, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>histogram layer</td><td align = 'left'>`_histogram`</td><td align = 'left'>`_bin`</td><td align = 'left'>stack</td><td align = 'left'>x or y<br><br>the granularity of the histogram can be altered by changing the number of bins.</td><td align = 'left' rowspan = 2>```{r ggplotHistogram1b, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data = diamonds, aes(x = carat)) + geom_histogram(bins = 10)```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotHistogram1b, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>histogram layer</td><td align = 'left'>`_histogram`</td><td align = 'left'>`_bin`</td><td align = 'left'>stack</td><td align = 'left'>x or y<br><br>`geom_histogram` bins continuous data and uses the number of cases in each bin as the height of a set of rectangles.<br><br></td><td align = 'left' rowspan = 2>```{r ggplotHistogramP, echo=FALSE, fig.width=5.5, fig.height=5.5, out.extra='class="thumb-L"'}ggplot(data = diamonds, aes(x = carat)) + geom_histogram(aes(fill = cut))```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotHistogramP, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>histogram layer</td><td align = 'left'>`_histogram`</td><td align = 'left'>`_bin`</td><td align = 'left'>dodge</td><td align = 'left'>x or y<br><br>`geom_histogram` bins continuous data and uses the number of cases in each bin as the height of a set of rectangles.<br><br></td><td align = 'left' rowspan = 2>```{r ggplotHistogram2, echo=FALSE, fig.width=5.5, fig.height=5.5, out.extra='class="thumb-L"'}ggplot(data = diamonds, aes(x = carat)) + geom_histogram(aes(fill = cut), position = 'dodge')```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotHistogram2, eval=FALSE}```</td></tr></tbody></table>### Density<br>plots ![](05_grammar_of_graphics_files/figure-html/ggplotDensity-s-1.png){class="thumb-s" #Gdensity-plots}`geom_density` constructs smooth density distributions from continuousvectors. The actual smoothed densities are calculated by its mainpairing function (`stat_density`). The following list describes themapping aesthetic properties associated with `geom_density` and`stat_density`. The entries in bold are compulsory. Note that densityplots are usually specified via the `geom_density` _function_ whichwill engage the `stat_density`. Therefore, confusingly, when calling`geom_density`, the compulsory paramaters are actually those requiredby `stat_density` (unless you indicated to use `stat_identity`).```{r ggplotDensity-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(data = diamonds, aes(x = carat)) + geom_density() + blank_theme```<details><summary>Show attributes</summary>| Parameter | `geom_density` | `stat_density` ||----------------------------------------------------------------------|----------------|----------------|| _aesthetics_ | | || **`x`** - variable to map to the x-axis | ✔ | ✔ || `group` - plot separate series without aesthetic differences | ✔ | ✔ || `alpha` - transparency | ✔ | ✔ || `colour` - colour of the points/lines | ✔ | ✔ || `fill` - inner colour of points/shapes | ✔ | ✔ || `linetype` - type of lines used to construct points/lines | ✔ | ✔ || `size` - thickness of the line | ✔ | ✔ || `weight` - weightings of values | ✔ | ✔ || | | || _additional parameters_ | | || `bw` - bandwidth smoothing (either sd of kernel or name of function) | "nrd0" | "nrd0" || `adjust` - multiplicate bandwidth adjustment | 1 | 1 || `kernel` - `density()` kernel to use | "gaussian" | "gaussian" || `n` - number of equ-spaced points to estimate density | 512 | 512 || `trim` - whether to trim the range of data | FALSE | FALSE || `orientation` - which axis (x or y) to operate on | NA | NA || | | || _Computed variables_ | | || `density` - density of points in bin | ✔ | || `count` - density * number of points | ✔ | || `scaled density` - density scaled to max of 1 | ✔ | || `ndensity` - density scaled to max of 1 | ✔ | |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>geom</th><th align = 'left'>stat</th><th align = 'left'>position</th><th align = 'left'>Aesthetic parameters / Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>density layer</td><td align = 'left'>`_density`</td><td align = 'left'>`_density`</td><td align = 'left'>identity</td><td align = 'left'>x or y<br><br></td><td align = 'left' rowspan = 2>```{r ggplotDensity, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data = diamonds, aes(x = carat)) + geom_density()```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotDensity, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>density layer</td><td align = 'left'>`_density`</td><td align = 'left'>`_density`</td><td align = 'left'>identity</td><td align = 'left'>x or y<br><br></td><td align = 'left' rowspan = 2>```{r ggplotDensity1, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data = diamonds, aes(x = carat)) + geom_density(bw=0.1)```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotDensity1, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>density layer</td><td align = 'left'>`_density`</td><td align = 'left'>`_density`</td><td align = 'left'>identity</td><td align = 'left'>x or y, fill or other grouping<br><br>Multiple densities with overlapping ranges</td><td align = 'left' rowspan = 2>```{r ggplotDensityP, echo=FALSE, fig.width=5.5, fig.height=5.5, out.extra='class="thumb-L"'}ggplot(data = diamonds, aes(x = carat)) + geom_density(aes(fill = cut), alpha=0.5)```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotDensityP, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>density layer</td><td align = 'left'>`_density`</td><td align = 'left'>`_density`</td><td align = 'left'>stack</td><td align = 'left'>x or y, fill or other grouping<br><br>Multiple densities stacked on top of one another</td><td align = 'left' rowspan = 2>```{r ggplotDensityP1, echo=FALSE, fig.width=5.5, fig.height=5.5, out.extra='class="thumb-L"'}ggplot(data = diamonds, aes(x = carat)) + geom_density(aes(fill = cut), position = 'stack')```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotDensityP1, eval=FALSE}```</td></tr></tbody></table>### Violin<br>plots ![](05_grammar_of_graphics_files/figure-html/ggplotViolin-s-1.png){class="thumb-s" #Gviolin-plots} `geom_violin` constructs violin plots. Violin plots are a blend onboxplot and density plots in that they are a density plot mirroredacross a central axis and displayed similarly to a boxplot. Sincethey are derived from density plots, `geom_violin` and its stat(`stat_ydensity`) have most of the same parameters as`geom_density`/`stat_density`. Violin plots are a useful way topresent continuous distributions that have greater granularity thanboxplots.```{r ggplotViolin-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(data = diamonds, aes(x = carat, y = carat)) + geom_violin(orientation = 'x') + blank_theme```<details><summary>Show attributes</summary>| Parameter | `geom_violin` | `stat_ydensity` ||----------------------------------------------------------------------|----------------|----------------|| _aesthetics_ | | || **`x`** - variable to map to the x-axis | ✔ | ✔ || `group` - plot separate series without aesthetic differences | ✔ | ✔ || `alpha` - transparency | ✔ | ✔ || `colour` - colour of the points/lines | ✔ | ✔ || `fill` - inner colour of points/shapes | ✔ | ✔ || `linetype` - type of lines used to construct points/lines | ✔ | ✔ || `size` - thickness of the line | ✔ | ✔ || `weight` - weightings of values | ✔ | ✔ || | | || _additional parameters_ | | || `bw` - bandwidth smoothing (either sd of kernel or name of function) | "nrd0" | "nrd0" || `adjust` - multiplicate bandwidth adjustment | 1 | 1 || `kernel` - `density()` kernel to use | "gaussian" | "gaussian" || `n` - number of equ-spaced points to estimate density | 512 | 512 || `trim` - whether to trim the range of data | FALSE | FALSE || `orientation` - which axis (x or y) to operate on | NA | NA || | | || _Computed variables_ | | || `density` - density of points in bin | ✔ | || `count` - density * number of points | ✔ | || `scaled density` - density scaled to max of 1 | ✔ | || `ndensity` - density scaled to max of 1 | ✔ | |: {.primary .bordered .sm .paramsTable}</details>```{r ggplotViolinP5, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(diamonds) + geom_violin(aes(y = carat, x = cut))```<table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>geom</th><th align = 'left'>stat</th><th align = 'left'>position</th><th align = 'left'>Aesthetic parameters / Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>violin</td><td align = 'left'>`_violin`</td><td align = 'left'>`_ydensity`</td><td align = 'left'>dodge</td><td align = 'left'>x<br><br>plot of quantiles, whiskers and outliers.</td><td align = 'left' rowspan = 2>```{r ggplotViolin, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(diamonds) + geom_violin(aes(y = carat, x = carat), orientation = 'x')```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotViolin, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>violin</td><td align = 'left'>`_violin`</td><td align = 'left'>`_ydensity`</td><td align = 'left'>dodge</td><td align = 'left'>y<br><br>plot of quantiles, whiskers and outliers.</td><td align = 'left' rowspan = 2>```{r ggplotViolin1, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(diamonds) + geom_violin(aes(y = carat, x = carat), orientation = 'y')```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotViolin1, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>conditional violin</td><td align = 'left'>`_violin`</td><td align = 'left'>`_ydensity`</td><td align = 'left'>dodge</td><td align = 'left'>y,x<br><br>plot of quantiles, whiskers and outliers.</td><td align = 'left' rowspan = 2>```{r ggplotViolinP, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(diamonds) + geom_violin(aes(y = carat, x = cut))```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotViolinP, eval=FALSE}```</td></tr></tbody></table>### QQ<br>plots ![](05_grammar_of_graphics_files/figure-html/ggplotQQ-s-1.png){class="thumb-s" #Gqq-plots} `geom_qq` constructs quantile-quantile (QQ) plots. QQ plotsillustrate the quantiles of the sample distribution against thequantiles expected for the theoretical distribution. A straight lineimplies that the sample distribution matches the theoreticaldistribution well. Deviations (typically at the tails) imply a lackof match. To help assess deviations from linearity, the`geom_qq_line`/`stat_qq_line` _function_ depicts the ideal match as astraight line for comparison.<!-- the following generates the small thumbnail to use in the tab icon-->```{r ggplotQQ-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(data = diamonds, aes(sample = carat)) + geom_qq() + blank_theme```<!-- the following two plots generate the thumbnails to use in the gallery-->```{r ggplotQQ, include = FALSE, echo=FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(diamonds) + geom_qq(aes(sample = carat))``````{r ggplotQQP, include = FALSE, echo=FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(diamonds) + geom_qq(aes(sample = carat, color=cut))```<details><summary>Show attributes</summary>| Parameter | `geom_qq` | `stat_qq` ||------------------------------------------------------------------------|--------------|--------------|| _aesthetics_ | | || **`sample`** - variable to map to the x-axis | ✔ | ✔ || `group` - plot separate series without aesthetic differences | ✔ | ✔ || `alpha` - transparency | ✔ | ✔ || `colour` - colour of the points/lines | ✔ | ✔ || `fill` - inner colour of points/shapes | ✔ | ✔ || `linetype` - type of lines used to construct points/lines | ✔ | ✔ || `size` - thickness of the line | ✔ | ✔ || `weight` - weightings of values | ✔ | ✔ || | | || _additional parameters_ | | || `distribution` - function that calculates quantiles for a distribution | stats::qnorm | stats::qnorm || `dparams` - additional parameters for the distribution | list() | list() || `orientation` - which axis (x or y) to operate on | NA | NA || | | || _Computed variables_ | | || `sample` - sample quantiles | ✔ | || `theoretical` - theoretical quantiles | ✔ | |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>geom</th><th align = 'left'>stat</th><th align = 'left'>position</th><th align = 'left'>Aesthetic parameters / Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>QQ plot</td><td align = 'left'>`_qq`</td><td align = 'left'>`_qq`</td><td align = 'left'>identity</td><td align = 'left'>sample<br><br></td><td align = 'left' rowspan = 2>```{r ggplotQQ1, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(diamonds, aes(sample = carat)) + geom_qq() + geom_qq_line()```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotQQ1, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>QQ plot</td><td align = 'left'>`_qq`</td><td align = 'left'>`_qq`</td><td align = 'left'>identity</td><td align = 'left'>sample<br><br>this example will explore a Gamma theoretical distribution (rather than the default Gaussian). When specifying an alternative distribution, it may be necessary to supply certain distribution parameters. In this case, the appropriate shape parameter for the Gamma distribution is required. This is first estimated via the `fitdistr()` _function_ from the `MASS` _package`.</td><td align = 'left' rowspan = 2>```{r ggplotQQ2, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}shape <- MASS::fitdistr(diamonds$carat, densfun = "gamma")$estimate["shape"]ggplot(diamonds, aes(sample = carat)) + geom_qq(distribution = stats::qgamma, dparams = list(shape = shape)) + geom_qq_line(distribution = stats::qgamma, dparams = list(shape = shape))```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotQQ2, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>QQ plot</td><td align = 'left'>`_qq`</td><td align = 'left'>`_qq`</td><td align = 'left'>identity</td><td align = 'left'>sample<br><br></td><td align = 'left' rowspan = 2>```{r ggplotQQ3, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(diamonds, aes(sample = carat, colour = cut)) + geom_qq() + geom_qq_line()```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotQQ3, eval=FALSE}```</td></tr></tbody></table>### Bar<br>plots ![](05_grammar_of_graphics_files/figure-html/ggplotBar-s-1.png){class="thumb-s" #Gbar-plots} `geom_bar` constructs barcharts. By default, the bins of each bar along with the associated bar heights are calculated by the `stats_count` function. The following list describes the mapping aesthetic properties associated with `geom_bar` and `stats_count`. The entries in bold are compulsory.<!-- the following generates the small thumbnail to use in the tab icon-->```{r ggplotBar-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(data = diamonds, aes(x = cut)) + geom_bar() + blank_theme```<!-- the following three plots generate the thumbnails to use in the gallery-->```{r ggplotBar, echo=FALSE, include=FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(diamonds) + geom_bar(aes(x = cut))``````{r ggplotBarS, echo=FALSE,include=FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(diamonds) + geom_bar(aes(x = cut, fill = clarity))``````{r ggplotBarP, echo=FALSE, include=FALSE,fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(diamonds) + geom_bar(aes(x = cut, fill = clarity), position = 'dodge')```<details><summary>Show attributes</summary>| Parameter | `geom_bar` | `stat_count` ||--------------------------------------------------------------|------------|--------------|| _aesthetics_ | | || **`x`** or **`y`** - variable to map to the x/y-axis | ✔ | ✔ || `group` - plot separate series without aesthetic differences | ✔ | ✔ || `alpha` - transparency | ✔ | ✔ || `colour` - colour of the points/lines | ✔ | ✔ || `fill` - inner colour of points/shapes | ✔ | ✔ || `linetype` - type of lines used to construct points/lines | ✔ | ✔ || `size` - thickness of the line | ✔ | ✔ || `weight` - weightings of values | ✔ | ✔ || | | || _additional parameters_ | | || `width` - width of the bars | NULL | NULL || `orientation` - which axis (x or y) to operate on | NA | NA || | | || _Computed variables_ | | || `count` - number of points to bin | ✔ | || `prop` - groupwise proportion | ✔ | |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>geom</th><th align = 'left'>stat</th><th align = 'left'>position</th><th align = 'left'>Aesthetic parameters / Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>Bar plot</td><td align = 'left'>`_bar`</td><td align = 'left'>`_count`</td><td align = 'left'>stack</td><td align = 'left'>sample<br><br></td><td align = 'left' rowspan = 2>```{r ggplotBar1, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(diamonds, aes(x = cut)) + geom_bar()```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotBar1, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>Stacked bar plot</td><td align = 'left'>`_bar`</td><td align = 'left'>`_count`</td><td align = 'left'>stack</td><td align = 'left'>sample<br><br>by default a _viridis_ colour blind safe palette is applied.</td><td align = 'left' rowspan = 2>```{r ggplotBar2, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(diamonds, aes(x = cut, fill = clarity)) + geom_bar()```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotBar2, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>Conditional bar plot</td><td align = 'left'>`_bar`</td><td align = 'left'>`_count`</td><td align = 'left'>dodge2</td><td align = 'left'>sample<br><br>by default a _viridis_ colour blind safe palette is applied.</td><td align = 'left' rowspan = 2>```{r ggplotBar3, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(diamonds, aes(x = cut, fill = clarity)) + geom_bar(position = "dodge2")```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotBar3, eval=FALSE}```</td></tr></tbody></table>### Dot<br>plots ![](05_grammar_of_graphics_files/figure-html/ggplotDot-s-1.png){class="thumb-s" #Gdot-plots}<!-- the following generates the small thumbnail to use in the tab icon-->```{r ggplotDot-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(data = CO2, aes(x = uptake)) + geom_dotplot(binwidth = 3) + blank_theme```<!-- the following two plots generate the thumbnails to use in the gallery-->```{r ggplotDot, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(CO2) + geom_dotplot(aes(x = uptake), binwidth = 3)``````{r ggplotDotP, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(CO2) + geom_dotplot(aes(x = uptake, fill = Type), stackgroups = TRUE, binpositions = 'all', binwidth=3, dotsize=1) ````geom_dotplot` draws dotplots of continuous data after binning thedata. This geom is unusual in that it does not have an associated`stat`. Instead, it calls its own density function. Dotplots arereally only useful for small datasets. The reason for this is thatthe counts in each bin are represented by circles (the size of whichis determined by the bin width) and thus if the counts are high, thestack of circles will extend beyond the y-axis (unless the stack ratiois altered). Moreover, the y-axis scale is meaningless.The following list describes the mapping aesthetic propertiesassociated with `geom_dotplot`. The entries in bold are compulsory.<details><summary>Show attributes</summary>| Parameter | `geom_histogram` ||----------------------------------------------------------------------------|------------------|| _aesthetics_ | || **`x`** - variable to map to the x-axis | ✔ || `group` - plot separate series without aesthetic differences | ✔ || `alpha` - transparency | ✔ || `colour` - colour of the points/lines | ✔ || `fill` - inner colour of points/shapes | ✔ || `linetype` - type of lines used to construct points/lines | ✔ || `size` - thickness of the line | ✔ || `weight` - weightings of values | ✔ || | || _additional parameters_ | || `binwidth` - (maximum) bin width | NULL || `binaxis` - the axis to bin along | "x" || `method` - method for binning | "dotdensity" || `binpositions` - method for determining the position of dots | "bygroup" || `stackdir` - which direction to stack dots | "up" || `stackratio` - how close to stack dots | 1 || `dotsize` - diameter of dot relative to binwidth | 1 || `stackgroups` - whether to stack dots across groups | FALSE || `origin` - origin of first bin | NULL || `right` - should bin intervals be closed on the right (a, b] or not [a, b) | TRUE || `width` - when `binaxis` is "y", the spacing of dot stacks for dodging | 0.9 || `drop` - whether to remove bins with zero counts | FALSE || | || _Computed variables_ | || `count` - number of points in bin | ✔ || `density` - density of points in bin | ✔ || `ncount` - counts scaled to max of 1 | ✔ || `ndensity` - density scaled to max of 1 | ✔ |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>geom</th><th align = 'left'>stat</th><th align = 'left'>position</th><th align = 'left'>Aesthetic parameters / Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>dotplot layer</td><td align = 'left'>`_dotplot`</td><td align = 'left'></td><td align = 'left'>identity</td><td align = 'left'>x or y<br><br></td><td align = 'left' rowspan = 2>```{r ggplotDot1, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = uptake)) + geom_dotplot()```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotDot1, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>dotplot layer</td><td align = 'left'>`_dotplot`</td><td align = 'left'></td><td align = 'left'>identity</td><td align = 'left'>x or y<br><br></td><td align = 'left' rowspan = 2>```{r ggplotDot2, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = uptake)) + geom_dotplot(binwidth = 3)```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotDot2, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>dotplot layer</td><td align = 'left'>`_dotplot`</td><td align = 'left'></td><td align = 'left'>identity</td><td align = 'left'>x or y<br><br></td><td align = 'left' rowspan = 2>```{r ggplotDot3, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = uptake, fill = Type)) + geom_dotplot(stackgroups = TRUE, binpositions = 'all', binwidth=3, dotsize=1) ```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotDot3, eval=FALSE}```</td></tr></tbody></table>### Scatterplot<br>matrix ![](05_grammar_of_graphics_files/figure-html/ggplotSPLOM-s-1.png){class="thumb-s" #Gscatterplot-matrix}The `ggpairs()` _function_ is not actually part of the `ggplot2`_package_. Rather it is part of its own package (`GGally`).Nevertheless, this graphic, in which all pairs of variables areplotted in a matrix of panels can be a very useful way to explore theindividual and relational characteristics of multiple variablessimultaneously.<!-- the following generates the small thumbnail to use in the tab icon-->```{r ggplotSPLOM-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggpairs(dplyr::select(iris, Sepal.Length, Sepal.Width, Species), colour = "Species", upper = list(continuous = "density", combo = "box"), diag = list(continuous = "density"), lower = list(continuous = "smooth"), axisLabels = "show")## ggpairs(data = CO2, colour = "Type", upper = list(continuous = "density",## combo = "box"), diag = list(continuous = "density"),## lower = list(continuous = "smooth"), axisLabels = "show")```<!-- the following plot generates the thumbnails to use in the gallery-->```{r ggplotScatterplotMatrix, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggpairs(iris, colour = "Species", upper = list(continuous = "density", combo = "box"), diag = list(continuous = "density"), lower = list(continuous = "smooth"), axisLabels = "show")```<details><summary>Show attributes</summary>| Parameter | `geom_histogram` ||-----------------------------------------------------------------------------|-------------------------------|| _aesthetics_ | || `group` - plot separate series without aesthetic differences | ✔ || `alpha` - transparency | ✔ || `colour` - colour of the points/lines | ✔ || `fill` - inner colour of points/shapes | ✔ || `linetype` - type of lines used to construct points/lines | ✔ || `size` - thickness of the line | ✔ || `weight` - weightings of values | ✔ || | || _additional parameters_ | || `columns` - which columns to include | 1:ncol(data) || `title,xlab,ylab` - titles | NULL || `upper,lower` - functions for plots to appear in off diagonal panels | ... || `diag` - function for plots to appear in diagonal panels | ... || `axisLabels` - whether and how to display axis labels | c("show", "internal", "none") || `columnLabels` - label names to display | colnames(data[columns]) || `labeller` - facet labeller function | "label_value" || `switch` - which (if any) facet labels to switch position | NULL || `showStrips` - whether to display all strips (panel banners) | NULL || `legend` - whether or position of legend | NULL || `cardinality_threshold` - max number of factor levels permitted | 15 || `progress` - whether to show a progress bar while computing (if >15 panels) | NULL || `proportions` - amount of area given to each plot | NULL |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>geom</th><th align = 'left'>stat</th><th align = 'left'>position</th><th align = 'left'>Aesthetic parameters / Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>dotplot layer</td><td align = 'left'>`_dotplot`</td><td align = 'left'></td><td align = 'left'>identity</td><td align = 'left'>x or y<br><br></td><td align = 'left' rowspan = 2>```{r ggplotdotplot1, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggpairs(iris, colour = "Species", upper = list(continuous = "density", combo = "box"), diag = list(continuous = "density"), lower = list(continuous = "smooth"), axisLabels = "show")```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotdotplot1, eval=FALSE}```</td></tr></tbody></table>:::## Visualising trends ::: {.panel-tabset}### Scatterplots<br><br> ![](05_grammar_of_graphics_files/figure-html/ggplotScatterplot-s-1.png){class="thumb-s" #Gscatterplots}```{r ggplotScatterplot-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(filter(CO2, Plant == first(Plant))) + geom_point(aes(x = conc, y = uptake)) + blank_theme``````{r ggplotScatterplot, include = FALSE, echo=FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(filter(CO2, Plant == first(Plant))) + geom_point(aes(x = conc, y = uptake)) ```[see points](#Gpoints)### Line<br>plots ![](05_grammar_of_graphics_files/figure-html/ggplotLines-s-1.png){class="thumb-s" #Gline-plots}`geom_line` draws lines joining coordinates. Typically the _stat_ usedis `stat_identity` as we wish to use the values in two continuousvectors as the coordinates of each line segment. `geom_line` differsfrom `geom_path` in that the former first orders the data according tothe x-axis.```{r ggplotLines-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(filter(CO2, Plant == first(Plant))) + geom_line(aes(x = conc, y = uptake)) + blank_theme``````{r ggplotLine, include=FALSE, echo=FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(filter(CO2, Plant == first(Plant))) + geom_line(aes(x = conc, y = uptake)) ```<details><summary>Show attributes</summary>| Parameter | `geom_line` ||---------------------------------------------------------------|-------------|| _aesthetics_ | || **`x`** - variable to map to the x-axis | ✔ || **`y`** - variable to map to the other axis | ✔ || `group` - plot separate series without aesthetic differences | ✔ || `alpha` - transparency | ✔ || `colour` - colour of the points/lines | ✔ || `fill` - inner colour of points/shapes | ✔ || `linetype` - type of lines used to construct points/lines | ✔ || `size` - thickness of the line | ✔ || `weight` - weightings of values | ✔ || | || _additional parameters_ | || | || _Computed variables_ | |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>geom</th><th align = 'left'>stat</th><th align = 'left'>position</th><th align = 'left'>Aesthetic parameters / Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>line plot</td><td align = 'left'>`_line`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br>scatterplot on only the first level of `Plant`.</td><td align = 'left' rowspan = 2>```{r ggplotScatterplot1, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(filter(CO2, Plant == first(Plant))) + geom_line(aes(x = conc, y = uptake)) ```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotScatterplot1, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>line plot</td><td align = 'left'>`_line`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br>scatterplot of each `Plant` level.</td><td align = 'left' rowspan = 2>```{r ggplotScatterplot2, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_line(aes(group = Plant))```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotScatterplot2, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>line plot</td><td align = 'left'>`_line`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br>scatterplot of each `Plant` level coloured separately.</td><td align = 'left' rowspan = 2>```{r ggplotScatterplot3, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_line(aes(colour = Plant))```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotScatterplot3, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>line plot</td><td align = 'left'>`_line`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br>scatterplot of mean across all `Plant`s.</td><td align = 'left' rowspan = 2>```{r ggplotScatterplot4, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_line(stat = "summary", fun = mean)```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotScatterplot4, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>line plot</td><td align = 'left'>`_line`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br>scatterplot of mean across all `Plant`s.</td><td align = 'left' rowspan = 2>```{r ggplotScatterplot6, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_line(stat = "summary", fun = mean, aes(color = Type))```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotScatterplot6, eval=FALSE}```</td></tr></tbody></table>### Smoother<br>plots ![](05_grammar_of_graphics_files/figure-html/ggplotSmoother-s-1.png){class="thumb-s" #Gsmoother-plots}`geom_smooth` draws smooths lines (and 95% confidence intervals)through data clouds. Typically the stat used is `stat_smooth` which inturn engages one of the available smoothing methods (e.g. `lm`, `glm`,`gam`, `loess` or `rlm`).```{r ggplotSmoother-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(filter(CO2, Plant == first(Plant))) + geom_smooth(aes(x = conc, y = uptake)) + blank_theme``````{r ggplotSmooth, include=FALSE, echo=FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(filter(CO2, Plant == first(Plant))) + geom_smooth(aes(x = conc, y = uptake)) ```<details><summary>Show attributes</summary>| Parameter | `geom_smooth` | `geom_smooth` ||----------------------------------------------------------------------|---------------|---------------|| _aesthetics_ | | || **`x`** - variable to map to the x-axis | ✔ | ✔ || **`y`** - variable to map to the other axis | ✔ | ✔ || `group` - plot separate series without aesthetic differences | ✔ | ✔ || `alpha` - transparency | ✔ | ✔ || `colour` - colour of the points/lines | ✔ | ✔ || `fill` - inner colour of points/shapes | ✔ | ✔ || `linetype` - type of lines used to construct points/lines | ✔ | ✔ || `size` - thickness of the line | ✔ | ✔ || `weight` - weightings of values | ✔ | ✔ || | | || _additional parameters_ | | || `method` - smoothing method (modelling function) | NULL | NULL || `formula` - formula to use in smoothing function | NULL | NULL || `se` - whether to display confidence intervals | TRUE | TRUE || `n` - number of points at which to evaluate the smoother | | 80 || `span` - degree of smoothing for loess smoother | | 0.75 || `fullrange` - whether the fit should span full range (or just data) | | FALSE || `method.args` - additional arguments passed on to modelling function | | list() || | | || _Computed variables_ | | || `y` or `x` - the predicted value | ✔ | || `ymin` or `xmin` - lower confidence interval | ✔ | || `ymax` or `xmax` - upper confidence interval | ✔ | || `se` - standard error | ✔ | |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>geom</th><th align = 'left'>stat</th><th align = 'left'>position</th><th align = 'left'>Aesthetic parameters / Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>line plot</td><td align = 'left'>`_line`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br>scatterplot on only the first level of `Plant`.</td><td align = 'left' rowspan = 2>```{r ggplotSmoother1, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(filter(CO2, Plant == first(Plant))) + geom_smooth(aes(x = conc, y = uptake)) ```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotSmoother1, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>line plot</td><td align = 'left'>`_line`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br>scatterplot of each `Plant` level.</td><td align = 'left' rowspan = 2>```{r ggplotSmoother2, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_smooth(aes(colour = Type))```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotSmoother2, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>line plot</td><td align = 'left'>`_line`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br>scatterplot of each `Plant` level coloured separately.</td><td align = 'left' rowspan = 2>```{r ggplotSmoother3, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_smooth(aes(colour = Type, linetype = Treatment))```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotSmoother4, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>line plot</td><td align = 'left'>`_line`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br>scatterplot of each `Plant` level coloured separately.</td><td align = 'left' rowspan = 2>```{r ggplotSmoother4, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(data = BOD, aes(x = Time, y = demand)) + geom_smooth(method = "lm", formula = "y ~ x")```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotSmoother4, eval=FALSE}```</td></tr></tbody></table>### Tiles<br><br> ![](05_grammar_of_graphics_files/figure-html/ggplotTiles-s-1.png){class="thumb-s" #Gtiles}`geom_tile` constructs heat maps given x,y coordinates and a z valueto associate with the fill of each tile. Note, the data must be acomplete grid.```{r ggplotTiles-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(faithfuld, aes(y = eruptions, x = waiting)) + geom_tile(aes(fill = density)) + blank_theme``````{r ggplotTile, include = FALSE, echo=FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(faithfuld, aes(y = eruptions, x = waiting)) + geom_tile(aes(fill = density)) ```<details><summary>Show attributes</summary>| Parameter | `geom_tile` ||--------------------------------------------------------------|-------------|| _aesthetics_ | || **`x`** - variable to map to the x-axis | ✔ || **`y`** - variable to map to the other axis | ✔ || `group` - plot separate series without aesthetic differences | ✔ || `alpha` - transparency | ✔ || `colour` - colour of the points/lines | ✔ || `fill` - inner colour of points/shapes | ✔ || `linetype` - type of lines used to construct points/lines | ✔ || `size` - thickness of the line | ✔ || `weight` - weightings of values | ✔ || | || _additional parameters_ | || `linejoin` - line join style | "mitre" || | || _Computed variables_ | |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>geom</th><th align = 'left'>stat</th><th align = 'left'>position</th><th align = 'left'>Aesthetic parameters / Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>tile plot</td><td align = 'left'>`_tile`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br>heatmap of 2d density.</td><td align = 'left' rowspan = 2>```{r ggplotTiles1, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(faithfuld, aes(y = eruptions, x = waiting)) + geom_tile(aes(fill = density)) ```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotTiles1, eval=FALSE}```</td></tr></tbody></table>### Raster<br><br> ![](05_grammar_of_graphics_files/figure-html/ggplotRasters-s-1.png){class="thumb-s" #Grasters}`geom_raster` is similar to `geom_tile` in that it constructs heatmaps given x,y coordinates and a z value to associate with the fill ofeach tile. However, unlike `geom_tile`, a full grid is not required as`geom_raster` is able to interpolate over the grid. The interpolationcan also smooth out the grid surface. This interpolation does make`geom_raster` slower than `geom_tile`.```{r ggplotRasters-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(faithfuld, aes(y = eruptions, x = waiting)) + geom_raster(aes(fill = density)) + blank_theme``````{r ggplotRaster, include = FALSE, echo=FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(faithfuld, aes(y = eruptions, x = waiting)) + geom_raster(aes(fill = density)) ```<details><summary>Show attributes</summary>| Parameter | `geom_raster` ||--------------------------------------------------------------|---------------|| _aesthetics_ | || **`x`** - variable to map to the x-axis | ✔ || **`y`** - variable to map to the other axis | ✔ || `group` - plot separate series without aesthetic differences | ✔ || `alpha` - transparency | ✔ || `colour` - colour of the points/lines | ✔ || `fill` - inner colour of points/shapes | ✔ || `linetype` - type of lines used to construct points/lines | ✔ || `size` - thickness of the line | ✔ || `weight` - weightings of values | ✔ || | || _additional parameters_ | || `hjust` - line join style | 0.5 || `vjust` - line join style | 0.5 || `interpolate` - line join style | FALSE || | || _Computed variables_ | |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>geom</th><th align = 'left'>stat</th><th align = 'left'>position</th><th align = 'left'>Aesthetic parameters / Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>raster plot</td><td align = 'left'>`_raster`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br>heatmap of 2d density.</td><td align = 'left' rowspan = 2>```{r ggplotRasters1, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(faithfuld, aes(y = eruptions, x = waiting)) + geom_raster(aes(fill = density)) ```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotRasters1, eval=FALSE}```</td></tr></tbody></table>### Contours<br><br> ![](05_grammar_of_graphics_files/figure-html/ggplotContours-s-1.png){class="thumb-s" #Gcontours}`geom_contour` constructs contour maps given x,y coordinates and a zvalue from which to calculate each contour.```{r ggplotContours-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(faithfuld, aes(y = eruptions, x = waiting)) + geom_contour(aes(z = density)) + blank_theme``````{r ggplotContour, include = FALSE, echo=FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(faithfuld, aes(y = eruptions, x = waiting)) + geom_contour(aes(z = density)) ```<details><summary>Show attributes</summary>| Parameter | `geom_contour` | `stat_contour` ||--------------------------------------------------------------|----------------|----------------|| _aesthetics_ | | || **`x`** - variable to map to the x-axis | ✔ | ✔ || **`y`** - variable to map to the y axis | ✔ | ✔ || **`z`** - variable to map to the z axis | ✔ | ✔ || `group` - plot separate series without aesthetic differences | ✔ | ✔ || `alpha` - transparency | ✔ | ✔ || `colour` - colour of the points/lines | ✔ | ✔ || `fill` - inner colour of points/shapes | ✔ | ✔ || `linetype` - type of lines used to construct points/lines | ✔ | ✔ || `size` - thickness of the line | ✔ | ✔ || `weight` - weightings of values | ✔ | ✔ || | | || _additional parameters_ | | || `bins` - number of contour bins | NULL | NULL || `binwidth` - width of contour bins | NULL | NULL || `breaks` - numer of contour bins (alternative to bins) | NULL | NULL || `lineend` - line end style | "butt" | "butt" || `linejoin` - line join style | "round" | "round" || `linemitre` - line mitre style | 10 | 10 || | | || _Computed variables_ | | || `level` - bin boundaries | ✔ | || `level_low, level_high, level_mid` - bin boundaries per band | ✔ | || `nlevel` - height of contour, scaled to max of 1 | ✔ | || `piece` - contour piece | ✔ | |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>geom</th><th align = 'left'>stat</th><th align = 'left'>position</th><th align = 'left'>Aesthetic parameters / Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>contour plot</td><td align = 'left'>`_contour`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br>contour plot of 2d density in relation to eruption and waiting times.</td><td align = 'left' rowspan = 2>```{r ggplotContours1, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(faithfuld, aes(y = eruptions, x = waiting)) + geom_contour(aes(z = density)) ```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotContours1, eval=FALSE}```</td></tr></tbody></table>### Filled<br>contour ![](05_grammar_of_graphics_files/figure-html/ggplotFilledContours-s-1.png){class="thumb-s" #Gfilled-contour}`geom_contour` constructs contour maps given x,y coordinates and a zvalue from which to calculate each contour.```{r ggplotFilledContours-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(faithfuld, aes(y = eruptions, x = waiting)) + geom_contour_filled(aes(z = density)) + blank_theme``````{r ggplotFilledContour, include = FALSE, echo=FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(faithfuld, aes(y = eruptions, x = waiting)) + geom_contour_filled(aes(z = density)) ```<details><summary>Show attributes</summary>| Parameter | `geom_contour_filled` | `stat_contour_filled` ||--------------------------------------------------------------|-----------------------|-----------------------|| _aesthetics_ | | || **`x`** - variable to map to the x-axis | ✔ | ✔ || **`y`** - variable to map to the y axis | ✔ | ✔ || **`z`** - variable to map to the z axis | ✔ | ✔ || `group` - plot separate series without aesthetic differences | ✔ | ✔ || `alpha` - transparency | ✔ | ✔ || `colour` - colour of the points/lines | ✔ | ✔ || `fill` - inner colour of points/shapes | ✔ | ✔ || `linetype` - type of lines used to construct points/lines | ✔ | ✔ || `size` - thickness of the line | ✔ | ✔ || `weight` - weightings of values | ✔ | ✔ || | | || _additional parameters_ | | || `bins` - number of contour bins | NULL | NULL || `binwidth` - width of contours | NULL | NULL || `breaks` - sets contour breaks (alternative to bins) | NULL | NULL || `lineend` - line end style | "butt" | "butt" || `linejoin` - line join style | "round" | "round" || `linemitre` - line mitre style | 10 | 10 || | | || _Computed variables_ | | || `level` - bin boundaries | ✔ | || `nlevel` - height of filled_contour, scaled to max of 1 | ✔ | || `piece` - filled_contour piece | ✔ | |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>geom</th><th align = 'left'>stat</th><th align = 'left'>position</th><th align = 'left'>Aesthetic parameters / Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>filled_contour plot</td><td align = 'left'>`_filled_contour`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br>filled contour plot of 2d density in relation to eruption and waiting times.</td><td align = 'left' rowspan = 2>```{r ggplotFilled_Contours1, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(faithfuld, aes(y = eruptions, x = waiting)) + geom_contour_filled(aes(z = density)) ```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotFilled_Contours1, eval=FALSE}```</td></tr></tbody></table>:::## Visualising uncertainty ::: {.panel-tabset}### Error<br>bars ![](05_grammar_of_graphics_files/figure-html/ggplotErrorbars-s-1.png){class="thumb-s" #Gerror-bars}`geom_errorbar` draws error bars based on upper and lower levels of yassociated with each level of x.```{r ggplotErrorbars-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}warpbreaks.sum <- warpbreaks %>% group_by(wool) %>% summarise(mean_se(breaks))ggplot(warpbreaks.sum, aes(y = y, x = wool, ymin = ymin, ymax = ymax)) + geom_errorbar() + blank_theme``````{r ggplotErrorbar, include=FALSE, echo=FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}warpbreaks.sum <- warpbreaks %>% group_by(wool) %>% summarise(mean_se(breaks))ggplot(warpbreaks.sum, aes(y = y, x = wool, ymin = ymin, ymax = ymax)) + geom_errorbar()```<details><summary>Show attributes</summary>| Parameter | `geom_errorbar` ||--------------------------------------------------------------|-----------------|| _aesthetics_ | || **`x`** - variable to map to the x-axis | ✔ || **`y`** - variable to map to the y axis | ✔ || `group` - plot separate series without aesthetic differences | ✔ || `alpha` - transparency | ✔ || `colour` - colour of the points/lines | ✔ || `fill` - inner colour of points/shapes | ✔ || `linetype` - type of lines used to construct points/lines | ✔ || `size` - thickness of the line | ✔ || `weight` - weightings of values | ✔ || | || _additional parameters_ | || `width` - width of the caps on the bars | 1 || | || _Computed variables_ | |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>geom</th><th align = 'left'>stat</th><th align = 'left'>position</th><th align = 'left'>Aesthetic parameters / Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>error bars</td><td align = 'left'>`_errorbar`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br></td><td align = 'left' rowspan = 2>```{r ggplotErrorbars1, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}warpbreaks.sum <- warpbreaks %>% group_by(wool) %>% summarise(mean_se(breaks))ggplot(warpbreaks.sum, aes(y = y, x = wool, ymin = ymin, ymax = ymax)) + geom_errorbar() ```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotErrorbars1, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>error bars</td><td align = 'left'>`_errorbar`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br></td><td align = 'left' rowspan = 2>```{r ggplotErrorbars2, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}warpbreaks.sum <- warpbreaks %>% group_by(wool) %>% summarise(mean_se(breaks))ggplot(warpbreaks.sum, aes(y = y, x = wool, ymin = ymin, ymax = ymax)) + geom_errorbar(width = 0.25) ```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotErrorbars2, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>error bars</td><td align = 'left'>`_errorbar`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br></td><td align = 'left' rowspan = 2>```{r ggplotErrorbars3, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}warpbreaks.sum <- warpbreaks %>% group_by(wool) %>% summarise(mean_se(breaks))ggplot(warpbreaks.sum, aes(y = y, x = wool, ymin = ymin, ymax = ymax)) + geom_crossbar(width = 0.25) ```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotErrorbars3, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>error bars</td><td align = 'left'>`_errorbar`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br></td><td align = 'left' rowspan = 2>```{r ggplotErrorbars4, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}warpbreaks.sum <- warpbreaks %>% group_by(wool, tension) %>% summarise(mean_se(breaks))ggplot(warpbreaks.sum, aes(y = y, x = tension, ymin = ymin, ymax = ymax)) + geom_errorbar(aes(colour = wool), position = position_dodge(width = 0.5), width = 0.25) ```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotErrorbars4, eval=FALSE}```</td></tr></tbody></table>### Line<br>ranges ![](05_grammar_of_graphics_files/figure-html/ggplotLineranges-s-1.png){class="thumb-s" #Gline-ranges}`geom_lineranges` draws uncertainty bars based on upper and lower levels of yassociated with each level of x.```{r ggplotLineranges-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}warpbreaks.sum <- warpbreaks %>% group_by(wool) %>% summarise(mean_se(breaks))ggplot(warpbreaks.sum, aes(y = y, x = wool, ymin = ymin, ymax = ymax)) + geom_linerange() + blank_theme``````{r ggplotLinerange, include=FALSE, echo=FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}warpbreaks.sum <- warpbreaks %>% group_by(wool) %>% summarise(mean_se(breaks))ggplot(warpbreaks.sum, aes(y = y, x = wool, ymin = ymin, ymax = ymax)) + geom_linerange()```<details><summary>Show attributes</summary>| Parameter | `geom_linerange` ||--------------------------------------------------------------|------------------|| _aesthetics_ | || **`x`** - variable to map to the x-axis | ✔ || **`y`** - variable to map to the y axis | ✔ || `group` - plot separate series without aesthetic differences | ✔ || `alpha` - transparency | ✔ || `colour` - colour of the points/lines | ✔ || `fill` - inner colour of points/shapes | ✔ || `linetype` - type of lines used to construct points/lines | ✔ || `size` - thickness of the line | ✔ || `weight` - weightings of values | ✔ || | || _additional parameters_ | || | || _Computed variables_ | |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>geom</th><th align = 'left'>stat</th><th align = 'left'>position</th><th align = 'left'>Aesthetic parameters / Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>line range</td><td align = 'left'>`_linerange`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br></td><td align = 'left' rowspan = 2>```{r ggplotLineranges1, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}warpbreaks.sum <- warpbreaks %>% group_by(wool) %>% summarise(mean_se(breaks))ggplot(warpbreaks.sum, aes(y = y, x = wool, ymin = ymin, ymax = ymax)) + geom_linerange() ```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotLineranges1, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>line ranges</td><td align = 'left'>`_lineranges`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br></td><td align = 'left' rowspan = 2>```{r ggplotLineranges2, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}warpbreaks.sum <- warpbreaks %>% group_by(wool, tension) %>% summarise(mean_se(breaks))ggplot(warpbreaks.sum, aes(y = y, x = tension, ymin = ymin, ymax = ymax)) + geom_linerange(aes(colour = wool), position = position_dodge(width = 0.5)) ```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotLineranges2, eval=FALSE}```</td></tr></tbody></table>### Point<br>ranges ![](05_grammar_of_graphics_files/figure-html/ggplotPointranges-s-1.png){class="thumb-s" #Gpoint-ranges}`geom_lineranges` draws uncertainty bars based on upper and lower levels of yassociated with each level of x.```{r ggplotPointranges-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}warpbreaks.sum <- warpbreaks %>% group_by(wool) %>% summarise(mean_se(breaks))ggplot(warpbreaks.sum, aes(y = y, x = wool, ymin = ymin, ymax = ymax)) + geom_pointrange() + blank_theme``````{r ggplotPointrange, include=FALSE, echo=FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}warpbreaks.sum <- warpbreaks %>% group_by(wool) %>% summarise(mean_se(breaks))ggplot(warpbreaks.sum, aes(y = y, x = wool, ymin = ymin, ymax = ymax)) + geom_pointrange()```<details><summary>Show attributes</summary>| Parameter | `geom_pointrange` ||----------------------------------------------------------------|-------------------|| _aesthetics_ | || **`x`** - variable to map to the x-axis | ✔ || **`y`** - variable to map to the y axis | ✔ || `group` - plot separate series without aesthetic differences | ✔ || `alpha` - transparency | ✔ || `colour` - colour of the points/lines | ✔ || `fill` - inner colour of points/shapes | ✔ || `linetype` - type of lines used to construct points/lines | ✔ || `size` - thickness of the line | ✔ || `weight` - weightings of values | ✔ || | || _additional parameters_ | || `flatten` - a multiplicative factor to increase the point size | 1 || | || _Computed variables_ | |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>geom</th><th align = 'left'>stat</th><th align = 'left'>position</th><th align = 'left'>Aesthetic parameters / Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>point range</td><td align = 'left'>`_pointrange`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br></td><td align = 'left' rowspan = 2>```{r ggplotPointranges1, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}warpbreaks.sum <- warpbreaks %>% group_by(wool) %>% summarise(mean_se(breaks))ggplot(warpbreaks.sum, aes(y = y, x = wool, ymin = ymin, ymax = ymax)) + geom_pointrange() ```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotPointranges1, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>point ranges</td><td align = 'left'>`_pointranges`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br></td><td align = 'left' rowspan = 2>```{r ggplotPointranges2, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}warpbreaks.sum <- warpbreaks %>% group_by(wool) %>% summarise(mean_se(breaks))ggplot(warpbreaks.sum, aes(y = y, x = wool, ymin = ymin, ymax = ymax)) + geom_pointrange(fatten = 5) ```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotPointranges2, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>point ranges</td><td align = 'left'>`_pointranges`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br></td><td align = 'left' rowspan = 2>```{r ggplotPointranges3, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}warpbreaks.sum <- warpbreaks %>% group_by(wool, tension) %>% summarise(mean_se(breaks))ggplot(warpbreaks.sum, aes(y = y, x = tension, ymin = ymin, ymax = ymax)) + geom_pointrange(aes(colour = wool), position = position_dodge(width = 0.5)) ```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotPointranges3, eval=FALSE}```</td></tr></tbody></table>### Ribbons<br><br> ![](05_grammar_of_graphics_files/figure-html/ggplotRibbons-s-1.png){class="thumb-s" #Gribbons}`geom_ribbon` draws uncertainty envelopes based on upper and lower levels of yassociated with each level of x.```{r ggplotRibbons-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}BOD.lm <- lm(demand ~ Time, data = BOD)xs <- seq(min(BOD$Time), max(BOD$Time), l = 100)pred <- data.frame(x = xs, predict(BOD.lm, newdata = data.frame(Time = xs), interval = "confidence"))ggplot(data = pred, aes(x = x, ymin = lwr, ymax = upr)) + geom_ribbon() + blank_theme```[see ribbons](#Gribbons):::## Other features ::: {.panel-tabset}### Straight<br>lines ![](05_grammar_of_graphics_files/figure-html/ggplotVline-s-1.png){class="thumb-s" #Gstraight-lines}`geom_vline` and `geom_hline` draw vertical and horizontal lines respectively. These are particularly useful for:- marking cross-hairs in ordination plots- providing extended guides along either axes.`geom_abline` is used for adding linear regression lines in the formof `y = bx + a` where `b` is the slope and `a` is the intercept (thisis where the `ab` in `abline` comes from).```{r ggplotVline-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(filter(CO2, Plant == first(Plant))) + geom_hline(yintercept = 25, linetype = 'dashed') + geom_vline(xintercept = 500, linetype = 'dashed') + geom_point(aes(x = conc, y = uptake)) + blank_theme``````{r ggplotVline, include=FALSE, echo=FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(filter(CO2, Plant == first(Plant))) + geom_hline(yintercept = 25, linetype = 'dashed') + geom_vline(xintercept = 500, linetype = 'dashed') + geom_point(aes(x = conc, y = uptake)) ```<details><summary>Show attributes</summary>| Parameter | `geom_vline` | `geom_hline` | `geom_abline` ||--------------------------------------------------------------|--------------|--------------|---------------|| _aesthetics_ | | | || **`xintercept`** - x-axis coordinate crossed by vline | ✔ | | || **`yintercept`** - y-axis coordinate crossed by hline | | ✔ | || **`intercept`** - y-axis coordinate crossed by abline | | | ✔ || **`slope`** - slope (gradient) of abline | | | ✔ || `group` - plot separate series without aesthetic differences | ✔ | | || `alpha` - transparency | ✔ | | || `colour` - colour of the points/lines | ✔ | | || `fill` - inner colour of points/shapes | ✔ | | || `linetype` - type of lines used to construct points/lines | ✔ | | || `size` - thickness of the line | ✔ | | || `weight` - weightings of values | ✔ | | || | | | || _additional parameters_ | | | || | | | || _Computed variables_ | | | |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>geom</th><th align = 'left'>stat</th><th align = 'left'>position</th><th align = 'left'>Aesthetic parameters / Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>straight lines</td><td align = 'left'>`_vline`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br></td><td align = 'left' rowspan = 2>```{r ggplotVline1, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(filter(CO2, Plant == first(Plant))) + geom_vline(xintercept = 500, linetype = 'dashed') + geom_point(aes(x = conc, y = uptake)) ```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotVline1, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>straight lines</td><td align = 'left'>`_hline`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br></td><td align = 'left' rowspan = 2>```{r ggplotVline2, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(filter(CO2, Plant == first(Plant))) + geom_hline(yintercept = 25, linetype = 'dashed') + geom_point(aes(x = conc, y = uptake)) ```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotVline2, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>straight lines</td><td align = 'left'>`_abline`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br></td><td align = 'left' rowspan = 2>```{r ggplotVline3, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(filter(CO2, Plant == first(Plant))) + geom_abline(slope = 0.01, intercept = 30, linetype = 'dashed') + geom_point(aes(x = conc, y = uptake)) ```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotVline3, eval=FALSE}```</td></tr></tbody></table>### Segments<br><br> ![](05_grammar_of_graphics_files/figure-html/ggplotSegment-s-1.png){class="thumb-s" #Gstraight-segments}`geom_segment` draws segments (separate lines) joining pairs ofcoordinates. These can be useful to represent vectors (e.g. windspeed and direction on a map) or movement, differences etc(particularly if given an arrow head).```{r ggplotSegment-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}BOD.lm <- lm(demand ~ Time, data = BOD)BOD$fitted <- fitted(BOD.lm)BOD$resid <- resid(BOD.lm)ggplot(BOD)+ geom_segment(aes(x=Time,y=demand, xend=Time,yend=fitted), arrow = arrow(length=unit(0.5, "cm"), ends = "first")) + blank_theme``````{r ggplotSegment, include=FALSE, echo=FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}BOD.lm <- lm(demand ~ Time, data = BOD)BOD$fitted <- fitted(BOD.lm)BOD$resid <- resid(BOD.lm)ggplot(BOD)+ geom_segment(aes(x=Time,y=demand, xend=Time,yend=fitted), arrow = arrow(length=unit(0.5, "cm"), ends = "first")) ```<details><summary>Show attributes</summary>| Parameter | `geom_segment` ||--------------------------------------------------------------|----------------|| _aesthetics_ | || **`x`** - x-axis coordinates for the start of lines | ✔ || **`y`** - y-axis coordinates for the start of lines | ✔ || **`xend`** - x-axis coordinates for the end of lines | ✔ || **`yend`** - y-axis coordinates for the end of lines | ✔ || `group` - plot separate series without aesthetic differences | ✔ || `alpha` - transparency | ✔ || `colour` - colour of the points/lines | ✔ || `fill` - inner colour of points/shapes | ✔ || `linetype` - type of lines used to construct points/lines | ✔ || `size` - thickness of the line | ✔ || `weight` - weightings of values | ✔ || | || _additional parameters_ | || `arrow` - specification of arrow heads | NULL || `arrow.fill` - fill colour of arrow head | NULL || `lineend` - style of line end | "butt" || `linejoin` - style of line join | "round" || | || _Computed variables_ | |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>geom</th><th align = 'left'>stat</th><th align = 'left'>position</th><th align = 'left'>Aesthetic parameters / Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>line segements</td><td align = 'left'>`_segement`</td><td align = 'left'>`_identity`</td><td align = 'left'>identity</td><td align = 'left'>x,y<br><br></td><td align = 'left' rowspan = 2>```{r ggplotSegment1, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}BOD.lm <- lm(demand ~ Time, data = BOD)BOD$fitted <- fitted(BOD.lm)BOD$resid <- resid(BOD.lm)ggplot(BOD)+ geom_segment(aes(x=Time,y=demand, xend=Time,yend=fitted), arrow = arrow(length=unit(0.5, "cm"), ends = "first")) ```</td></tr><tr><td align = 'left' font-size=2em colspan=5>```{r ggplotSegment1, eval=FALSE}```</td></tr></tbody></table>### Text<br><br> ![](05_grammar_of_graphics_files/figure-html/ggplotText-s-1.png){class="thumb-s" #Gtext}[see text](#Gtext):::# Coordinate system The coordinate system controls the nature and scale of the axes.## Types of coordinate systems ::: {.panel-tabset}### Cartesian<br>coordinates ![](05_grammar_of_graphics_files/figure-html/ggplotCoord-Cartesian-s-1.png){class="thumb-s" #Gcartesian-coordinates}```{r ggplotCoord-Cartesian-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(data = BOD, aes(x = Time, y = demand)) + geom_line() + coord_cartesian() + blank_theme``````{r ggplotCoord-Cartesian, include=FALSE, echo=FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(data = BOD, aes(x = Time, y = demand)) + geom_line() + coord_cartesian()```<details><summary>Show attributes</summary>| Parameter | `geom_segment` ||--------------------------------------------------------------------------------------------------------------|----------------|| `xlim` - limits for the x axis | NULL || `ylim` - limits for the y axis | NULL || `expand` - whether to add a small expansion to the axes to ensure geoms and axes do not overlapp | TRUE || `expand` - whether or not to provide a warning when the default coordinate system is being replaced | FALSE || `clip` - whether or not to clip plotting to within the plotting margins ("on") or to extend into the margins | "on" |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>coord</th><th align = 'left'>Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>Cartesian coordinates</td><td align = 'left'>`_cartesian`</td><td align = 'left'><br><br></td><td align = 'left' rowspan = 2>```{r ggplotCoord-Cartesian1, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data = BOD, aes(x = Time, y = demand)) + geom_line() + coord_cartesian()```</td></tr><tr><td align = 'left' font-size=2em colspan=3>```{r ggplotCoord-Cartesian, eval=FALSE}```</td></tr></tbody></table>### Polar<br>coordinates ![](05_grammar_of_graphics_files/figure-html/ggplotPolar-Polar-s-1.png){class="thumb-s" #Gpolar-coordinates}```{r ggplotPolar-Polar-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(data = BOD, aes(x = Time, y = demand)) + geom_line() + coord_polar() + blank_theme``````{r ggplotPolar-Polar, include=FALSE, echo=FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(data = BOD, aes(x = Time, y = demand)) + geom_line() + coord_polar()```<details><summary>Show attributes</summary>| Parameter | `geom_segment` ||--------------------------------------------------------------------------------------------------------------|----------------|| `theta` - map angle to either 'x' or 'y' | 'x' || `start` - offset (applied clockwise by default) from 12 o'clock in radians | 0 || `direction` - which direction ('clockwise': 1 or 'anticlockwise': -1) | 1 || `clip` - whether or not to clip plotting to within the plotting margins ("on") or to extend into the margins | "on" |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>coord</th><th align = 'left'>Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>Polar coordinates</td><td align = 'left'>`_polar`</td><td align = 'left'><br><br></td><td align = 'left' rowspan = 2>```{r ggplotCoord-Polar1, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data = BOD, aes(x = Time, y = demand)) + geom_line() + coord_polar()```</td></tr><tr><td align = 'left' font-size=2em colspan=3>```{r ggplotCoord-Polar1, eval=FALSE}```</td></tr></tbody></table>### Flip<br>coordinates ![](05_grammar_of_graphics_files/figure-html/ggplotFlip-Flip-s-1.png){class="thumb-s" #Gflip-coordinates}```{r ggplotFlip-Flip-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(data = BOD, aes(x = Time, y = demand)) + geom_line() + coord_flip() + blank_theme``````{r ggplotFlip-Flip, include=FALSE, echo=FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(data = BOD, aes(x = Time, y = demand)) + geom_line() + coord_flip()```<details><summary>Show attributes</summary>| Parameter | `geom_segment` ||--------------------------------------------------------------------------------------------------------------|----------------|| `xlim` - limits for the x axis | NULL || `ylim` - limits for the y axis | NULL || `expand` - whether to add a small expansion to the axes to ensure geoms and axes do not overlapp | TRUE || `expand` - whether or not to provide a warning when the default coordinate system is being replaced | FALSE || `clip` - whether or not to clip plotting to within the plotting margins ("on") or to extend into the margins | "on" |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>coord</th><th align = 'left'>Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>Flip coordinates</td><td align = 'left'>`_flip`</td><td align = 'left'><br><br></td><td align = 'left' rowspan = 2>```{r ggplotCoord-Flip1, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data = BOD, aes(x = Time, y = demand)) + geom_line() + coord_flip()```</td></tr><tr><td align = 'left' font-size=2em colspan=3>```{r ggplotCoord-Flip1, eval=FALSE}```</td></tr></tbody></table>### Fixed<br>coordinates ![](05_grammar_of_graphics_files/figure-html/ggplotCoord-Fixed-s-1.png){class="thumb-s" #Gfixed-coordinates}```{r ggplotCoord-Fixed-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(data = BOD, aes(x = Time, y = demand)) + geom_line() + coord_fixed() + blank_theme``````{r ggplotCoord-Fixed, include=FALSE, echo=FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(data = BOD, aes(x = Time, y = demand)) + geom_line() + coord_fixed()```<details><summary>Show attributes</summary>| Parameter | `geom_segment` ||--------------------------------------------------------------------------------------------------------------|----------------|| `ratio` - aspect ratio (y/x) | 1 || `xlim` - limits for the x axis | NULL || `ylim` - limits for the y axis | NULL || `expand` - whether to add a small expansion to the axes to ensure geoms and axes do not overlapp | TRUE || `expand` - whether or not to provide a warning when the default coordinate system is being replaced | FALSE || `clip` - whether or not to clip plotting to within the plotting margins ("on") or to extend into the margins | "on" |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>coord</th><th align = 'left'>Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>Fixed coordinates</td><td align = 'left'>`_fixed`</td><td align = 'left'><br><br></td><td align = 'left' rowspan = 2>```{r ggplotCoord-Fixed1, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data = BOD, aes(x = Time, y = demand)) + geom_line() + coord_fixed()```</td></tr><tr><td align = 'left' font-size=2em colspan=3>```{r ggplotCoord-Fixed1, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>Fixed ratio of coordinates</td><td align = 'left'>`_fixed`</td><td align = 'left'><br><br></td><td align = 'left' rowspan = 2>```{r ggplotCoord-Fixed2, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data = BOD, aes(x = Time, y = demand)) + geom_line() + coord_fixed(ratio = 0.5)```</td></tr><tr><td align = 'left' font-size=2em colspan=3>```{r ggplotCoord-Fixed2, eval=FALSE}```</td></tr></tbody></table>### Transformed<br>coordinates ![](05_grammar_of_graphics_files/figure-html/ggplotCoord-Trans-s-1.png){class="thumb-s" #Gtransformed-coordinates}```{r ggplotCoord-Trans-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(data = BOD, aes(x = Time, y = demand)) + geom_line() + coord_trans(x = "log10") + blank_theme``````{r ggplotCoord-Trans, include=FALSE, echo=FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(data = BOD, aes(x = Time, y = demand)) + geom_line() + coord_trans(x = "log10")```<details><summary>Show attributes</summary>| Parameter | `geom_segment` ||--------------------------------------------------------------------------------------------------------------|----------------|| `x` - the transformation to apply to the x-axis | "identity" || `y` - the transformation to apply to the y-axis | "identity" || `xlim` - limits for the x axis | NULL || `ylim` - limits for the y axis | NULL || `expand` - whether or not to provide a warning when the default coordinate system is being replaced | FALSE || `clip` - whether or not to clip plotting to within the plotting margins ("on") or to extend into the margins | "on" |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>coord</th><th align = 'left'>Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>Transformed coordinates</td><td align = 'left'>`_trans`</td><td align = 'left'><br><br></td><td align = 'left' rowspan = 2>```{r ggplotCoord-Trans1, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data = BOD, aes(x = Time, y = demand)) + geom_line() + coord_trans(x = "log10")```</td></tr><tr><td align = 'left' font-size=2em colspan=3>```{r ggplotCoord-Trans1, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>Trans ratio of coordinates</td><td align = 'left'>`_trans`</td><td align = 'left'><br><br></td><td align = 'left' rowspan = 2>```{r ggplotCoord-Trans2, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data = BOD, aes(x = Time, y = demand)) + geom_line() + coord_trans(x = scales::exp_trans(2))```</td></tr><tr><td align = 'left' font-size=2em colspan=3>```{r ggplotCoord-Trans2, eval=FALSE}```</td></tr></tbody></table>:::## Altering axes scales via the coordinate system ### ZoomingModifying scales with _coords\__ affects the zoom on the graph. That is,it defines the extent and nature of the axes coordinates. By contrast,altering limits via _scale\__ routines will alter the scope of dataincluded in a manner analogous to operating on a subset of the data.To illustrate this, lets produce a linear smoother for the _BOD_ data.To help us appreciate the differences between `coords_` and `scale_`when altering one axis (_x_ in this case), we will ensure that alleach plot has the same range of the other axis (_y_) and that theaspect ratio for the axes are all the same.```{r ggplotCoord-CoordAxes, echo=TRUE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}g <- ggplot(data = BOD, aes(x = Time, y = demand)) + geom_smooth(method = "lm") + geom_point()```<details><summary>Show default plot</summary>```{r ggplotCoord-CoordAxes1, eval=FALSE,echo=TRUE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}g + coord_fixed(ratio = 0.1, ylim = c(-5, 35))``````{r ggplotCoord-CoordAxes1, eval = TRUE, echo=FALSE, fig.width=2.5, fig.height=2.5}```</details>:::: {style='display:flex; place-content: space-between; flex-wrap: wrap;'}::: {style='width:49%;'}**Scale via _coords_ scale**```{r ggplotCoord-CoordAxes2, eval = FALSE, tidy = FALSE, echo=TRUE, fig.width=2.5, fig.height=2.5}g + coord_fixed(ratio = 0.1, ylim = c(-5, 35), xlim = c(2, 6))``````{r ggplotCoord-CoordAxes2, eval = TRUE, echo=FALSE, fig.width=2.5, fig.height=2.5}```:::::: {style='width:49%;'}**Scale via _scale_ scale**```{r ggplotCoord-CoordAxes3, eval = FALSE, echo=TRUE, fig.width=2.5, fig.height=2.5}g + coord_fixed(ratio = 0.1, ylim = c(-5, 35)) + scale_x_continuous(limits = c(2, 6))``````{r ggplotCoord-CoordAxes3, eval = TRUE, echo=FALSE, fig.width=2.5, fig.height=2.5}```:::::::Notice that the left hand figure (that restricts the x-axis scale)simply zooms in on the plot thereby cutting some of the data off. Bycontrast, `scale_` (right hand figure) removes data that are outsidethe range and thus also alters the output of the smoother (linearmodel).### Geom re-scalingIn addition to altering the zoom of the axes, axes (coordinate system)scales can be transformed to other scales via the `coord_trans`_function_. Such transformations of the coordinate system take place**after** statistics have been calculated and _geoms_derived. Therefore the shape of geoms are altered.To illustrate the difference between `coord_trans` and `scale_`, letscreate a fabricated data set of 50 points in which _y_ is anexponential function of _x_ (with noise).```{r ggplotCoord-CoordTrans, eval = TRUE, echo=TRUE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}set.seed(1)n<-50dat <- data.frame(x = exp((1:n+rnorm(n,sd=2))/10), y = 1:n+rnorm(n,sd=2))g <- ggplot(data = dat, aes(x = x, y = y))```<table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Linear scales</th><th align = 'left'>`coord_trans`</th><th align = 'left'>`scales_`</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>```{r ggplotCoord-Trans1a, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}g + geom_point()``````{r ggplotCoord-Trans1a, eval=FALSE, class="plain"}```</td><td align = 'left'>```{r ggplotCoord-Trans1b, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}g + geom_point() + coord_trans(x="log10")``````{r ggplotCoord-Trans1b, eval=FALSE, class="plain"}```</td><td align = 'left'>```{r ggplotCoord-Trans1c, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}g + geom_point() + scale_x_continuous(trans="log10")``````{r ggplotCoord-Trans1c, eval=FALSE, class="plain"}```</td></tr><tr class = 'odd'><td align = 'left'>```{r ggplotCoord-Trans2a, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}g + geom_point() + geom_smooth(method="lm")``````{r ggplotCoord-Trans2a, eval=FALSE, class="plain"}```</td><td align = 'left'>```{r ggplotCoord-Trans2b, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}g + geom_point() + geom_smooth(method="lm") + coord_trans(x="log10") ``````{r ggplotCoord-Trans2b, eval=FALSE, class="plain"}```</td><td align = 'left'>```{r ggplotCoord-Trans2c, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}g + geom_point() + geom_smooth(method="lm") + scale_x_continuous(trans="log10") ``````{r ggplotCoord-Trans2c, eval=FALSE, class="plain"}```</td></tr></tbody></table>In the above, the `log10` _transformer_ function was applied to eitherthe coordinates or the axes scales. More information about this andother transformer functions is provided in the [scalessection](#scales).# Scales The idea of scales is that you present the plotting engine with dataor characteristics in one scale and use the various `scale_`_functions_ to convert those data into another scale. In the grammarof graphics, scales are synonymous for units of data, colors, shapes,sizes etc of plotting features and the axes and guides (legends)provide a visual cue for what the scales are. For example:- you might include data that ranges from 10 to 20 units, yet you wish to produce a plot that uses a restricted range of 12-16.- you have presented grouped data (data with multiple trends) and instructed the graphing engine to assign different colour codes to each trend. You can then define a colour scale to adjust the exact colours rendered.- similarly, you might have indicated how plotting symbol shape and size are to be distinguished in your data set. You can then assign scales that define the exact shapes and symbol sizes rendered.Technically, scales determine how attributes of the data are mappedinto aesthetic geom properties. The majority of geom's (geometricobjects) have the following aesthetic properties:| Parameter ||----------------------------------------------------------------------------------|| `x` - the x position (coordinates) of the geom || `y` - the y position (coordinates) of the geom || `size` - size of the geom (e.g. the size of points, thickness of lines) || `shape` - the shape of the geom || `linetype` - the type of line to use for the geom's outline (solid, dashed, etc) || `colour` - the colour of the geom's outline || `fill` - the colour of the geom's fill (inside colour) || `alpha` - the transparency of the geom (0 = transparent, through to 1 = opaque) |: {.primary .bordered .sm .paramsTable}In turn, each of these properties are mapped to a scale - the defaultsof which are automatically selected according to what is appropriatefor the sort of data. For example, if the variable mapped to an axisis continuous, the a continuous scale is the default. If the variablemapped to an axis is categorical, then a discrete scale is thedefault. And so on... The following table describes the typical scale associated with eachof the major variable types:| Variable type | Scale ||-----------------------------+--------------------------------------------------------------|| `numeric` - continuous data | `_continuous` - data mapped onto a continuous scale || `numeric` - continuous data | `_log10` - data mapped onto a log (base 10) continuous scale || `numeric` - continuous data | `_sqrt` - data mapped onto a sqrt continuous scale || `numeric` - continuous data | `_reverse` - data mapped onto a reverse continuous scale || `numeric` - continuous data | `_binned` - data mapped onto a binned (discretized) scale || | || `factor` - categorical data | `_discrete` - data mapped onto a categorical scale || | || `date` - date data | `_date` - data mapped according to dates || `POSIXct` - date/time data | `_datetime` - data mapped according to date/time || `POSIXct` - date/time data | `_time` - data mapped according to date/time || | |: {.primary .bordered .sm .paramsTable}Some properties, such as colour also have additional scales that arespecific to the characteristic. The scales effect not only thecharacteristics of the geoms, they also effect the guides (legends)that accompany the geoms.Scaling functions comprise the prefix `scale_`, followed by the nameof an aesthetic property and suffixed by the type of scale. Hence afunction to manually define a colour scale would be`scale_colour_manual`.- `name` - a title applied to the scale. In the case of scales for x and y (the x,y coordinate of _geoms_), the name is the axis title. For all other scales, the name is the title of the guide (legend). <details><summary>Explore</summary> <table class='table table-primary table-bordered table-sm paramsTable'> <thead> <tr class = 'header'> <th align = 'left'>Feature</th> <th align = 'left'>Notes</th> <th align = 'left'>Example plot</th> </tr> </thead> <tbody> <tr class = 'odd'> <td align = 'left'>Simple axis title</td> <td align = 'left'>Provide a more meaningful axis title for the x-axis</td> <td align = 'left' rowspan = 2>```{r ggplotExampleScaleName1, echo=FALSE, fig.width=3, fig.height=3, out.extra='class="thumb-L"'} ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point() + scale_x_continuous(name = "CO₂ concentration") ``` </td> </tr> <tr> <td align = 'left' font-size=2em colspan=2>```{r ggplotExampleScaleName1, eval=FALSE,echo=TRUE, fig.width=3, fig.height=3} ``` </td> </tr> <tr class = 'even'> <td align = 'left'>Math in axis title</td> <td align = 'left'> More elaborate titles that include special characters and mathematical notations are supported via `expression()`'s. For more info on plotting mathematical expressions, run `demo(plotmath)`. Note the use of the `~` character everywhere that a space would appear. That is because an _expression_ cannot have spaces. </td> <td align = 'left' rowspan = 2>```{r ggplotExampleScaleName2, echo=FALSE, fig.width=3, fig.height=3, out.extra='class="thumb-L"'} ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point() + scale_x_continuous( name = expression(Ambient~CO[2]~concentration~(mg/l)) ) ``` </td> </tr> <tr> <td align = 'left' font-size=2em colspan=2>```{r ggplotExampleScaleName2, eval=FALSE, echo=TRUE, fig.width=3, fig.height=3, out.extra='class="thumb-L"'} ``` </td> </tr> <tr class = 'odd'> <td align = 'left'>Math in axis title</td> <td align = 'left'> Alternatively, _expressions_ can be built up by _pasting_ objects together. </td> <td align = 'left' rowspan = 2>```{r ggplotExampleScaleName3, echo=FALSE, fig.width=3, fig.height=3, out.extra='class="thumb-L"'} ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point() + scale_x_continuous( name = expression(paste("Ambient ", CO[2], " concentration (mg/l)"))) ``` </td> </tr> <tr> <td align = 'left' font-size=2em colspan=2>```{r ggplotExampleScaleName3, eval=FALSE, echo=TRUE, fig.width=3, fig.height=3, out.extra='class="thumb-L"'} ``` </td> </tr> </tbody> </table> </details>- `breaks` - the increments on the guide. For `scale_x_` and`scale_y_`, breaks are the axis tick locations. For all other scales, the breaks indicate the increments of the characteristic in the legend (e.g. how many point shapes are featured in the legend). <details><summary>Explore</summary> By default, _pretty_ breaks will be used for continuous (including dates) data. It is possible to define your own breaks. <table class='table table-primary table-bordered table-sm paramsTable'> <thead> <tr class = 'header'> <th align = 'left'>Feature</th> <th align = 'left'>Notes</th> <th align = 'left'>Example plot</th> </tr> </thead> <tbody> <tr class = 'odd'> <td align = 'left'>User defined breaks</td> <td align = 'left'>Provide a vector of breaks (tick marks)</td> <td align = 'left' rowspan = 2>```{r ggplotExampleScaleBreaks1, echo=FALSE, fig.width=3, fig.height=3, out.extra='class="thumb-L"'} ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point() + scale_x_continuous(breaks = c(200, 500, 800)) ``` </td> </tr> <tr> <td align = 'left' font-size=2em colspan=2>```{r ggplotExampleScaleBreaks1, eval=FALSE,echo=TRUE, fig.width=3, fig.height=3} ``` </td> </tr> <tr class = 'even'> <td align = 'left'>User defined breaks</td> <td align = 'left'>Provide a more useful breaks for log scale axes</td> <td align = 'left' rowspan = 2>```{r ggplotExampleScaleBreaks2, echo=FALSE, fig.width=3, fig.height=3, out.extra='class="thumb-L"'} ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point() + scale_x_log10(breaks = as.vector(c(1,2,5,10) %o% 10^(-1:2))) ``` </td> </tr> <tr> <td align = 'left' font-size=2em colspan=2>```{r ggplotExampleScaleBreaks2, eval=FALSE,echo=TRUE, fig.width=3, fig.height=3} ``` </td> </tr> <tr class = 'odd'> <td align = 'left'>User defined breaks</td> <td align = 'left'> Alternatively, you can apply other functions that define breaks. There are some useful break functions in the `scales` _package_ </td> <td align = 'left' rowspan = 2>```{r ggplotExampleScaleBreaks3, echo=FALSE, fig.width=3, fig.height=3, out.extra='class="thumb-L"'} ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point() + scale_x_continuous(breaks = scales::breaks_log(n=7)) ``` </td> </tr> <tr> <td align = 'left' font-size=2em colspan=2>```{r ggplotExampleScaleBreaks3, eval=FALSE,echo=TRUE, fig.width=3, fig.height=3} ``` </td> </tr> <tr class = 'even'> <td align = 'left'>User defined breaks</td> <td align = 'left'> For discrete data, breaks should return a vector of values that correspond to categorical levels (in the order of the levels). In the following example, only one of the Treatment groups has a tick mark. </td> <td align = 'left' rowspan = 2>```{r ggplotExampleScaleBreaks4, echo=FALSE, fig.width=3, fig.height=3, out.extra='class="thumb-L"'} ggplot(data = CO2, aes(x = Treatment, y = uptake)) + geom_point() + scale_x_discrete(breaks = c("chilled")) ``` </td> </tr> <tr> <td align = 'left' font-size=2em colspan=2>```{r ggplotExampleScaleBreaks4, eval=FALSE,echo=TRUE, fig.width=3, fig.height=3} ``` </td> </tr> </tbody> </table> </details>- `labels` - the labels given to the increments on the guide. For`scale_x_` and `scale_y_`, labels are the axis tick labels. For all other scales, the labels are the labels given to items in the legend. <details><summary>Explore</summary> The `scales` _package_ has a number of label formatting _functions_. These all start with `label_` <table class='table table-primary table-bordered table-sm paramsTable'> <thead> <tr class = 'header'> <th align = 'left'>Feature</th> <th align = 'left'>Notes</th> <th align = 'left'>Example plot</th> </tr> </thead> <tbody> <tr class = 'odd'> <td align = 'left'>User defined tick labels</td> <td align = 'left'>Format x-axis tick labels to include comma's every multiple of 1000 </td> <td align = 'left' rowspan = 2>```{r ggplotExampleScaleLabels1, echo=FALSE, fig.width=3, fig.height=3, out.extra='class="thumb-L"'} ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point() + scale_x_continuous(labels = scales::label_comma()) ``` </td> </tr> <tr> <td align = 'left' font-size=2em colspan=2>```{r ggplotExampleScaleLabels1, eval=FALSE,echo=TRUE, fig.width=3, fig.height=3} ``` </td> </tr> <tr class = 'odd'> <td align = 'left'>User defined tick labels</td> <td align = 'left'> Format y-axis labels to include percentage sign. By default, the `label_percent` _function_ also assumes that the data mapped to the scale are proportions (that is the values are between 0 and 1). So by default, the function will multiple (`scale`) the values by 100. In this case, since the y-axis values are all between 0 and 100, we indicate that they should be multiplied by 1 (e.g. no change). </td> <td align = 'left' rowspan = 2>```{r ggplotExampleScaleLabels2, echo=FALSE, fig.width=3, fig.height=3, out.extra='class="thumb-L"'} ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point() + scale_y_continuous(labels = scales::label_percent(scale=1)) ``` </td> </tr> <tr> <td align = 'left' font-size=2em colspan=2>```{r ggplotExampleScaleLabels2, eval=FALSE,echo=TRUE, fig.width=3, fig.height=3} ``` </td> </tr> <tr class = 'even'> <td align = 'left'>User defined tick labels</td> <td align = 'left'> Format y-axis labels to include percentage sign. On this occasion we will derive a new variable to map to the y-axis. These values are in the range from 0 to 1, so we do want to `scale` (multiply) by 100. We will also adjust the `accuracy` (number to round to). In this case, we will indicate that it should round to the nearest 1 (whole number). </td> <td align = 'left' rowspan = 2>```{r ggplotExampleScaleLabels3, echo=FALSE, fig.width=3, fig.height=3, out.extra='class="thumb-L"'} CO2 <- CO2 %>% mutate(percent = uptake/100) ggplot(data = CO2, aes(x = conc, y = percent)) + geom_point() + scale_y_continuous(labels = scales::label_percent(accuracy = 1)) ``` </td> </tr> <tr> <td align = 'left' font-size=2em colspan=2>```{r ggplotExampleScaleLabels3, eval=FALSE,echo=TRUE, fig.width=3, fig.height=3} ``` </td> </tr> <tr class = 'odd'> <td align = 'left'>User defined tick labels</td> <td align = 'left'> Format the x-axis ticks to append '.spp' after each `Type` and make each tick mark italic. To apply any mathematical notation (including italic formatting) we must use a label formatter that parses expressions. We will define the format of the labels first in the dataset. </td> <td align = 'left' rowspan = 2>```{r ggplotExampleScaleLabels4, echo=FALSE, fig.width=3, fig.height=3, out.extra='class="thumb-L"'} CO2 <- CO2 %>% mutate(species = paste0('italic(', Type, '.spp)')) ggplot(data = CO2, aes(x = species, y = uptake)) + geom_point() + scale_x_discrete(labels = scales::label_parse()) ``` </td> </tr> <tr> <td align = 'left' font-size=2em colspan=2>```{r ggplotExampleScaleLabels4, eval=FALSE,echo=TRUE, fig.width=3, fig.height=3} ``` </td> </tr> <tr class = 'even'> <td align = 'left'>User defined tick labels</td> <td align = 'left'> Or, we can define the formatting on the fly, </td> <td align = 'left' rowspan = 2>```{r ggplotExampleScaleLabels5, echo=FALSE, fig.width=3, fig.height=3, out.extra='class="thumb-L"'} ggplot(data = CO2, aes(x = Type, y = uptake)) + geom_point() + scale_x_discrete( labels = scales::label_math(italic(paste(.x, '.spp', sep=''))) ) ``` </td> </tr> <tr> <td align = 'left' font-size=2em colspan=2>```{r ggplotExampleScaleLabels5, eval=FALSE,echo=TRUE, fig.width=3, fig.height=3} ``` </td> </tr> </tbody> </table> </details>- `limits` - the span/range of data represented in the scale. Note, if the range is inside the range of the data, the data are sub-setted. <details><summary>Explore</summary> <table class='table table-primary table-bordered table-sm paramsTable'> <thead> <tr class = 'header'> <th align = 'left'>Feature</th> <th align = 'left'>Notes</th> <th align = 'left'>Example plot</th> </tr> </thead> <tbody> <tr class = 'odd'> <td align = 'left'>Range of the scale</td> <td align = 'left'>Extend the range of the x-axis </td> <td align = 'left' rowspan = 2>```{r ggplotExampleScaleLimits1, echo=FALSE, fig.width=3, fig.height=3, out.extra='class="thumb-L"'} ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point() + scale_x_continuous(limits = c(0, 2000)) ``` </td> </tr> <tr> <td align = 'left' font-size=2em colspan=2>```{r ggplotExampleScaleLimits1, eval=FALSE,echo=TRUE, fig.width=3, fig.height=3} ``` </td> </tr> <tr class = 'even'> <td align = 'left'>Range of the scale</td> <td align = 'left'>Extend the range represented by a colour bar </td> <td align = 'left' rowspan = 2>```{r ggplotExampleScaleLimits2, echo=FALSE, fig.width=3, fig.height=3, out.extra='class="thumb-L"'} ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point(aes(colour=uptake)) + scale_colour_continuous(limits = c(0,60)) ``` </td> </tr> <tr> <td align = 'left' font-size=2em colspan=2>```{r ggplotExampleScaleLimits2, eval=FALSE,echo=TRUE, fig.width=3, fig.height=3} ``` </td> </tr> <tr class = 'odd'> <td align = 'left'>Range of the scale</td> <td align = 'left'> This can also include categorical data - in this case, we are adding an additional category. Although this category does not appear in the data, it may appear in other data for which you are producing similar plots and you wish to maintain the same legends and colour palettes. </td> <td align = 'left' rowspan = 2>```{r ggplotExampleScaleLimits3, echo=FALSE, fig.width=3, fig.height=3, out.extra='class="thumb-L"'} ggplot(data = CO2, aes(x = Type, y = uptake)) + geom_violin(aes(fill=Treatment)) + scale_fill_discrete(limits = c('nonchilled','chilled','heated')) ``` </td> </tr> <tr> <td align = 'left' font-size=2em colspan=2>```{r ggplotExampleScaleLimits3, eval=FALSE,echo=TRUE, fig.width=3, fig.height=3} ``` </td> </tr> </tbody> </table> </details>- `trans` - scale transformations applied - obviously this is only relevant to scales that are associated with continuous data. <details><summary>Explore</summary> <table class='table table-primary table-bordered table-sm paramsTable'> <thead> <tr class = 'header'> <th align = 'left'>Feature</th> <th align = 'left'>Notes</th> <th align = 'left'>Example plot</th> </tr> </thead> <tbody> <tr class = 'odd'> <td align = 'left'>Log (base 10) scale</td> <td align = 'left'>Apply a log (base 10) transformation of the x-axis </td> <td align = 'left' rowspan = 2>```{r ggplotExampleScaleTrans1, echo=FALSE, fig.width=3, fig.height=3, out.extra='class="thumb-L"'} ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point() + scale_x_continuous(trans = "log10") ``` </td> </tr> <tr> <td align = 'left' font-size=2em colspan=2>```{r ggplotExampleScaleTrans1, eval=FALSE,echo=TRUE, fig.width=3, fig.height=3} ``` </td> </tr> <tr class = 'even'> <td align = 'left'>Square-root scale</td> <td align = 'left'>Apply a square-root transformation of the x-axis </td> <td align = 'left' rowspan = 2>```{r ggplotExampleScaleTrans2, echo=FALSE, fig.width=3, fig.height=3, out.extra='class="thumb-L"'} ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point() + scale_x_continuous(trans = "sqrt") ``` </td> </tr> <tr> <td align = 'left' font-size=2em colspan=2>```{r ggplotExampleScaleTrans2, eval=FALSE,echo=TRUE, fig.width=3, fig.height=3} ``` </td> </tr> <tr class = 'odd'> <td align = 'left'>Log (base 2) scale</td> <td align = 'left'> Apply a log (base 2) transformation of the x-axis The `scales` _package_ has a set of functions to achieve common transformation. </td> <td align = 'left' rowspan = 2>```{r ggplotExampleScaleTrans3, echo=FALSE, fig.width=3, fig.height=3, out.extra='class="thumb-L"'} ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point() + scale_x_continuous(trans = scales::log10_trans()) ``` </td> </tr> <tr> <td align = 'left' font-size=2em colspan=2>```{r ggplotExampleScaleTrans3, eval=FALSE,echo=TRUE, fig.width=3, fig.height=3} ``` </td> </tr> <tr class = 'even'> <td align = 'left'>Log (base 2) scale</td> <td align = 'left'> There is also a general _log_ transform that allows you to indicate the logarithmic base. When using the `log_trans` _function_, it is a good idea to also use a related function to control the position of tick mark breaks. </td> <td align = 'left' rowspan = 2>```{r ggplotExampleScaleTrans4, echo=FALSE, fig.width=3, fig.height=3, out.extra='class="thumb-L"'} ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point() + scale_x_continuous(trans = scales::log_trans(base = 2), breaks = scales::log_breaks(base = 2)) ``` </td> </tr> <tr> <td align = 'left' font-size=2em colspan=2>```{r ggplotExampleScaleTrans4, eval=FALSE,echo=TRUE, fig.width=3, fig.height=3} ``` </td> </tr> <tr class = 'odd'> <td align = 'left'>Log (base 2) scale</td> <td align = 'left'> If you wish to apply a logarithmic scale transform, yet also include a tickmark at 0, then the `psuedo_log_trans` is a very useful function. </td> <td align = 'left' rowspan = 2>```{r ggplotExampleScaleTrans5, echo=FALSE, fig.width=3, fig.height=3, out.extra='class="thumb-L"'} ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point() + scale_x_continuous(trans = scales::pseudo_log_trans(), limits = c(0, 1500)) ``` </td> </tr> <tr> <td align = 'left' font-size=2em colspan=2>```{r ggplotExampleScaleTrans5, eval=FALSE,echo=TRUE, fig.width=3, fig.height=3} ``` </td> </tr> </tbody> </table> </details>- `guide` - a function used to create/modify the associated guide (axes or legend). <details><summary>Explore</summary> <table class='table table-primary table-bordered table-sm paramsTable'> <thead> <tr class = 'header'> <th align = 'left'>Feature</th> <th align = 'left'>Notes</th> <th align = 'left'>Example plot</th> </tr> </thead> <tbody> <tr class = 'odd'> <td align = 'left'>Remove guide</td> <td align = 'left'> Scale the size of the points according to a continuous variable, yet remove the associated legend. </td> <td align = 'left' rowspan = 2>```{r ggplotExampleScaleGuide1, echo=FALSE, fig.width=3, fig.height=3, out.extra='class="thumb-L"'} ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point(aes(size = uptake)) + scale_size(guide = NULL) ``` </td> </tr> <tr> <td align = 'left' font-size=2em colspan=2>```{r ggplotExampleScaleGuide1, eval=FALSE,echo=TRUE, fig.width=3, fig.height=3} ``` </td> </tr> <tr class = 'even'> <td align = 'left'>Override aethetics </td> <td align = 'left'> Colour the points according to a categorical variable (`Type`), but increase the size of the points in the legend to make them more obvious. </td> <td align = 'left' rowspan = 2>```{r ggplotExampleScaleGuide3, echo=FALSE, fig.width=3, fig.height=3, out.extra='class="thumb-L"'} ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point(aes(colour = Type)) + scale_colour_discrete( guide = guide_legend(override.aes = list(size = 5)) ) ``` </td> </tr> <tr> <td align = 'left' font-size=2em colspan=2>```{r ggplotExampleScaleGuide3, eval=FALSE,echo=TRUE, fig.width=3, fig.height=3} ``` </td> </tr> </tbody> </table> </details>::: {.panel-tabset}## Axes<br><br> ![](05_grammar_of_graphics_files/figure-html/ggplotScale-Axes-s-1.png){class="thumb-s" #Gaxes}```{r ggplotScale-Axes-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point(colour = "grey") + scale_x_continuous() + theme_classic()``````{r ggplotScale-Axes, include=FALSE, echo=FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point(colour = "grey") + scale_x_continuous() + theme_classic()```<details><summary>Show attributes</summary>| Parameter ||---------------------------------------------------------------------|| _All_ || `position` - axis position ('left', 'right, 'top', 'bottom') || `sec.axis` - secondary axis || || _\_date_, _\_datetime_ || `date_breaks` - distance between breaks (in date units) || `date_labels` - formatting for labels || `date_minor_breaks` - distance between minor breaks (in date units) || `timezone` - timezeone to use when displaying date/datetime || |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>scale</th><th align = 'left'>Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>Linear scaling</td><td align = 'left'>`_continuous`</td><td align = 'left'><br><br></td><td align = 'left' rowspan = 2>```{r ggplotScale_Axes1, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point() + scale_x_continuous()```</td></tr><tr><td align = 'left' font-size=2em colspan=3>```{r ggplotScale_Axes1, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>Add a secondary y-axis</td><td align = 'left'>`_continuous`</td><td align = 'left'>Add a secondary y-axis and alter its scale. Note, by design, `ggplot`only permits secondary axes that are derived from the primary axis(are pure re-scale) - it is not possible (without unsupported effort)to represent an additional variable.</td><td align = 'left' rowspan = 2>```{r ggplotScale_Axes2, echo=FALSE, fig.width=3, fig.height=3, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point() + scale_y_continuous( name = expression(Uptake~(mu*mol/m^3)), sec.axis = sec_axis( name = expression(Uptake~(mg/m^3)), trans = ~ .*44/1000) )```</td></tr><tr><td align = 'left' font-size=2em colspan=3>```{r ggplotScale_Axes2, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>Add a secondary y-axis</td><td align = 'left'>`_continuous`</td><td align = 'left'>Add a secondary y-axis, except put the derived axis on the left side and the standard axis on the right</td><td align = 'left' rowspan = 2>```{r ggplotScale_Axes3, echo=FALSE, fig.width=3, fig.height=3, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point() + scale_y_continuous( name = expression(Uptake~(mu*mol/m^3)), position = "right", sec.axis = sec_axis( name = expression(Uptake~(mg/m^3)), trans = ~ .*44/1000) )```</td></tr><tr><td align = 'left' font-size=2em colspan=3>```{r ggplotScale_Axes3, eval=FALSE}```</td></tr></tbody></table>## Size<br><br> ![](05_grammar_of_graphics_files/figure-html/ggplotScale-Size-s-1.png){class="thumb-s" #Gsize}The `scale_size_` scales control the size of geoms (such as the size of points).```{r ggplotScale-Size-s, echo=FALSE, include = FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(data = BOD, aes(x = Time, y = demand)) + geom_point(aes(size = demand)) + theme_classic()``````{r ggplotScale-Size, include=FALSE, echo=FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(data = BOD, aes(x = Time, y = demand)) + geom_point(aes(size = demand)) + theme_classic()```<details><summary>Show attributes</summary>| Parameter | `_size` | `_size_area` | `_size_binned` ||-------------------------------------------------|---------|--------------|----------------|| `range` - range (min, max) of symbol sizes | c(1, 6) | | c(1,6) || `max_size` - maximum size of symbols | | 6 | || `n.breaks` - approximate number of major breaks | | | NULL || `nice.breaks` - whether to use nice breaks | | | TRUE |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>scale</th><th align = 'left'>Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>Scale size</td><td align = 'left'>`_size`</td><td align = 'left'>Scale the size according to a continuous variable. The size of the symbols will be proportional to the value in the variable and the guide (legend) will provide an indication of the symbol sizes correspondingto a set of values. </td><td align = 'left' rowspan = 2>```{r ggplotScale_Size1, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point(aes(size = uptake)) + scale_size()```</td></tr><tr><td align = 'left' font-size=2em colspan=3>```{r ggplotScale_Size1, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>Scale size</td><td align = 'left'>`_size`</td><td align = 'left'>Scale the size according to a continuous variable</td><td align = 'left' rowspan = 2>```{r ggplotScale_Size2, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point(aes(size = uptake)) + scale_size(range = c(0.5,3))```</td></tr><tr><td align = 'left' font-size=2em colspan=3>```{r ggplotScale_Size2, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>Scale size</td><td align = 'left'>`_size_binned`</td><td align = 'left'>Scale the size according to a continuous variable that has been binned. In this case, as there are five bins, the symbols will be of one of five sizes (rather than a continuum).</td><td align = 'left' rowspan = 2>```{r ggplotScale_Size3, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point(aes(size = uptake)) + scale_size_binned()```</td></tr><tr><td align = 'left' font-size=2em colspan=3>```{r ggplotScale_Size3, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>Scale size</td><td align = 'left'>`_size_discrete`</td><td align = 'left'>Scale the size according to the levels of a categorical variable </td><td align = 'left' rowspan = 2>```{r ggplotScale_Size4, echo=FALSE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point(aes(size = Type)) + scale_size_discrete()```</td></tr><tr><td align = 'left' font-size=2em colspan=3>```{r ggplotScale_Size4, eval=FALSE}```</td></tr></tbody></table>## Shape<br><br> ![](05_grammar_of_graphics_files/figure-html/ggplotScale-Shape-s-1.png){class="thumb-s" #Gshape}The `scale_shape_` scales control the shape of geoms (such as theshape of the plotting point)```{r ggplotScale-Shape-s, echo=FALSE, include = FALSE, fig.height=5, fig.width=5, out.width='300px', out.height='300px', message=FALSE}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point(aes(shape = Type)) + scale_shape(solid = TRUE) + theme_classic()``````{r ggplotScale-Shape, include=FALSE, echo=FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point(aes(shape = Type)) + scale_shape(solid = FALSE)```<details><summary>Show attributes</summary>| Parameter | `_shape` | `_shape_binned` ||-------------------------------------------------------------------------|----------|-----------------|| `solid` - whether the plotting symbols should be solid (TRUE) or hollow | TRUE | TRUE |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>scale</th><th align = 'left'>Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>Scale shape</td><td align = 'left'>`_shape`</td><td align = 'left'>Use hollow shapes.</td><td align = 'left' rowspan = 2>```{r ggplotScale_Shape1, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point(aes(shape = Type)) + scale_shape(solid = FALSE)```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotScale_Shape1, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>Scale shape</td><td align = 'left'>`_shape_manual`</td><td align = 'left'>Manually defined the symbol shapes.</td><td align = 'left' rowspan = 2>```{r ggplotScale_Shape2, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point(aes(shape = Type)) + scale_shape_manual(values = c(16, 21))```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotScale_Shape2, eval=FALSE}```</td></tr></tbody></table>## Linetype<br><br> ![](05_grammar_of_graphics_files/figure-html/ggplotScale-Linetype-s-1.png){class="thumb-s" #Glinetype}The `scale_linetype_` scales control the linetype of geoms. ```{r ggplotScale-Linetype-s, echo=FALSE, include = FALSE, fig.height=5, fig.width=5, out.width='300px', out.height='300px', message=FALSE}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_smooth(aes(linetype = Type), se=FALSE, show.legend = FALSE, color='black') + theme_classic()``````{r ggplotScale-Linetype, include=FALSE, echo=FALSE, fig.height=3, fig.width=3, out.width='300px', out.height='300px', message=FALSE}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_smooth(aes(linetype = Type)) ```<table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>scale</th><th align = 'left'>Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>Scale line type</td><td align = 'left'>`_linetype`</td><td align = 'left'>Manually define the line types and alter the associated legend labels.</td><td align = 'left' rowspan = 2>```{r ggplotScale_Linetype1, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_smooth(aes(linetype = Treatment)) + scale_linetype_manual(name = "Temperature", values = c('solid', 'dotted'), labels = c('Chilled', 'Non-chilled'))```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotScale_Linetype1, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>Scale line type</td><td align = 'left'>`_linetype`</td><td align = 'left'>Manually define the line types and alter the associated legend labelsand remove the ribbon from the legend.</td><td align = 'left' rowspan = 2>```{r ggplotScale_Linetype2, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_smooth(aes(linetype = Treatment)) + scale_linetype_manual(name = "Temperature", values = c('solid', 'dotted'), labels = c('Chilled', 'Non-chilled'), guide = guide_legend(override.aes = list(fill = NA)) )```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotScale_Linetype2, eval=FALSE}```</td></tr></tbody></table>## Colour<br>fill ![](05_grammar_of_graphics_files/figure-html/ggplotScale-Color-s-1.png){class="thumb-s" #colour-fillA}```{r ggplotScale-Color-s, echo=FALSE, include = FALSE, fig.height=5, fig.width=5, out.width='300px', out.height='300px', message=FALSE}ggplot(data = BOD, aes(x = Time, y = demand)) + geom_point(aes(color = demand), size = 12) + theme_classic()```_Colour_ (or color) is used to represent the colour of lines and solidsymbols (e.g. the outer color of shapes). By contrast, _fill_ is usedto represent the filling (inner) colour of shapes.Colors can be specified by either:- a name of one of the 657 named colors <details><summary>Color names</summary> The names of all the available colors can be obtained via:```{r ggplotScale_ColorF, eval=FALSE} colors() ``` Below is a visual representation of those colours and their names (although you are going to need exceptional eye sight to read the names ;)).```{r ggplotScale_ColorG, echo=FALSE, fig.width=20, fig.height=20, out.height='650px', out.width='650px'} rgb2col = function(rgbmat){ # function to apply to each column of input rgbmat ProcessColumn = function(col){ rgb(rgbmat[1, col], rgbmat[2, col], rgbmat[3, col], maxColorValue = 255) } # Apply the function sapply(1:ncol(rgbmat), ProcessColumn) } is_Dark <- function(Colors) { sapply(Colors,function(x) sum(col2rgb(x)*c(299, 587,114))/1000 < 123) } d=data.frame(c=colors(), y=seq(0, length(colors())-1)%%66, x=seq(0, length(colors())-1)%/%66) d <- d %>% mutate(hex = rgb2col(col2rgb(colors())), dark = is_Dark(c)) ggplot() + scale_x_continuous(name="", breaks=NULL, expand=c(0, 0)) + scale_y_continuous(name="", breaks=NULL, expand=c(0, 0)) + scale_fill_identity() + geom_rect(data=d, mapping=aes(xmin=x, xmax=x+1, ymin=y, ymax=y+1), fill="white") + geom_rect(data=d, mapping=aes(xmin=x+0.05, xmax=x+0.95, ymin=y+0, ymax=y+1, fill=c)) + geom_text(data=d, mapping=aes(x=x+0.5, y=y+0.5, label=c, color=dark), hjust=0.5, vjust=0.5, size=3, show.legend = FALSE) + scale_color_manual(breaks = c(FALSE, TRUE), values = c('black', 'white')) ``` </details>- hexedecimal codes (e.g. `#FF0000` is 'red'). This can also be an eight digit code in which case the last two digits represent alpha (e.g. `#FF000050` is 50% transparent red).- RGB color codes- a colour palette <details><summary>Palettes</summary>```{r, eval=TRUE, echo=TRUE, fig.width=12, fig.height=8, out.width='750px'} colorspace::hcl_palettes(plot = TRUE, n = 5) RColorBrewer::display.brewer.all() ``` </details><details><summary>Show attributes</summary>| Parameter | default | default ||------------------------------------------------------------|---------------|--------------|| | `_continuous` | `_binned` || `type` - the type of colour scale | 'gradient' | || ('gradient', 'viridis') | | || | | || | `_gradient` | `gradient2` || `low` - colour of low end of gradient | '#132B43' | muted("red") || `high` - colour of high end of gradient | '#56B1F7' | muted("blue" || `space` - colour space | 'Lab' | 'Lab' || `mid` - colour of mid point in gradient | | 'white' || `midpoint` - data value associated with middle of gradient | | 0 || | | || | `_gradientn` | || `colours` - vector of colours | | || `values` - position associated with eaah colour | | || | | || | `_brewer` | `_distiller` || `type` - type of color scale ('div', 'seq' or 'qual') | 'seq' | 'seq' || `palette` - name or number of colorbrewer palette | 1 | 1 || `direction` - order of colours from the palette | 1 | -1 || | | |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>scale</th><th align = 'left'>Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>Scale colour type</td><td align = 'left'>`_colour_gradient`</td><td align = 'left'>Manually define a colour scale that gradually transitions from red to blue.</td><td align = 'left' rowspan = 2>```{r ggplotScale_Colour1, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point(aes(colour = uptake)) + scale_colour_gradient(low = 'red', high = 'blue')```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotScale_Colour1, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>Scale colour type</td><td align = 'left'>`_colour_gradient2`</td><td align = 'left'>Manually define a colour scale that gradually transitions from red to blue via a white middle.</td><td align = 'left' rowspan = 2>```{r ggplotScale_Colour2, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point(aes(colour = uptake)) + scale_colour_gradient2(low = 'red', mid = "white", high = 'blue', midpoint = 25)```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotScale_Colour2, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>Scale colour type</td><td align = 'left'>`_colour_gradientn`</td><td align = 'left'>Manually define a colour scale that gradually transitions along a pre-defined colour palette.</td><td align = 'left' rowspan = 2>```{r ggplotScale_Colour3, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point(aes(colour = uptake)) + scale_colour_gradientn(colours = terrain.colors(12))```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotScale_Colour3, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>Scale colour type</td><td align = 'left'>`_colour_distiller`</td><td align = 'left'>Manually define a colour scale that gradually transitions according to a colorbrewer palette on a **continuous** variable.</td><td align = 'left' rowspan = 2>```{r ggplotScale_Colour4, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point(aes(colour = uptake)) + scale_colour_distiller(palette = 'Reds')```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotScale_Colour4, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>Scale colour type</td><td align = 'left'>`_color_viridis_c`</td><td align = 'left'>Manually define a colour scale that gradually transitions according to a Viridis colourblind safe palette.</td><td align = 'left' rowspan = 2>```{r ggplotScale_Colour5, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point(aes(colour = uptake)) + scale_color_viridis_c(option = "D")```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotScale_Colour5, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>Scale colour type</td><td align = 'left'>`_color_brewer`</td><td align = 'left'>Manually define a colour scale that uses the 'Divergent' (div) `RdYlGr`(palette 2) colorBrewer palette on a **categorical** variable.</td><td align = 'left' rowspan = 2>```{r ggplotScale_Colour6, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point(aes(colour = interaction(Treatment, Type))) + scale_color_brewer(type = 'div', palette = 2)```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotScale_Colour6, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>Scale colour type</td><td align = 'left'>`_color_distiller`</td><td align = 'left'>Apply an interpolated colorbrewer scale 'Oranges' sequential scale (reverse order).</td><td align = 'left' rowspan = 2>```{r ggplotScale_Colour7, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point(aes(colour = uptake)) + scale_color_distiller(type = 'seq', palette = 'Oranges', direction = -1)```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotScale_Colour7, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>Scale colour type</td><td align = 'left'>`_color_fermenter`</td><td align = 'left'>Apply an interpolated colorbrewer scale 'Oranges' sequential scale (reverse order) and binned.</td><td align = 'left' rowspan = 2>```{r ggplotScale_Colour8, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point(aes(colour = uptake)) + scale_color_fermenter(type = 'seq', palette = 'Oranges', direction = -1)```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotScale_Colour8, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>Scale colour type</td><td align = 'left'>`_color_gradientn`</td><td align = 'left'>Apply a user-defined color palette</td><td align = 'left' rowspan = 2>```{r ggplotScale_Colour9, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}my_palette = colorRampPalette(colors=c('red','green','blue'))ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point(aes(colour = uptake)) + scale_color_gradientn(colours = my_palette(5))```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotScale_Colour9, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>Scale colour type</td><td align = 'left'>`_color_hue`</td><td align = 'left'>Apply a hue palette to categorical data.</td><td align = 'left' rowspan = 2>```{r ggplotScale_Colour10, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point(aes(colour = interaction(Treatment, Type))) + scale_color_hue(l=80, c=130)```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotScale_Colour10, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>Scale colour type</td><td align = 'left'>`_colour_grey`</td><td align = 'left'>Apply a hue palette to categorical data.</td><td align = 'left' rowspan = 2>```{r ggplotScale_Colour11, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point(aes(colour = interaction(Treatment, Type))) + scale_colour_grey(start = 0.2, end = 0.8)```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotScale_Colour11, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>Scale colour type</td><td align = 'left'>`_colour_manual`</td><td align = 'left'>Manually set the colours for a categorical variable. One of the colours is a named colour, the other is a hex code.</td><td align = 'left' rowspan = 2>```{r ggplotScale_Colour12, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point(aes(colour = Type)) + scale_colour_manual(breaks = c('Mississippi','Quebec'), values = c('red', '#00AA00'))```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotScale_Colour12, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>Scale colour type</td><td align = 'left'>`_colour_identity`</td><td align = 'left'>Manually set the colours for a categorical variable. One of the colours is a named colour, the other is a hex code.</td><td align = 'left' rowspan = 2>```{r ggplotScale_Colour13, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}CO2 <- CO2 %>% mutate(cUptake = cut(uptake, breaks = c(0,15,30,46), labels = c("red", "#00AA00", 1)))ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point(aes(colour = cUptake)) + scale_colour_identity(labels = c('Low', 'Medium', 'High'), guide = 'legend')```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotScale_Colour13, eval=FALSE, tidy=FALSE}```</td></tr></tbody></table>## Alpha<br><br> ![](05_grammar_of_graphics_files/figure-html/ggplotScale-Alpha-s-1.png){class="thumb-s" #alpha-fillA}```{r ggplotScale-Alpha-s, echo=FALSE, include = FALSE, fig.height=5, fig.width=5, out.width='300px', out.height='300px', message=FALSE}ggplot(data = BOD, aes(x = Time, y = demand)) + geom_point(aes(alpha = demand), size = 12) + theme_classic()```_alpha_ is used to represent the opacity of lines and solidsymbols. An _alpha_ value of 0 is transparent and an _alpha_ value of1 is fully opaque.<details><summary>Show attributes</summary>| Parameter | default ||----------------------------------------+----------|| `range` - output range of alpha values | c(0.1,1) |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>scale</th><th align = 'left'>Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>Scale alpha type</td><td align = 'left'>`_alpha_continuous`</td><td align = 'left'>Manually define a narrower alpha scale.</td><td align = 'left' rowspan = 2>```{r ggplotScale_Alpha1, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point(aes(alpha = uptake)) + scale_alpha_continuous(range = c(0.5, 1))```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotScale_Alpha1, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>Scale alpha type</td><td align = 'left'>`_alpha_discrete`</td><td align = 'left'>Alpha (transparency) determined by categorical variable(s).</td><td align = 'left' rowspan = 2>```{r ggplotScale_Alpha2, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point(aes(alpha = interaction(Treatment, Type))) + scale_alpha_discrete()```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotScale_Alpha2, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>Scale alpha type</td><td align = 'left'>`_alpha_manual`</td><td align = 'left'>Alpha (transparency) determined manually for a categorical variable(s).</td><td align = 'left' rowspan = 2>```{r ggplotScale_Alpha3, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}CO2 <- CO2 %>% mutate(cUptake = cut(uptake, breaks = c(0,15,30,46), labels = c("red", "#00AA00", 1)))ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point(aes(alpha = cUptake)) + scale_alpha_manual(values = c(0.3, 0.6, 0.95))```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotScale_Alpha3, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>Scale alpha type</td><td align = 'left'>`_alpha_identity`</td><td align = 'left'>Alpha (transparency) determined manually for a categorical variable(s).</td><td align = 'left' rowspan = 2>```{r ggplotScale_Alpha4, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}CO2 <- CO2 %>% mutate(cUptake = cut(uptake, breaks = c(0,15,30,46), labels = c(0.3, 0.6, 0.95)), cUptake = as.numeric(as.character(cUptake)))ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point(aes(alpha = cUptake)) + scale_alpha_identity()```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotScale_Alpha4, eval=FALSE}```</td></tr></tbody></table>:::# Facets (panels) Faceting splits the data up into a matrix of panels on the basis ofone or more categorical vectors. Since facets display subsets of thedata, they are very useful for examining trends in hierarchicaldesigns. There are two faceting function, that reflect twoalternative approaches:- `facet_wrap(~cell)` - creates a set of panels based on a factor and wraps the panels into a 2-d matrix. cell represents a categorical vector or set of categorical vectors- `facet_wrap(row~column)` - creates a set of panels based on a factor and wraps the panels into a 2-d matrix. row and column represents the categorical vectors used to define the rows and columns of the matrix respectivelyWrapped and gridded facets share a number of parameters:- **`facet`** - variables defining rows/columns of the grid. This can be either a formula or vector. **Note, this is not deprecated for`facet_grid`**- `nrow` - number of grid rows- `ncol` - number of grid columns- `scales` - one of "fixed" (default, axes the same across all panels), "free" (axes unique per panel), "free_x" (x-axes free), "free-y" (y-axes free) <details><summary>Explore</summary>```{r ggplotStrip-scales, eval = TRUE, echo=TRUE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'} g <- ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point() ``` <table class='table table-primary table-bordered table-sm paramsTable'> <thead> <tr class = 'header'> <th align = 'left'>Fixed axes scales</th> <th align = 'left'>Free axes scales</th> </tr> </thead> <tbody> <tr class = 'odd'> <td align = 'left'>```{r ggplotStrip-scales1a, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L" style="width:250px;height:250px;"'} g + facet_wrap(~Plant, scales='fixed') ``````{r ggplotStrip-scales1a, eval=FALSE} ``` </td> <td align = 'left'>```{r ggplotStrip-scales1b, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L" style="width:250px;height:250px;"'} g + facet_wrap(~Plant, scales='free') ``````{r ggplotStrip-scales1b, eval=FALSE} ``` </td> </tr> </tbody> </table> </details>- `as.table` - if TRUE (default), arranged from top left to bottom right, otherwise, from bottom left to top right- `drop` - whether to drop factor combinations that lack data (default TRUE)- `shrink` - whether to shrink axes scales to match applied statistics (TRUE, default) or raw data (FALSE)- `labeller` - a function that determines the format of facet (strip) labels (default, 'label_value') <details><summary>Explore</summary>```{r ggplotStrip-labeller, eval = TRUE, echo=TRUE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'} g <- ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point() ``` <table class='table table-primary table-bordered table-sm paramsTable'> <thead> <tr class = 'header'> <th align = 'left'>Default labelling</th> <th align = 'left'>Append variable name to labels</th> </tr> </thead> <tbody> <tr class = 'odd'> <td align = 'left'>```{r ggplotStrip-labeller1a, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L" style="width:250px;height:250px;"'} g + facet_wrap(~Plant, labeller = 'label_value') ``````{r ggplotStrip-labeller1a, eval=FALSE} ``` </td> <td align = 'left'>```{r ggplotStrip-labeller1b, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L" style="width:250px;height:250px;"'} g + facet_wrap(~Plant, labeller = 'label_both') ``````{r ggplotStrip-labeller1b, eval=FALSE} ``` </td> </tr> <tr class = 'header'> <th align = 'left'>Wrap long labels</th> <th align = 'left'>Add special characters/formatting to labels</th> </tr> <tr class = 'odd'> <td align = 'left'>```{r ggplotStrip-labeller1c, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L" style="width:250px;height:250px;"'} CO2 <- CO2 %>% mutate(Species = paste0('Plant from ', Type)) g <- ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point() g + facet_wrap(~Species, labeller = label_wrap_gen(5)) ``````{r ggplotStrip-labeller1c, eval=FALSE, tidy=FALSE} ``` </td> <td align = 'left'>```{r ggplotStrip-labeller1d, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L" style="width:250px;height:250px;"'} CO2 <- CO2 %>% mutate(species = paste0('italic(', Type, '.spp)')) g <- ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point() g + facet_wrap(~species, labeller = 'label_parsed') ``````{r ggplotStrip-labeller1d, eval=FALSE, tidy=FALSE} ``` </td> </tr> </tbody> </table> </details>- `switch` - alternative positions for the panel strips ('x': move top labels to bottom, 'y': move right labels to left, 'both': move both top and right labels to bottom and left) - `dir` - direction in which to wrap panels ('h': horizontally', 'v': vertically) <details><summary>Explore</summary>```{r ggplotStrip-direction, eval = TRUE, echo=TRUE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'} g <- ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point() ``` <table class='table table-primary table-bordered table-sm paramsTable'> <thead> <tr class = 'header'> <th align = 'left'>Wrap panels horizontally</th> <th align = 'left'>Wrap panels vertically</th> </tr> </thead> <tbody> <tr class = 'odd'> <td align = 'left'>```{r ggplotStrip-direction1a, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L" style="width:250px;height:250px;"'} g + facet_wrap(~Plant) ``````{r ggplotStrip-direction1a, eval=FALSE} ``` </td> <td align = 'left'>```{r ggplotStrip-direction1b, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L" style="width:250px;height:250px;"'} g + facet_wrap(~Plant, dir = 'v') ``````{r ggplotStrip-direction1b, eval=FALSE} ``` </td> </tbody> </table> </details>- `strip.position` - position of the panel label ('top','bottom', 'left', 'right') <details><summary>Explore</summary>```{r ggplotStrip-position, eval = TRUE, echo=TRUE, fig.width=2.5, fig.height=2.5, out.extra='class="thumb-L"'} g <- ggplot(data = CO2, aes(x = conc, y = uptake)) + geom_point() ``` <table class='table table-primary table-bordered table-sm paramsTable'> <thead> <tr class = 'header'> <th align = 'left'>Strip labels on top</th> <th align = 'left'>Strip labels on right</th> </tr> </thead> <tbody> <tr class = 'odd'> <td align = 'left'>```{r ggplotStrip-position1a, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L" style="width:250px;height:250px;"'} g + facet_wrap(~Plant) ``````{r ggplotStrip-position1a, eval=FALSE} ``` </td> <td align = 'left'>```{r ggplotStrip-position1b, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L" style="width:250px;height:250px;"'} g + facet_wrap(~Plant, strip.position = 'right') ``````{r ggplotStrip-position1b, eval=FALSE} ``` </td> </body> </table> </details>::: {.panel-tabset}## Facet wrap ![](05_grammar_of_graphics_files/figure-html/ggplotFacet-wrap-s-1.png){class="thumb-s" style="width:80px; height:80px;" #facet-wrapA}```{r ggplotFacet-wrap-s, echo=FALSE, include = FALSE, fig.height=5, fig.width=5, out.width='300px', out.height='300px', message=FALSE}ggplot(CO2, aes(y=uptake, x=conc)) + geom_smooth() + geom_point() + facet_wrap(~Plant) + blank_theme```<details><summary>Show attributes</summary>| Parameter | default ||----------------------------------------------------------------------------------|---------|| `nrow` - number of grid rows | NULL || `ncol` - number of grid columns | NULL || `dir` - direction in which to wrap panels ('h': horizontally', 'v': vertically) | 'h' || `strip.position` - position of the panel label ('top','bottom', 'left', 'right') | 'top' || | |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>facet</th><th align = 'left'>Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>Facet wrap</td><td align = 'left'>`_wrap`</td><td align = 'left'>Matrix of panels split by a single categorical vector</td><td align = 'left' rowspan = 2>```{r ggplotFacet_wrap1, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(CO2, aes(y=uptake, x=conc)) + geom_smooth() + geom_point() + facet_wrap(~Plant)```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotFacet_wrap1, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>Facet wrap</td><td align = 'left'>`_wrap`</td><td align = 'left'>Matrix of panels split by a single categorical vector and wrap the panels vertically</td><td align = 'left' rowspan = 2>```{r ggplotFacet_wrap4, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(CO2, aes(y=uptake, x=conc)) + geom_smooth() + geom_point() + facet_wrap(~Plant, dir = 'v')```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotFacet_wrap4, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>Facet wrap</td><td align = 'left'>`_wrap`</td><td align = 'left'>Matrix of panels split by two categorical vectors</td><td align = 'left' rowspan = 2>```{r ggplotFacet_wrap2, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(CO2, aes(y=uptake, x=conc)) + geom_smooth() + geom_point() + facet_wrap(~Treatment + Type, labeller = labeller(Treatment = label_both, Type = label_both, multi_line = FALSE))```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotFacet_wrap2, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>Facet wrap</td><td align = 'left'>`_wrap`</td><td align = 'left'>Matrix of panels split by a single categorical vector with strip labels moved right of panels</td><td align = 'left' rowspan = 2>```{r ggplotFacet_wrap3, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(CO2, aes(y=uptake, x=conc)) + geom_smooth() + geom_point() + facet_wrap(~Plant, strip.position = 'right')```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotFacet_wrap3, eval=FALSE}```</td></tr></tbody></table>## Facet grid ![](05_grammar_of_graphics_files/figure-html/ggplotFacet-grid-s-1.png){class="thumb-s" style="width:80px; height:80px;" #facet-gridA}```{r ggplotFacet-grid-s, echo=FALSE, include = FALSE, fig.height=5, fig.width=5, out.width='300px', out.height='300px', message=FALSE}ggplot(CO2, aes(y=uptake, x=conc)) + geom_smooth() + geom_point() + facet_grid(Treatment~Type) + blank_theme```<details><summary>Show attributes</summary>| Parameter | default ||---------------------------------------------------------------------------------------------------------------------------------------|---------|| `rows` - variables used to faceting for the rows | NULL || `cols` - variables used to faceting for the columns | NULL || `space` - whether all panels are the same size ('fixed') or proportional to the scale of associated data ('free', 'free_x', 'free_y') | 'fixed' || `margins` - whether to include marginal summaries or which variables to associate marginal summaries for | FALSE || | |: {.primary .bordered .sm .paramsTable}</details><table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>facet</th><th align = 'left'>Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>Facet wrap</td><td align = 'left'>`_wrap`</td><td align = 'left'>Matrix of panels split by two categorical vectors</td><td align = 'left' rowspan = 2>```{r ggplotFacet_grid1, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(CO2, aes(y=uptake, x=conc)) + geom_smooth() + geom_point() + facet_grid(rows = vars(Treatment), cols = vars(Type))```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotFacet_grid1, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>Facet grid</td><td align = 'left'>`_grid`</td><td align = 'left'>Matrix of panels split by two categorical vectors</td><td align = 'left' rowspan = 2>```{r ggplotFacet_grid2, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(CO2, aes(y=uptake, x=conc)) + geom_smooth() + geom_point() + facet_grid(rows = vars(Treatment), cols = vars(Type), margins = TRUE)```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotFacet_grid2, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>Facet grid</td><td align = 'left'>`_grid`</td><td align = 'left'>Matrix of panels split by two categorical vectors</td><td align = 'left' rowspan = 2>```{r ggplotFacet_grid3, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(CO2, aes(y=uptake, x=conc)) + geom_smooth() + geom_point() + facet_grid(rows = vars(Type), cols = vars(Treatment), space = 'free')```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotFacet_grid3, eval=FALSE}```</td></tr></tbody></table>:::# Themes ```{r ggplotThemes-s, echo=FALSE, include = FALSE, fig.height=5, fig.width=5, out.width='300px', out.height='300px', message=FALSE}ggplot(CO2, aes(y=uptake, x=conc)) + geom_point() + theme_classic()```Themes govern the overall style of the graphic. In particular, theycontrol:- the look and positioning of the axes (and their ticks, titles and labels)- the look and positioning of the legends (size,alignment, font, direction)- the look of plots (spacing and titles)- the look of panels (background, grid lines)- the look of panels strips (background, alignment, font)<table class='table table-primary table-bordered table-sm paramsTable'><thead><tr class = 'header'><th align = 'left'>Feature</th><th align = 'left'>Theme</th><th align = 'left'>Notes</th><th align = 'left'>Example plot</th></tr></thead><tbody><tr class = 'odd'><td align = 'left'>Black and white theme</td><td align = 'left'>`_bw`</td><td align = 'left'>Black and white theme</td><td align = 'left' rowspan = 2>```{r ggplotTheme1, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(CO2, aes(y=uptake, x=conc)) + geom_point() + theme_bw()```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotTheme1, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>Classic theme</td><td align = 'left'>`_classic`</td><td align = 'left'>Black and white theme</td><td align = 'left' rowspan = 2>```{r ggplotTheme2, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(CO2, aes(y=uptake, x=conc)) + geom_point() + theme_classic()```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotTheme2, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>Classic theme</td><td align = 'left'>`_grey`</td><td align = 'left'>Grey theme</td><td align = 'left' rowspan = 2>```{r ggplotTheme3, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(CO2, aes(y=uptake, x=conc)) + geom_point() + theme_grey()```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotTheme3, eval=FALSE}```</td></tr><tr class = 'even'><td align = 'left'>Minimal theme</td><td align = 'left'>`_grey`</td><td align = 'left'>Minimal theme</td><td align = 'left' rowspan = 2>```{r ggplotTheme4, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(CO2, aes(y=uptake, x=conc)) + geom_point() + theme_minimal()```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotTheme4, eval=FALSE}```</td></tr><tr class = 'odd'><td align = 'left'>Empty (void) theme</td><td align = 'left'>`_void`</td><td align = 'left'>Minimal theme</td><td align = 'left' rowspan = 2>```{r ggplotTheme5, echo=FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}ggplot(CO2, aes(y=uptake, x=conc)) + geom_point() + theme_void()```</td></tr><tr><td align = 'left' font-shape=2em colspan=3>```{r ggplotTheme5, eval=FALSE}```</td></tr></tbody></table>Along with these pre-fabricated themes, it is possible to create yourown theme. This is done via the `theme()` function. Non-data themableelements comprise of either a line, rectangle or text. Therefore, theycan all be modified via one of the following functions:- `element_blank()` - remove the element- `element_line()` - set the properties of a line- `element_rect()` - set the properties of a rectangle- `element_text()` - set the properties of textThe spacing around elements is defined by `margin()`. The sizes ofelements can be directly supplied or defined relative to the size ofthe parent element via the `rel()` function.<details><summary>Show attributes</summary>| Parameter | `_line` | `_rect` | `_text` ||-----------------------------------------------------------------------|---------|---------|---------|| `inherit.blank` - | FALSE | FALSE | FALSE || `fill` - element fill colour | | NULL | || `colour` - line/border colour | NULL | NULL | NULL || `size` - line/border line size (mm) or font size (pt) | NULL | NULL | NULL || `linetype` - line type (1:8, name or hex string) | NULL | NULL | || `lineend` - line end style (round, butt, square) | NULL | | || `arrow` - arrow specification | NULL | | || `family` - font family | | | NULL || `face` - font face (plain, italic, bold, bold.italic) | | | NULL || `hjust` - horizontal justification (in [0, 1]) | | | NULL || `vjust` - vertical justification (in [0, 1]) | | | NULL || `angle` - angle (in [0, 360]) | | | NULL || `lineheight` - height of a line of text | | | NULL || `margin` - spacing around text (see `margin()`) | | | NULL || `debug` - if TRUE draws a rectangle behind text and a point at anchor | | | NULL || | | | |: {.primary .bordered .sm .paramsTable}</details>To get an appreciation of the theme elements controlled by a theme,enter the associated function at the command prompt (note, most themesbuild upon the default 'grey' theme):```{r ggplotTheme6, echo=TRUE, eval = FALSE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}theme_grey()```<details><summary>see output</summary>```{r ggplotTheme6, echo=FALSE, eval = TRUE, fig.width=5, fig.height=5, out.extra='class="thumb-L"'}```</details>