文本幾何對於標記圖很有用。它們可以單獨用作散點圖,也可以與其他幾何圖形結合使用,例如,用於標記點或注釋條形的高度。 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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
