plot
: Makes scatterplots, line plots, among other plots.lines
: Adds lines to an already-made plot.par
: Change plotting options.hist
: Makes a histogram.boxplot
: Makes a boxplot.text
: Adds text to an already-made plot.legend
: Adds a legend to an already-made plot.mosaicplot
: Makes a mosaic plot.barplot
: Makes a bar plot.jitter
: Adds a small value to data (so points don’t overlap on a plot).rug
: Adds a rugplot to an already-made plot.polygon
: Adds a shape to an already-made plot.points
: Adds a scatterplot to an already-made plot.mtext
: Adds text on the edges of an already-made plot.table
: Builds frequency and two-way tables.density
: Calculates the density.loess
: Calculates a smooth line.predict
: Predicts new values based on a model.?plot.default
. Useful ones are:main
: This controls the title.xlab
, ylab
: These control the x and y axis labels.col
: This will control the color of the lines/points/areas.cex
: This will control the size of points.pch
: The type of point (circle, dot, triangle, etc…)lwd
: Line width.lty
: Line type (solid, dashed, dotted, etc…).ggplot2
.par
sets the graphics options, where mfrow
is the parameter controling the facets.old_options
. The last line reinstates the old options.ggplot2
is included in the tidyverse
package, and is the current standard for data visualization in R. Authored by Hadley Wickham, gg
stands for “Grammar of Graphics.” In learning ggplot2
, you may find the following cheat sheet to be a helpful reference.ggplot2
are:ggplot2
requires these ingredients. To create a plot, one must specify the desired geom; which data variables are to be aesthetically mapped to the geom; and the scales to use to control the mapping. The skeleton of any ggplot2
command is as follows; parts in italics are to be replaced with specific data variable names, geoms, and scales:x
scale.A
and B
to the point
geom on a Cartesian plane. Note in ?geom_point
that two scales are required for aesthetic mappings to point geoms, x and y:aes()
command.?shape
help file, we can find code to see all possible shapes:aes()
command and specify the variables we wish to map. Consider the following code, and note the different looks, error messages and warnings that appear when attempting to apply aesthetic mappings using various scales depending on the data type. In ggplot
-speak, “continuous” refers to quantitative data in general; while “discrete” refers to categorical data:ggplot2
package is the idea of layers. Aesthetic mappings to different geoms can take place simply by specifying additional mappings with a +
sign. For example, suppose we want to create the above scatterplots with points and lines. This requires two aesthetic mappings: one from the data to the points geom, and one from the data to the lines geom. We can see this in what follows. Note that because both geom_point()
and geom_line()
rely on the same aesthetic mapping, we could simplify the code by specifiying the appropriate mapping in the initial ggplot()
command. The following two lines of code are equivalent:Challenge
C
to geom_line()
using the size scale?dplyr
commands) may work better. Well-structured data will save you lots of time when making figures with ggplot.data
argumentaes
), by selecting the variables to be plotted and the scales to define the geomsgeoms
– graphical representation of the data in the plot (points, lines, bars). To add a geom to the plot use +
operator+
in the ggplot2
package allows you to modify existing ggplot
objects. This means you can easily set up plot “templates” and conveniently explore different types of plots, so the above plot can also be generated with code like this:ggplot()
function can be seen by any geom layers that you add (i.e., these are universal plot settings). This includes the x and y axis you set up in aes()
.ggplot()
function.+
sign used to add layers must be placed at the end of each line containing a layer. If, instead, the +
sign is added in the line before the other layer, ggplot2
will not add the new layer and will return an error message.surveys
data set, overplotting of points can be a limitation of scatter plots. One strategy for handling such settings is to use hexagonal binning of observations. The plot space is tessellated into hexagons. Each hexagon is assigned a color based on the number of observations that fall within its boundaries. To use hexagonal binning with ggplot2
, first install the R package hexbin
from CRAN:geom_hex()
function:alpha
) to avoid overplotting:weight
over species_id
with the plot types showing in different colors. Is this a good way to show this type of data?geom_violin()
.scale_y_log10()
.hindfoot_length
. Overlay the boxplot layer on a jitter layer to show actual measurements.plot_id
).plot_id
. Consider changing the class of plot_id
from integer to factor. Why does this change how R makes the graph?ggplot
counts the number of observations in each level and plots those counts on the y-axis. For example, consider plotting the number of each sex in the data set:dplyr
to group the data by sex and compute the mean length. We can also filter out the unlabeled sexes:mean_length
variable to the y scale. We must specify stat='identity'
inside the geom_bar()
command, to specify that we want to use the actual values in the mean_length
column on the y-axis instead of the default count:Challenges
group_by()
, summarize()
, n()
and mean()
to create a data set with the species IDs in one column, the number of observations in the second column, and the mean hind foot length in the third column.group = species_id
:color
also automatically groups the data):year
, species_id
, and sex
:color
(within a single plot):theme_bw()
. Additionally, you can remove the grid:ggplot2
themestheme_bw()
, which changes the plot background to white, ggplot2
comes with several other themes which can be useful to quickly change the look of your visualization. The complete list of themes is available at http://docs.ggplot2.org/current/ggtheme.html. theme_minimal()
and theme_light()
are popular, and theme_void()
can be useful as a starting point to create a new hand-crafted theme.ggplot2
extensions website provides a list of packages that extend the capabilities of ggplot2
, including additional themes.Challenge
facet_wrap
geometry extracts plots into an arbitrary number of dimensions to allow them to cleanly fit on one page. On the other hand, the facet_grid
geometry allows you to explicitly specify how you want your plots to be arranged via formula notation (rows ~ columns
; a .
can be used as a placeholder that indicates only one row or column).ggplot2
cheat sheet, and think of ways you could improve the plot.Challange
ggplot2
cheat sheet for inspiration.width
, height
and dpi
):width
and height
also determine the font size in the saved plot.