Data visualization in ggplot2
By the end of this session, you will
ggplot2 figureWe DO NOT expect:
ggplot2or
Golden rule: refers 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 collect the 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/Boxpot))
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 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 titleggplot2count_matrix.csvRNA (In rows) expresion level for each sample (in column)
sample_metadata.csvInformation for each mouse
Documents folder.setwd(PATH/TO/NEW/WORKING/DIRECTORY) to change the working directory.getwd() to see your current working directory.Consider the following file structure
There are two ways of specifying a path:
/ (root)
├── home
│ └── user
│ ├── documents
│ │ ├── course
│ │ │ ├── THIS_MARKDOWN.rmd
│ │ │ └── IMAGE.png
│ │ └── notes.txt
│ └── downloads
| └── data.csv
└── varReminder for Windows system
\ instead of / in your file explorer. In R, you need to transform these \s to / if you directly copy and paste the file path.C:\Users\Ray\Blah\BlahAbsolute Path
An absolute path starts from the root directory (/) and specifies the full path to a file or directory.
THIS_MARKDOWN.rmd: /home/user/documents/course /THIS_MARKDOWN.rmddata.csv: /home/user/downloads/data.csvRelative Path
A relative path is defined relative to the current working directory.
./ refer to the current directory,../ means “go up one directory level”.Assume that we are working on THIS_MARKDOWN.rmd, so the current directory is /home/user/documents /course.
IMAGE.png : ./IMAGE.png or IMAGE.pngdata.csv: ../../data.csv