ggplot
University of Oslo
December 1, 2022
Base
Base
Lattice
Base
Lattice
ggplot2
ggplot2
ggplot2
ggplot2
x
, xmin
, xmax
y
, ymin
, ymax
label
group
fill
, color
size
, alpha
shape
, linetype
ggplot(mtcars, aes(factor(carb), mpg)) +
stat_summary(
fun.data = mean_se,
geom = "pointrange",
width = 0.1,
color = "firebrick"
) +
labs(
x = "Number of carborators",
y = "Mile per gallon"
) +
geom_point(
position = position_jitter(width = 0.05),
shape = 21,
fill = "whitesmoke"
) +
coord_flip() +
annotate(
geom = "text",
y = c(23, 25.5, 27.5),
x = c(0.8, 0.8, 0.8),
label = c("xmin", "x", "xmax"),
family = "consolas",
color = "blue"
)
ggplot(mtcars, aes(factor(carb), mpg)) +
stat_summary(
fun.data = mean_se,
geom = "pointrange",
width = 0.1,
color = "firebrick"
) +
labs(
x = "Number of carborators",
y = "Mile per gallon"
) +
geom_point(
position = position_jitter(width = 0.05),
shape = 21,
fill = "whitesmoke"
) +
annotate(
geom = "text",
y = c(23, 25.5, 27.5),
x = c(0.8, 0.8, 0.8),
label = c("ymin", "y", "ymax"),
family = "consolas",
color = "blue"
)
ggplot(mtcars, aes(factor(carb), mpg)) +
stat_summary(
fun.data = mean_se,
geom = "pointrange",
width = 0.1,
color = "firebrick"
) +
labs(
x = "Number of carborators",
y = "Mile per gallon"
) +
geom_point(
position = position_jitter(width = 0.05),
shape = 21,
fill = "whitesmoke"
) +
geom_text(
data = ~subset(.x, carb >= 6),
aes(label = model),
check_overlap = TRUE,
vjust = -1
)
ggplot(mtcars, aes(factor(carb), mpg, group = am, color = factor(am))) +
geom_point(
position = position_jitter(width = 0.05),
fill = "whitesmoke"
) +
stat_summary(
fun = mean,
geom = "line"
) +
stat_summary(
fun.data = mean_se,
geom = "pointrange",
width = 0.1,
fill = "whitesmoke"
) +
labs(
x = "Number of carborators",
y = "Mile per gallon",
color = "Engine",
fill = "Engine",
) +
theme(
legend.position = c(1, 1),
legend.justification = c(1, 1) + 0.25
)
ggplot(mtcars, aes(factor(carb), mpg, group = am)) +
geom_point(
position = position_jitter(width = 0.05),
fill = "whitesmoke",
aes(shape = factor(am))
) +
stat_summary(
fun = mean,
geom = "line",
aes(linetype = factor(am))
) +
stat_summary(
fun.data = mean_se,
geom = "pointrange",
width = 0.1,
fill = "whitesmoke",
aes(shape = factor(am), linetype = factor(am))
) +
labs(
x = "Number of carborators",
y = "Mile per gallon",
color = "Engine",
linetype = "Engine",
fill = "Engine",
shape = "Engine"
) +
scale_shape_discrete(solid = FALSE) +
theme(
legend.position = c(1, 1),
legend.justification = c(1, 1) + 0.25
)
geom_smooth
)ggplot(mtcars, aes(factor(carb), mpg, group = am)) +
geom_point(
position = position_jitter(width = 0.05),
fill = "whitesmoke",
aes(shape = factor(am))
) +
stat_summary(
fun = mean,
geom = "line",
aes(linetype = factor(am))
) +
stat_summary(
fun.data = mean_se,
geom = "pointrange",
width = 0.1,
fill = "whitesmoke",
aes(shape = factor(am), linetype = factor(am))
) +
labs(
x = "Number of carborators",
y = "Mile per gallon",
color = "Engine",
linetype = "Engine",
fill = "Engine",
shape = "Engine"
) +
scale_shape_discrete(solid = FALSE) +
theme(
legend.position = c(1, 1),
legend.justification = c(1, 1) + 0.25
)
ggplot(mtcars, aes(factor(carb), mpg)) +
geom_point(
position = position_jitter(width = 0.05),
aes(shape = am),
fill = "whitesmoke",
size = rel(3)
) +
geom_smooth(
method = "lm",
formula = "y ~ x",
se = FALSE,
aes(linetype = am, group = am)
) +
labs(
x = "Number of carborators",
y = "Mile per gallon",
color = "Engine",
linetype = "Engine",
fill = "Engine",
shape = "Engine"
) +
scale_shape_discrete(solid = FALSE) +
theme(
legend.position = c(1, 1),
legend.justification = c(1, 1) + 0.25
)
cancer <- as.data.table(survival::cancer)
cancer[, age_group := cut(age, seq(35, 85, 10))]
ggplot(cancer, aes(time)) +
geom_histogram(
aes(y = after_stat(density)),
fill = "#afafaf",
binwidth = 365.241/4,
color = "#a0a0a0",
na.rm = TRUE
) +
geom_density(na.rm = TRUE) +
facet_wrap(
facets = vars(age_group),
) +
labs(title = "Example of facet wrap")
cancer <- as.data.table(survival::cancer)
cancer[, age_group := cut(age, seq(35, 85, 10))]
ggplot(cancer, aes(time)) +
geom_histogram(
aes(y = after_stat(density)),
fill = "#afafaf",
binwidth = 365.241/4,
color = "#a0a0a0",
na.rm = TRUE
) +
geom_density(na.rm = TRUE) +
facet_wrap(
facets = vars(age_group),
labeller = labeller(
age_group = function(x) {
gsub("\\((.*?),(.*?)\\]", "Age Group: \\1-\\2", x)
}
)
) +
labs(title = "Example of facet wrap")
Function to specify (customize) scales. Usually takes the form scale_<GEOM>_<TYPE>
.
cancer <- as.data.table(survival::cancer)
cancer[, age_group := cut(age, seq(35, 85, 10))]
ggplot(cancer, aes(time)) +
geom_histogram(
aes(y = after_stat(density)),
fill = "#afafaf",
binwidth = 365.241/4,
color = "#a0a0a0",
na.rm = TRUE
) +
geom_density(na.rm = TRUE) +
facet_wrap(
facets = vars(age_group),
labeller = labeller(
age_group = function(x) {
gsub("\\((.*?),(.*?)\\]", "Age Group: \\1-\\2", x)
}
)
) +
labs(title = "Example of facet wrap")
ggplot(cancer, aes(time)) +
geom_histogram(
aes(y = after_stat(density)),
fill = "#afafaf",
binwidth = 365.241/4,
color = "#a0a0a0",
na.rm = TRUE
) +
geom_density(na.rm = TRUE) +
facet_wrap(
facets = vars(age_group),
labeller = labeller(
age_group = function(x) {
gsub("\\((.*?),(.*?)\\]", "Age Group: \\1-\\2", x)
}
)
) +
labs(title = "Example of facet wrap") +
scale_x_continuous(
breaks = seq(0, 3, 0.5) * 365.241,
labels = function(x) x / 365.241
)
ggplot(cancer, aes(time)) +
geom_histogram(
aes(y = after_stat(density)),
fill = "#afafaf",
binwidth = 365.241/4,
color = "#a0a0a0",
na.rm = TRUE
) +
geom_density(na.rm = TRUE) +
facet_wrap(
facets = vars(age_group),
labeller = labeller(
age_group = function(x) {
gsub("\\((.*?),(.*?)\\]", "Age Group: \\1-\\2", x)
}
)
) +
labs(title = "Example of facet wrap") +
scale_x_continuous(
breaks = seq(0, 3, 0.5) * 365.241,
labels = function(x) x / 365.241
) +
scale_y_continuous(
labels = scales::label_scientific()
)
mtcars$gear <- as.factor(mtcars$gear)
ggplot(mtcars, aes(hp, mpg, color = gear)) +
geom_point(aes(size = wt), alpha = 0.8) +
scale_size_continuous(breaks = seq(1, 6, 2), limits = c(1, 6)) +
theme(
legend.position = c(1, 1),
legend.justification = c(1.5, 1.5),
legend.direction = "horizontal"
) +
labs(
x = "Horse power",
y = "Mile per gallon",
color = "Gear",
size = "Weight"
)
ggplot2
title
, text
, ticks
, line
background
, margin
, spacing
, key
, title
,
position
, direction
justification
, box
spacing
, grid
, background
title
, subtitle
, caption
, tag
, margin
background
, text
mtcars$gear <- as.factor(mtcars$gear)
plt <- ggplot(mtcars, aes(hp, mpg, color = gear)) +
geom_point(aes(size = wt), alpha = 0.8) +
scale_size_continuous(breaks = seq(1, 6, 2), limits = c(1, 6)) +
facet_grid(rows = vars(am)) +
theme(
legend.position = c(1, 1),
legend.justification = c(1.5, 1.5),
legend.direction = "horizontal"
) +
labs(
title = "Power vs Consumption",
x = "Horse power",
y = "Mile per gallon",
color = "Gear",
size = "Weight"
)
plt
Helpers:
element_text
element_line
element_rect
element_blank
margin
unit
geom
to add custom elementsgeom
can be point
, line
, segment
, rect
and moreannotation_custom
patchwork
, cowplot
gganimate
ggpolot2
ggstatsplot
and ggpubr
ggplot2
with statistics and annotations
Books and Resources
Dataset collection
https://vincentarelbundock.github.io/Rdatasets/datasets.html