if (FALSE) { # require("ggiraph", quietly = TRUE, character.only = TRUE)
#
library(ggplot2)
library(gdtools)
font_set_liberation()
# -------------------------------------------------------------------
# Basic usage with markdown formatting
# -------------------------------------------------------------------
df <- data.frame(
x = 1:3,
y = 1:3,
label = c("**Bold**", "*Italic*", "`Code`")
)
x <- girafe(
ggplot(df, aes(x, y, label = label)) +
geom_text_md(),
options = list(opts_sizing(rescale = FALSE)),
dependencies = list(
liberationsansHtmlDependency(),
liberationmonoHtmlDependency()
)
)
x
# -------------------------------------------------------------------
# Customizing size and colour aesthetics
# -------------------------------------------------------------------
df <- data.frame(
x = c(1, 2, 3),
y = c(1, 2, 3),
label = c(
"**Small** text",
"**Medium** text",
"**Large** text"
),
size = c(3, 5, 7),
color = c("steelblue", "darkgreen", "firebrick")
)
x <- girafe(
ggplot(df, aes(x, y, label = label, size = size, colour = color)) +
geom_text_md(width = 1) +
scale_size_identity() +
scale_colour_identity() +
scale_x_continuous(expand = c(.15, .15)) +
theme_minimal(),
options = list(opts_sizing(rescale = FALSE)),
dependencies = list(
liberationsansHtmlDependency(),
liberationmonoHtmlDependency()
)
)
x
# -------------------------------------------------------------------
# Using different font families
# -------------------------------------------------------------------
df <- data.frame(
x = c(1, 1, 1),
y = c(3, 2, 1),
label = c(
"**Sans-serif** font (*default*)",
"**Serif** font (*elegant*)",
"**Monospace** font (`code style`)"
),
family = c("Liberation Sans", "Liberation Serif", "Liberation Mono")
)
x <- girafe(
ggplot(df, aes(x, y, label = label, family = family)) +
geom_text_md(hjust = 0) +
xlim(0.5, 3) +
theme_void(),
options = list(opts_sizing(rescale = FALSE)),
dependencies = list(
liberationsansHtmlDependency(),
liberationserifHtmlDependency(),
liberationmonoHtmlDependency()
)
)
x
# -------------------------------------------------------------------
# Text rotation with angle aesthetic
# -------------------------------------------------------------------
df <- data.frame(
x = c(1, 2, 3, 4),
y = c(1, 1, 1, 1),
label = "**Rotated** *text*",
angle = c(0, 45, 90, -45)
)
x <- girafe(
ggplot(df, aes(x, y, label = label, angle = angle)) +
geom_text_md(size = 4) +
scale_x_continuous(expand = c(.15, .15)) +
ylim(0.5, 1.5) +
theme_minimal(),
options = list(opts_sizing(rescale = FALSE)),
dependencies = list(
liberationsansHtmlDependency()
)
)
x
# -------------------------------------------------------------------
# Justification with hjust and vjust
# -------------------------------------------------------------------
df <- data.frame(
x = rep(2, 9),
y = rep(2, 9),
hjust = rep(c(0, 0.5, 1), 3),
vjust = rep(c(0, 0.5, 1), each = 3),
label = paste0(
"**h=",
rep(c(0, 0.5, 1), 3),
"**\n*v=",
rep(c(0, 0.5, 1), each = 3),
"*"
)
)
x <- girafe(
ggplot(df, aes(x, y, label = label, hjust = hjust, vjust = vjust)) +
geom_point(color = "red", size = 3) +
geom_text_md(size = 3) +
facet_grid(vjust ~ hjust, labeller = label_both) +
xlim(1, 3) +
ylim(1, 3) +
theme_minimal(),
options = list(opts_sizing(rescale = FALSE)),
dependencies = list(
liberationsansHtmlDependency()
)
)
x
# -------------------------------------------------------------------
# Text wrapping with width parameter
# -------------------------------------------------------------------
df <- data.frame(
x = 1,
y = 1,
label = paste0(
"This is a **long text** that will be *wrapped* ",
"automatically when it exceeds the specified `width`."
)
)
x <- girafe(
ggplot(df, aes(x, y, label = label)) +
geom_text_md(width = 2, hjust = 0) +
xlim(0.5, 3) +
theme_void(),
options = list(opts_sizing(rescale = FALSE)),
dependencies = list(
liberationsansHtmlDependency(),
liberationmonoHtmlDependency()
)
)
x
# -------------------------------------------------------------------
# Alpha transparency
# -------------------------------------------------------------------
df <- data.frame(
x = 1:4,
y = rep(1, 4),
label = "**Text**",
alpha = c(1, 0.7, 0.4, 0.2)
)
x <- girafe(
ggplot(df, aes(x, y, label = label, alpha = alpha)) +
geom_text_md(size = 8) +
scale_alpha_identity() +
theme_minimal(),
options = list(opts_sizing(rescale = FALSE)),
dependencies = list(
liberationsansHtmlDependency()
)
)
x
# -------------------------------------------------------------------
# Practical example: Annotated scatter plot
# -------------------------------------------------------------------
x <- girafe(
ggplot(mtcars, aes(mpg, wt)) +
geom_point(colour = "steelblue", size = 2) +
geom_smooth(method = "lm", se = FALSE, colour = "firebrick") +
geom_text_md(
label = "**R\u00B2 = 0.75**\n*p < 0.001*\n\n`lm(wt ~ mpg)`",
x = 32,
y = 5,
hjust = 1,
vjust = 1,
size = 3.5,
colour = "gray30"
) +
labs(
title = "MPG vs Weight",
x = "Miles per Gallon",
y = "Weight (1000 lbs)"
) +
theme_minimal(base_family = "Liberation Sans"),
options = list(opts_sizing(rescale = FALSE)),
dependencies = list(
liberationsansHtmlDependency(),
liberationmonoHtmlDependency()
)
)
x
}
Run the code above in your browser using DataLab