查看原文
其他

R 语言|公式上下标、斜粗体、文本颜色、小数位数等|几乎包含全部所需(更新)

ecologyR ecologyR 2024-04-01

👉 鼠标修改布局!全球独家 R 语言 SEM 可视化工具

👉 完全用 R 语言实现 Manuel Delgado-Baquerizo 风格的 SEM 图

👉️ 尝试用 R 语言画一个 PNAS 文章的 SEM 图

👉 R 语言 SEM 之多组分析(Multigroup)

👉 R 语言 SEM 之复合变量(Composite)的构造案例
👉 R 语言 SEM 之使用 ggplot2 衍生包画 Nature 结构方程模型图
👉 R 语言 SEM 合集

(点击 👉 即可跳转。)

引言

如题,简单记录之。

  • 几乎包括了所有最常用的用法(更新版)!

1 expression 数学公式(上下标、斜体、粗体等)
2 bquote 换行、公式里引用参数等
3 sprintf 格式化的文本、数字组合(保留小数位数)
4 ggtext 坐标轴文字颜色、markdown、富文本格式

0 加载包

library(ggplot2)
library(dplyr)

expression:数学公式(上下标、斜体、粗体等)

# 1. expression:数学公式(上下标、斜体、粗体等)
font <- "Source Sans Pro"
theme_set(
theme_minimal(
base_family = font,
base_size = 16
) +
theme(
panel.grid.major.x = element_blank(),
panel.grid.minor = element_blank()
)
)

ggplot() +
annotate(
"text",
x = 0,
y = 0.02,
# 斜体(italic)
# 两个文本,使用 ~ 连接,表示空格;使用 * 连接,表示无空格
label = "italic(Eq) * ':' ~ y == 3 * x^2 + 1 * ',' ~ italic(P) == 0.003 *',' ~ italic(R)^2 == 0.7",
size = 9,
family = font,
parse = TRUE
) +
annotate(
"text",
x = 0,
y = 0.01,
label = "CO[2] * ',' ~ N[2] * O ~ (italic(µ) * g ~ g^-1 ~ h^1)",
size = 9,
family = font,
parse = TRUE
) +
annotate(
"text",
x = 0,
y = 0,
label = "bold('BOLD text') ~ '|' ~ bolditalic('BOLD text')", # 加粗 & 斜体加粗
size = 9,
family = font,
parse = TRUE
) +
labs(
x = expression(italic("K") ^-1), # 上标
y = expression("NO" [3] ^"-" ~ ", NH" [4] ^"+"),
title = expression(italic("L")["p"]), # 下标
subtitle = expression()
) +
coord_cartesian(clip = "off")

bquote:换行、公式里引用参数等

# 2. bquote:换行、引用参数等
# https://www.r-bloggers.com/2018/03/math-notation-for-r-plot-titles-expression-and-bquote/

# 通过 .(value) 引用数值
some.value1 <- 3.14159
par(cex = 2)
plot(0,
0,
pch = 19,
main = bquote("Hello" ~ r[xy] == .(some.value1) ~ "and" ~ beta^2)
)

# 换行
plot(
0,
0,
main = bquote(
atop(
"first line:" ~ r[xy] == .(cor) ~ "and" ~ beta^2,
"next line:" ~ alpha^-2
)
)
)

sprintf:格式化的文本、数字组合

# 3. 格式化的文本、数字组合(sprintf)
some.value2 <- 3.1
some.string <- "is a string"

par(cex = 2)
plot(
0,
0,
pch = 19,
xlab = sprintf("Two digits: %.2f", some.value2), # %.2f 两位小数(包括零)
ylab = sprintf("Text: %s", some.string),
main = sprintf("number: %.4f\ntext: %s", some.value2, some.string) # %.4f 四位小数(包括零)
)

ggtext:markdown、富文本格式(坐标轴文字颜色等)

  • 其实 ggtext 写公式可能更方便。

# 4. 富文本格式 / markdown(ggtext)
library(ggtext)

ggplot(NULL) +
geom_richtext(
aes(x = 0, y = 1),
label = "Regular",
family = font
) +
geom_richtext(
aes(x = 0, y = 0),
label = "Line 1<br>Line 2",
family = font
) +
geom_richtext(
aes(x = 0, y = -1),
label = "<span style = 'color:red'>Red color</span>
<span style = 'color:orange'>Orange color</span>",
family = font
) +
geom_richtext(
aes(x = 0, y = -2),
label = "<span style = 'font-size:14pt'>Larger text</span>
<span style = 'font-size:10pt'>smaller text</span>",
family = font
) +
geom_richtext(
aes(x = 0, y = -3),
label = "*P* = 0.003, *R*<sup>2</sup> = 0.7",
family = font,
fill = NA, # remove background
label.color = NA, # remove outline
size = 9
) +
geom_richtext(
aes(x = 0, y = -4),
label = "N<sub>2</sub>O (*µ*g g<sup>-1</sup> h<sup>-1</sup>)",
family = font,
fill = "white", # white background
label.color = NA, # remove outline
size = 9
)

# 设置每个 Species 对应的颜色
iris$Species <- factor(iris$Species) # 必须是具有 levels 的 factor
n_class <- nlevels(iris$Species)
n_Species <- length(unique(iris$Species))
clrs3 <- RColorBrewer::brewer.pal(n_Species, "Dark2")
clr3_map <- setNames(clrs3, levels(iris$Species))
clr3_map
setosa versicolor virginica
"#1B9E77" "#D95F02" "#7570B3"
iris |>
mutate(
clr_Species = clr3_map[Species],
clr_Text = sprintf("<span style ='color:%s'>%s</span>", clr_Species, Species)
) |>
ggplot(aes(clr_Text, Petal.Length, color = Species)) +
geom_point() +
scale_color_manual(values = clr3_map) +
theme(
axis.text.x = element_markdown() # x 轴文字是 markdown 格式
)

mpg$class <- factor(mpg$class) # 必须是具有 levels 的 factor
n_class <- nlevels(mpg$class)
clrs7 <- RColorBrewer::brewer.pal(n_class, "Dark2")
clr7_map <- setNames(clrs7, levels(mpg$class))
clr7_map
2seater compact midsize minivan pickup subcompact suv
"#1B9E77" "#D95F02" "#7570B3" "#E7298A" "#66A61E" "#E6AB02" "#A6761D"
mpg |>
mutate(
clr_class = clr7_map[class],
clr_Text = sprintf("<span style ='color:%s'>%s</span>", clr_class, class)
) |>
ggplot(aes(clr_Text, displ, color = class)) +
geom_boxplot() +
scale_color_manual(values = clr7_map) +
theme(
panel.grid.major.x = element_blank(),
axis.text.x = element_markdown(size = 12) # x 轴文字是 markdown 格式
)

# 同理 facet 文字也很容易设置颜色
mpg$class <- factor(mpg$class) # 必须是具有 levels 的 factor
n_class <- nlevels(mpg$class)
clrs7 <- RColorBrewer::brewer.pal(n_class, "Dark2")
clr7_map <- setNames(clrs7, levels(mpg$class))

mpg |>
mutate(
clr_class = clr7_map[class],
clr_Text = sprintf("<span style ='color:%s'>%s</span>", clr_class, class)
) |>
ggplot(aes(hwy, displ, color = class)) +
geom_point() +
scale_color_manual(values = clr7_map) +
facet_wrap(. ~ clr_Text) +
theme(
strip.text = element_markdown() # strip 文字是 markdown 格式
)


继续滑动看下一个
向上滑动看下一个

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存