在绘图区添加文字、标签框和公式

函数索引 / 文字与数学排版
geom_markup() · geom_markup_label() · geom_formula() · annotate_markup() · annotate_formula()
把每条观测的文字或公式显示在给定坐标,也可以在固定位置加入一条注释。
怎么用
为数据图层映射 x、y、label。geom_markup_label() 提供带内边距的标签框;固定注释可用 annotate_markup() 或 annotate_formula()。图层文字大小以毫米计。
返回结果
返回 ggplot2 图层。阅读方向作用于文字段落,数值坐标仍遵循原图的坐标系。
调用方式
geom_markup(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
format = "markdown",
width = NULL,
lang = NULL,
text_direction = "auto",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
align = "left",
paragraph_spacing = 0
)
geom_markup_label(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
format = "markdown",
width = NULL,
lang = NULL,
text_direction = "auto",
padding = grid::unit(1.5, "mm"),
radius = grid::unit(1, "mm"),
border = "grey50",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
geom_formula(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
annotate_markup(
x,
y,
label,
...,
format = "markdown",
width = NULL,
lang = NULL,
text_direction = "auto"
)
annotate_formula(x, y, label, ...)参数
mapping、data、stat、position、...、format、width、lang、text_direction、na.rm、show.legend、inherit.aes、align、paragraph_spacing、padding、radius、border、x, y、label
R 示例
ggplot2::ggplot(mtcars, ggplot2::aes(wt, mpg)) +
ggplot2::geom_point() + annotate_formula(4, 30, "frac(x[i], sigma)")
labels <- data.frame(x = 1:2, y = 1, site = c("**North**", "**South**"),
expression = c("x[1]", "x[2]"))
p <- ggplot2::ggplot(labels, ggplot2::aes(x, y)) +
geom_markup(ggplot2::aes(label = site)) +
geom_markup_label(ggplot2::aes(y = y + 1, label = site), fill = "grey95") +
geom_formula(ggplot2::aes(y = y - 1, label = expression)) +
annotate_markup(1.5, 2.5, "Two **sites**")
ggplot2::layer_data(p, 1L)[c("x", "y", "label")]