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.Gapminder data is a dataset for the survey on each country with its life expectancy, GDP per capita, and population size. The data is collected by Gapminder Foundation.
country: a categorical variable 142 levelscontinent :a categorical variable with 5 levelsyear: going from 1952 to 2007 in increments of 5 yearspop : populationgdpPercap: GDP per capitalifeExp: life expectancyRead the data
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 gm 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() |
# Create a scatter plot with customized axis and color legend labels
p +
geom_point(aes(y = lifeExp, color=continent)) + # Add points layer for data representation
scale_x_continuous(name="GDP per capita") + # Customize x-axis label
scale_y_continuous(name="Life Expectancy",breaks=c(25,50,75)) + # Customize y-axis label
scale_colour_discrete(name="Continent",labels=c("Afr", "Ameri", "Asia", "Eur", "Ocean")) # Customize color legend titleggplot2