ggplot2
, Part 12024-10-21
A good analogy for R packages is that they are like apps you can download onto a mobile phone:
knitr
tidyverse
rstatix
janitor
ggridges
devtools
oi_biostat_data
oi_biostat_data
here
library()
commandTip: at the top of your Qmd file, create a chunk that loads all of the R packages you want to use in that file.
Use the library()
command to load each required package.
library()
commands to load needed packages must be in the Qmd fileggplot2
ggplot2
in tidyverseggplot2
is tidyverse’s data visualization package
gg
in “ggplot2” stands for Grammar of Graphics
It is inspired by the book Grammar of Graphics by Leland Wilkinson
Each variable must have its own column.
Each observation must have its own row.
Each value must have its own cell.
ggplot2
needs at least the following three to produce a chart:
ggplot2 uses data to construct a plot
Works best with tiday data (when every observation is a row and each variable is a column)
First step in plotting:
ggplot
function, which stores the data to be used later by other parts of the plotting systemmpg
dataset, we would start as follows:Mappings use the aes()
function to map variables to the different axes on a plot
aes()
stands for “aesthetics”cty
and hwy
columns to map to the x- and y-coordinates in the plot, we can do that as follows:Every layer consists of three important parts:
The geometry that determines how data are displayed, such as points, lines, or rectangles
The statistical transformation that may compute new variables from the data and affect what of the data is displayed.
The position adjustment that primarily determines where a piece of data is being displayed
A layer can be constructed using the geom_*()
and stat_*()
functions
Here is how we can use two layers to display the cty
and hwy
columns of the mpg
dataset as points and stack a trend line on top:
Data: still mpg
Mapping: using aesthetic to specify only one variable in the x-axis (cty
)
Layers: using geom_histogram()
to show a plot of the counts per cty
(which is city mileage)
Make sure you are working in a Quarto document that has all the libraries loaded
Use glimpse()
to look at the variables in mpg
Choose one of the variables to make a plot for
Go to this site: https://bookdown.dongzhuoer.com/hadley/ggplot2-book/geom
Make a plot for the variable!
05:00
We can change labels!
Increase (or decrease) text size so we can read it / it fits nicely!
element_text()
function
05:00
ggplot
ggplot2
package website: https://ggplot2.tidyverse.org/articles/ggplot2.htmlggplot2
: https://ggplot2-book.org/ggplot2
: https://socviz.co/index.html#prefaceR06 Slides