10/2/24, 1:33 PM Ggplot2 Demo: Introduction to ggplot2 Components
Ggplot2 Demo: Introduction to ggplot2
Components
Thao D.
2024-09-29
Intro to components of ggplots
Setting up libs
if(!require("palmerpenguins"))[Link]("palmerpenguins")
library(palmerpenguins) # dataset
library(ggplot2) #base ggplot2
library(dplyr)
library(wesanderson)
library(ggdist)
library(ggExtra)
library(ggpmisc)
library(patchwork)
Import Penguins Data Set
head(penguins)
## # A tibble: 6 × 8
## species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
## <fct> <fct> <dbl> <dbl> <int> <int>
## 1 Adelie Torgersen 39.1 18.7 181 3750
## 2 Adelie Torgersen 39.5 17.4 186 3800
## 3 Adelie Torgersen 40.3 18 195 3250
## 4 Adelie Torgersen NA NA NA NA
## 5 Adelie Torgersen 36.7 19.3 193 3450
## 6 Adelie Torgersen 39.3 20.6 190 3650
## # ℹ 2 more variables: sex <fct>, year <int>
summary(penguins)
[Link] 1/26
10/2/24, 1:33 PM Ggplot2 Demo: Introduction to ggplot2 Components
## species island bill_length_mm bill_depth_mm
## Adelie :152 Biscoe :168 Min. :32.10 Min. :13.10
## Chinstrap: 68 Dream :124 1st Qu.:39.23 1st Qu.:15.60
## Gentoo :124 Torgersen: 52 Median :44.45 Median :17.30
## Mean :43.92 Mean :17.15
## 3rd Qu.:48.50 3rd Qu.:18.70
## Max. :59.60 Max. :21.50
## NA's :2 NA's :2
## flipper_length_mm body_mass_g sex year
## Min. :172.0 Min. :2700 female:165 Min. :2007
## 1st Qu.:190.0 1st Qu.:3550 male :168 1st Qu.:2007
## Median :197.0 Median :4050 NA's : 11 Median :2008
## Mean :200.9 Mean :4202 Mean :2008
## 3rd Qu.:213.0 3rd Qu.:4750 3rd Qu.:2009
## Max. :231.0 Max. :6300 Max. :2009
## NA's :2 NA's :2
Data
Observation and variables to be visualized. In this case:
Body mass
Bill length
Species and Island as categories
[Link] 2/26
10/2/24, 1:33 PM Ggplot2 Demo: Introduction to ggplot2 Components
Asthetics
Visual elements (color, shape, position, size, etc) used to encode data In this case:
color -> species
x position
y position
Scales (and guides)
Scales translate data units into visual units, guides translate the visual units back to data units
In this case:
Continuous x-y axes
Categorical - species
Geoms Objects
Objects, often having multiple asthetics, that present data visually
In this case:
Data is represented using points
Trend is represented as a line
Stats Objects
Any calculation or tranformations applied to the data in order to plot it
In this case:
For points: none
Line: linear regression model
Facets
Plots can be split into small multiples by a variable In this case:
Faceted by Island
Coordinate System
How spatial positions are represented on paper (or screen), eg: map, X-Y, polar, radial, etc. In this case: Cartesian
coordinates - default
Coding time
1. Plot a jitter plot of raw data with mean +- sd
[Link] 3/26
10/2/24, 1:33 PM Ggplot2 Demo: Introduction to ggplot2 Components
Solution code goes here
# Modify this code
ggplot(data=penguins,
mapping= aes(x=island,
y=body_mass_g))
[Link] 4/26
10/2/24, 1:33 PM Ggplot2 Demo: Introduction to ggplot2 Components
[Link] on top, with violin, color by sex
[Link] 5/26
10/2/24, 1:33 PM Ggplot2 Demo: Introduction to ggplot2 Components
Solution code goes here
# Modify this code
library(wesanderson) #BottleRocket2
ggplot(data=penguins,
mapping= aes(x=island,
y=body_mass_g)) +
geom_boxplot() +
geom_jitter()
[Link] 6/26
10/2/24, 1:33 PM Ggplot2 Demo: Introduction to ggplot2 Components
3. Scaling axes & color
[Link] 7/26
10/2/24, 1:33 PM Ggplot2 Demo: Introduction to ggplot2 Components
Solution code goes here
# custom_cols = c("Adelie"="#CD0405","Chinstrap"="#AD7007","Gentoo"="#7570b3")
ggplot(penguins,
mapping = aes(x=body_mass_g,
y=bill_length_mm,
color=species)) +
facet_grid(rows=vars(island))
[Link] 8/26
10/2/24, 1:33 PM Ggplot2 Demo: Introduction to ggplot2 Components
4. (Optional) Example: using stat_summary()
[Link] 9/26
10/2/24, 1:33 PM Ggplot2 Demo: Introduction to ggplot2 Components
Solution code goes here
# Modify this code
ggplot(data=penguins,
mapping= aes(x=island,
y=body_mass_g))
[Link] 10/26
10/2/24, 1:33 PM Ggplot2 Demo: Introduction to ggplot2 Components
# Modify the line to spread out 80% of pop from the mean????
5. Distribution: Hist, Density plot
[Link] 11/26
10/2/24, 1:33 PM Ggplot2 Demo: Introduction to ggplot2 Components
Solution code goes here
ggplot(data=penguins,
mapping = aes(
x=body_mass_g,
y = species
))
[Link] 12/26
10/2/24, 1:33 PM Ggplot2 Demo: Introduction to ggplot2 Components
6. More options for distribution plot
[Link] 13/26
10/2/24, 1:33 PM Ggplot2 Demo: Introduction to ggplot2 Components
Reference ([Link]
Solution Code
ggplot(data=penguins,
mapping = aes(
x=body_mass_g,
y = island
))
[Link] 14/26
10/2/24, 1:33 PM Ggplot2 Demo: Introduction to ggplot2 Components
6.1 Marginal
[Link] 15/26
10/2/24, 1:33 PM Ggplot2 Demo: Introduction to ggplot2 Components
##### Solution Code
ggplot(data=penguins,
mapping = aes(
x=bill_depth_mm,
y = bill_length_mm,
size=body_mass_g,
color= species
))
[Link] 16/26
10/2/24, 1:33 PM Ggplot2 Demo: Introduction to ggplot2 Components
7. Small Multiples with Facet
facet_wrap()
[Link] 17/26
10/2/24, 1:33 PM Ggplot2 Demo: Introduction to ggplot2 Components
Solution Code
ggplot(data=penguins,
mapping = aes(
x=body_mass_g,
y = island
))
[Link] 18/26
10/2/24, 1:33 PM Ggplot2 Demo: Introduction to ggplot2 Components
facet_grid()
[Link] 19/26
10/2/24, 1:33 PM Ggplot2 Demo: Introduction to ggplot2 Components
Solution Code
ggplot(data=penguins,
mapping = aes(
x=body_mass_g,
y = bill_length_mm
))
[Link] 20/26
10/2/24, 1:33 PM Ggplot2 Demo: Introduction to ggplot2 Components
[Link] & Annotation
[Link] 21/26
10/2/24, 1:33 PM Ggplot2 Demo: Introduction to ggplot2 Components
Solution Code
ggplot(data=penguins,
mapping = aes(
x=body_mass_g,
y = bill_length_mm
))
[Link] 22/26
10/2/24, 1:33 PM Ggplot2 Demo: Introduction to ggplot2 Components
9. Combining plots - Positioning
p1<-vio_plot
p2<-half_eye_plot
p3<-box_wrap
p4<-dens_plot+theme([Link] = "none")
p4 | p2/p1
[Link] 23/26
10/2/24, 1:33 PM Ggplot2 Demo: Introduction to ggplot2 Components
layout <- '
A#B
#C#
D#E
'
wrap_plots(D = p1, C = p2, B = p3, design = layout)
[Link] 24/26
10/2/24, 1:33 PM Ggplot2 Demo: Introduction to ggplot2 Components
p1 + inset_element(p4,left = 0.6, bottom = 0.6, right = 1, top = 1)
[Link] 25/26
10/2/24, 1:33 PM Ggplot2 Demo: Introduction to ggplot2 Components
Bonus
ggplot2 official docs ([Link]
ggplot2 other ([Link]
Color Template ([Link]
Layout Multiple Plots with Patchwork ([Link]
[Link] 26/26