原文:https://cran.r-project.org/web/packages/ggtext/vignettes/plotting_text.html
ggtext包定義了兩個新的geoms, geom_richtext()和geom_textbox()翅睛,用于為圖形進(jìn)行文本標(biāo)記匿又, 分別繪制簡單的文本標(biāo)記(沒有自動換行)和文本框(帶自動換行)
使用geom_richtext()理肺,能將markdown形式的文本標(biāo)置于圖形中⊥龊牵可以看做是以前常用的geom_label()或geom_text()的代替品。
例子1——簡單文本標(biāo)記
- 使用r2的值添加圖形注釋助琐,用iris數(shù)據(jù)為例憎账,用傳統(tǒng)的geom_text()標(biāo)記如下:
library(ggplot2)
library(dplyr)
library(glue)
iris_cor <- iris %>%
group_by(Species) %>%
summarize(r_square = cor(Sepal.Length, Sepal.Width)^2) %>%
mutate(
# location of each text label in data coordinates
Sepal.Length = 8, Sepal.Width = 4.5,
# text label containing r^2 value
label = glue("r^2 = {round(r_square, 2)}")
)
iris_cor
#> # A tibble: 3 x 5
#> Species r_square Sepal.Length Sepal.Width label
#> <fct> <dbl> <dbl> <dbl> <glue>
#> 1 setosa 0.551 8 4.5 r^2 = 0.55
#> 2 versicolor 0.277 8 4.5 r^2 = 0.28
#> 3 virginica 0.209 8 4.5 r^2 = 0.21
iris_facets <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point() +
geom_smooth(method = "lm", formula = y ~ x) +
facet_wrap(~Species) +
theme_bw()
iris_facets +
geom_text(
data = iris_cor,
aes(label = label),
hjust = 1, vjust = 1
代碼可以work趴捅,但是結(jié)果不讓人滿意垫毙。1)r是數(shù)學(xué)變量,應(yīng)該斜體拱绑。2)2應(yīng)該是上標(biāo)不是^2综芥。用geom_richtext()可以輕松創(chuàng)建markdown標(biāo)記。
library(ggtext)
iris_cor_md <- iris_cor %>%
mutate(
# markdown version of text label
label = glue("*r*<sup>2</sup> = {round(r_square, 2)}")
)
iris_cor_md
#> # A tibble: 3 x 5
#> Species r_square Sepal.Length Sepal.Width label
#> <fct> <dbl> <dbl> <dbl> <glue>
#> 1 setosa 0.551 8 4.5 *r*<sup>2</sup> = 0.55
#> 2 versicolor 0.277 8 4.5 *r*<sup>2</sup> = 0.28
#> 3 virginica 0.209 8 4.5 *r*<sup>2</sup> = 0.21
iris_facets +
geom_richtext(
data = iris_cor_md,
aes(label = label),
hjust = 1, vjust = 1
)
默認(rèn)情況下猎拨,geom_richtext() 要給文本加一個框膀藐,可以通過設(shè)置填充色和外框線的顏色讓它不可見 (fill = NA, label.colour = NA)屠阻。
iris_facets +
geom_richtext(
data = iris_cor_md,
aes(label = label),
hjust = 1, vjust = 1,
# remove label background and outline
fill = NA, label.color = NA,
# remove label padding, since we have removed the label outline
label.padding = grid::unit(rep(0, 4), "pt")
)
如同ggplot2一樣,可以各自選擇偏愛的標(biāo)記線额各、填充和文章的顏色国觉。
iris_facets +
aes(colour = Species) +
geom_richtext(
data = iris_cor_md,
aes(
label = label,
fill = after_scale(alpha(colour, .2))
),
text.colour = "black",
hjust = 1, vjust = 1
) +
theme(legend.position = "none")
還可以旋轉(zhuǎn)文字標(biāo)記(雖然不建議這么做)。
iris_facets +
aes(colour = Species) +
geom_richtext(
data = iris_cor_md,
aes(
x = 7.5,
label = label,
fill = after_scale(alpha(colour, .2))
),
text.colour = "black",
hjust = 1, vjust = 1,
angle = 30
) +
theme(legend.position = "none")
文本框
用geom_textbox()為圖形添加markdown形式的文本框(帶自動換行)虾啦÷榫鳎可以指定文本框的寬度,可以是絕對寬度(如"cm","pt", "in")傲醉,也可以是相對寬度(如"npc")蝇闭。
df <- data.frame(
x = 0.1,
y = 0.8,
label = "*Lorem ipsum dolor sit amet,* consectetur adipiscing
elit. Quisque tincidunt eget arcu in pulvinar. Morbi varius leo
vel consectetur luctus. **Morbi facilisis justo non fringilla.**
Vivamus sagittis sem felis, vel lobortis risus mattis eget. Nam
quis imperdiet felis, in convallis elit."
)
p <- ggplot() +
geom_textbox(
data = df,
aes(x, y, label = label),
width = grid::unit(0.73, "npc"), # 73% of plot panel width
hjust = 0, vjust = 1
) +
xlim(0, 1) + ylim(0, 1)
p
如果改變了圖形的寬度,文本框會隨著改變硬毕,文本自動適應(yīng)呻引。
image.png
用hjust和vjust對齊x和y軸定義的參考點(diǎn),它不會影響文本框中的文本吐咳。文本框的文本對齊逻悠,可以用halign和valign。例如用halign=0.5進(jìn)行中心文本對齊挪丢。
ggplot() +
geom_textbox(
data = df,
aes(x, y, label = label),
width = grid::unit(0.73, "npc"), # 73% of plot panel width
hjust = 0, vjust = 1,
halign = 0.5 # centered text
) +
xlim(0, 1) + ylim(0, 1)
文本框雖然不能任意角度旋轉(zhuǎn)蹂风,但是可以有四個選擇。
df <- data.frame(
x = 0.5,
y = 0.5,
label = "The quick brown fox jumps over the lazy dog.",
orientation = c("upright", "left-rotated", "inverted", "right-rotated")
)
ggplot() +
geom_textbox(
data = df,
aes(x, y, label = label, orientation = orientation),
width = grid::unit(1.5, "in"),
height = grid::unit(1.5, "in"),
box.margin = grid::unit(rep(0.25, 4), "in"),
hjust = 0, vjust = 1
) +
xlim(0, 1) + ylim(0, 1) +
scale_discrete_identity(aesthetics = "orientation")