Data visualization in ggplot2
By the end of this session, you will
ggplot2ggplot2 figureWe DO NOT expect:
ggplot2About Ray’s part
I am responsible for introducing commonly used libraries in R. So most of the time we will keep using functions that are predefined by others.
Questions that Ray could barely give you an explanation better than AI/documentation:
?FUNCTION_NAMECompetency you need to develop:
Use code formatting for special terms and examples, such as This or This are code.
Code chunk, and it is executable when you open the file in RStudio
.qmd version file won’t have the image display but the content is the sameor
Golden rule: refer to the documentation of the specific package
About ggplot2
ggplot2 is part of the tidyverse collection of R packages.iris is a dataset introduced by Ronald Fisher in 1936 that collects iris flower morphology measurements from 3 species
Setosa,VersicolorVirginicait measures the length and width of iris’s sepal and petal
ggplot2 exampleThinking process and decision flowchart will help you determine which type of graph to use (at least within the scope of this course):
flowchart LR
A{Data type} -->|Continuous| B{Purpose}
A{Data type} -->|Discrete| C{Purpose}
B{Purpose}-->|Exploration| D((Histogram/Boxplot))
B{Purpose} -->|Association| E((Scatter plot))
B{Purpose} -->|Association+time| T((Line plot))
C{Purpose}-->|Exploration| F((Bar chart))
C{Purpose} -->|Association| G((Tree map))
First, we define the coordinate system for our iris data in R
The function is ggplot(), which takes two arguments 1
data: The data you are going to usemapping: Should be wrapped in aes() specifying how each column maps to the coordinate.The general format for plotting items will be
geom_XXX(mapping=aes())
In aes(), you can map anything to the figure, common terms could be
xycolor, or colourfill, shape, etc.These functions are named in the format geom_XXXX:
| Function name | |
|---|---|
| Histogram | geom_histogram() |
| Box chart | geom_boxplot() |
| Bar chart | geom_bar() |
| Scatter chart | geom_point() |
| Line chart | geom_line() |
+ operator to stack on the original ggplot()geom_XXX()ggplot()Some notes 1: color affects the border color (dots/lines), fill affects the rectangle color
Scales are functions indicate how each variable behave and presents on the graph. The function usually in the format scale_(AES)_(datatype):
| Discrete | Continuous | |
|---|---|---|
| X | scale_x_discrete() |
scale_x_continuous() |
| Y | scale_y_discrete() |
scale_y_continuous() |
| color | scale_color_discrete() |
scale_color_continuous() |
Most of the time you only need to handle the presentation in 
| Argument name | Axis | Legend |
|---|---|---|
| name | Label | Title |
| breaks | Ticks | Key |
| labels | Tick label | Key Label |
# Create a scatter plot with customized axis and color legend labels
p +
geom_point(aes(y = Petal.Width, color=Species)) + # Add points layer for data representation
scale_x_continuous(name="Sepal Length") + # Customize x-axis label
scale_y_continuous(name="Petal Width") + # Customize y-axis label
scale_colour_discrete(name="Species name") # Customize color legend titleggplot2