文本几何对于标记图很有用。它们可以单独用作散点图,也可以与其他几何图形结合使用,例如,用于标记点或注释条形的高度。 geom_text() 仅向绘图添加文本。 geom_label() 在文本后面绘制一个矩形,使其更易于阅读。
用法
geom_label(
  mapping = NULL,
  data = NULL,
  stat = "identity",
  position = "identity",
  ...,
  parse = FALSE,
  nudge_x = 0,
  nudge_y = 0,
  label.padding = unit(0.25, "lines"),
  label.r = unit(0.15, "lines"),
  label.size = 0.25,
  na.rm = FALSE,
  show.legend = NA,
  inherit.aes = TRUE
)
geom_text(
  mapping = NULL,
  data = NULL,
  stat = "identity",
  position = "identity",
  ...,
  parse = FALSE,
  nudge_x = 0,
  nudge_y = 0,
  check_overlap = FALSE,
  na.rm = FALSE,
  show.legend = NA,
  inherit.aes = TRUE
)
参数
- mapping
 - 
由
aes()创建的一组美学映射。如果指定且inherit.aes = TRUE(默认),它将与绘图顶层的默认映射组合。如果没有绘图映射,则必须提供mapping。 - data
 - 
该层要显示的数据。有以下三种选择:
如果默认为
NULL,则数据继承自ggplot()调用中指定的绘图数据。data.frame或其他对象将覆盖绘图数据。所有对象都将被强化以生成 DataFrame 。请参阅fortify()将为其创建变量。将使用单个参数(绘图数据)调用
function。返回值必须是data.frame,并将用作图层数据。可以从formula创建function(例如~ head(.x, 10))。 - stat
 - 
用于该层数据的统计变换,可以作为
ggprotoGeom子类,也可以作为命名去掉stat_前缀的统计数据的字符串(例如"count"而不是"stat_count") - position
 - 
位置调整,可以是字符串,也可以是调用位置调整函数的结果。不能与
nudge_x或nudge_y共同指定。 - ...
 - 
其他参数传递给
layer()。这些通常是美学,用于将美学设置为固定值,例如colour = "red"或size = 3。它们也可能是配对的 geom/stat 的参数。 - parse
 - 
如果是
TRUE,标签将被解析为表达式并按照?plotmath中的说明进行显示。 - nudge_x, nudge_y
 - 
水平和垂直调整以微移标签。对于从点偏移文本非常有用,特别是在离散比例上。不能与
position共同指定。 - label.padding
 - 
标签周围的填充量。默认为 0.25 行。
 - label.r
 - 
圆角半径。默认为 0.15 行。
 - label.size
 - 
标签边框尺寸,以毫米为单位。
 - na.rm
 - 
如果
FALSE,则默认缺失值将被删除并带有警告。如果TRUE,缺失值将被静默删除。 - show.legend
 - 
合乎逻辑的。该层是否应该包含在图例中?
NA(默认值)包括是否映射了任何美学。FALSE从不包含,而TRUE始终包含。它也可以是一个命名的逻辑向量,以精细地选择要显示的美学。 - inherit.aes
 - 
如果
FALSE,则覆盖默认美学,而不是与它们组合。这对于定义数据和美观的辅助函数最有用,并且不应继承默认绘图规范的行为,例如borders()。 - check_overlap
 - 
如果是
TRUE,则不会绘制与同一层中先前文本重叠的文本。check_overlap在绘制时按数据顺序发生。因此,在调用geom_text()之前,应按标签列排列数据。请注意,geom_label()不支持此参数。 
细节
请注意,当您调整绘图大小时,即使绘图区域的大小发生变化,文本标签也会保持相同的大小。发生这种情况是因为文本元素的"width"和"height"为0。显然,文本标签确实有高度和宽度,但它们是物理单位,而不是数据单位。出于同样的原因,默认情况下,堆叠和闪避文本将不起作用,并且轴限制不会自动扩展以包含所有文本。
geom_text() 和 geom_label() 为数据中的每一行添加标签,即使坐标 x、y 在调用 geom_label() 或 geom_text() 中设置为单个值。要在指定点添加标签,请使用 annotate() 和 annotate(geom = "text", ...) 或 annotate(geom = "label", ...) 。
要自动定位不重叠的文本标签,请参阅ggrepel 包。
美学
geom_text() 理解以下美学(所需的美学以粗体显示):
- 
x - 
y - 
label - 
alpha - 
angle - 
colour - 
family - 
fontface - 
group - 
hjust - 
lineheight - 
size - 
vjust 
在 vignette("ggplot2-specs") 中了解有关设置这些美学的更多信息。
结盟
您可以使用 vjust 和 hjust 美学修改文本对齐方式。这些可以是 0(右/下)和 1(上/左)之间的数字或字符( "left" 、 "middle" 、 "right" 、 "bottom" 、 "center" 、 "top" )。有两种特殊的对齐方式: "inward" 和 "outward" 。向内始终将文本朝中心对齐,向外将文本远离中心对齐。
例子
p <- ggplot(mtcars, aes(wt, mpg, label = rownames(mtcars)))
p + geom_text()
# Avoid overlaps
p + geom_text(check_overlap = TRUE)
# Labels with background
p + geom_label()
# Change size of the label
p + geom_text(size = 10)
# Set aesthetics to fixed value
p +
  geom_point() +
  geom_text(hjust = 0, nudge_x = 0.05)
p +
  geom_point() +
  geom_text(vjust = 0, nudge_y = 0.5)
p +
  geom_point() +
  geom_text(angle = 45)
if (FALSE) {
# Doesn't work on all systems
p +
  geom_text(family = "Times New Roman")
}
# Add aesthetic mappings
p + geom_text(aes(colour = factor(cyl)))
p + geom_text(aes(colour = factor(cyl))) +
  scale_colour_discrete(l = 40)
p + geom_label(aes(fill = factor(cyl)), colour = "white", fontface = "bold")
p + geom_text(aes(size = wt))
# Scale height of text, rather than sqrt(height)
p +
  geom_text(aes(size = wt)) +
  scale_radius(range = c(3,6))
# You can display expressions by setting parse = TRUE.  The
# details of the display are described in ?plotmath, but note that
# geom_text uses strings, not expressions.
p +
  geom_text(
    aes(label = paste(wt, "^(", cyl, ")", sep = "")),
    parse = TRUE
  )
# Add a text annotation
p +
  geom_text() +
  annotate(
    "text", label = "plot mpg vs. wt",
    x = 2, y = 15, size = 8, colour = "red"
  )
# \donttest{
# Aligning labels and bars --------------------------------------------------
df <- data.frame(
  x = factor(c(1, 1, 2, 2)),
  y = c(1, 3, 2, 1),
  grp = c("a", "b", "a", "b")
)
# ggplot2 doesn't know you want to give the labels the same virtual width
# as the bars:
ggplot(data = df, aes(x, y, group = grp)) +
  geom_col(aes(fill = grp), position = "dodge") +
  geom_text(aes(label = y), position = "dodge")
#> Warning: Width not defined
#> ℹ Set with `position_dodge(width = ...)`
# So tell it:
ggplot(data = df, aes(x, y, group = grp)) +
  geom_col(aes(fill = grp), position = "dodge") +
  geom_text(aes(label = y), position = position_dodge(0.9))
# You can't nudge and dodge text, so instead adjust the y position
ggplot(data = df, aes(x, y, group = grp)) +
  geom_col(aes(fill = grp), position = "dodge") +
  geom_text(
    aes(label = y, y = y + 0.05),
    position = position_dodge(0.9),
    vjust = 0
  )
# To place text in the middle of each bar in a stacked barplot, you
# need to set the vjust parameter of position_stack()
ggplot(data = df, aes(x, y, group = grp)) +
 geom_col(aes(fill = grp)) +
 geom_text(aes(label = y), position = position_stack(vjust = 0.5))
# Justification -------------------------------------------------------------
df <- data.frame(
  x = c(1, 1, 2, 2, 1.5),
  y = c(1, 2, 1, 2, 1.5),
  text = c("bottom-left", "top-left", "bottom-right", "top-right", "center")
)
ggplot(df, aes(x, y)) +
  geom_text(aes(label = text))
ggplot(df, aes(x, y)) +
  geom_text(aes(label = text), vjust = "inward", hjust = "inward")
# }
相关用法
- R ggplot2 geom_tile 矩形
 - R ggplot2 geom_qq 分位数-分位数图
 - R ggplot2 geom_spoke 由位置、方向和距离参数化的线段
 - R ggplot2 geom_quantile 分位数回归
 - R ggplot2 geom_ribbon 函数区和面积图
 - R ggplot2 geom_boxplot 盒须图(Tukey 风格)
 - R ggplot2 geom_hex 二维箱计数的六边形热图
 - R ggplot2 geom_bar 条形图
 - R ggplot2 geom_bin_2d 二维 bin 计数热图
 - R ggplot2 geom_jitter 抖动点
 - R ggplot2 geom_point 积分
 - R ggplot2 geom_linerange 垂直间隔:线、横线和误差线
 - R ggplot2 geom_blank 什么也不画
 - R ggplot2 geom_path 连接观察结果
 - R ggplot2 geom_violin 小提琴情节
 - R ggplot2 geom_dotplot 点图
 - R ggplot2 geom_errorbarh 水平误差线
 - R ggplot2 geom_function 将函数绘制为连续曲线
 - R ggplot2 geom_polygon 多边形
 - R ggplot2 geom_histogram 直方图和频数多边形
 - R ggplot2 geom_segment 线段和曲线
 - R ggplot2 geom_density_2d 二维密度估计的等值线
 - R ggplot2 geom_map 参考Map中的多边形
 - R ggplot2 geom_density 平滑密度估计
 - R ggplot2 geom_abline 参考线:水平、垂直和对角线
 
注:本文由纯净天空筛选整理自Hadley Wickham等大神的英文原创作品 Text。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
