R 语言绘图中如何添加特殊符号文本?

今天有个培训班的小伙伴问了一个这样的问题:

老师,分面facet的时候,想把每个小图的标题带个特殊符号该怎么操作呢。举个例子,原来分面出来的标题是1/2/3现在想改成1℃/2℃/3℃

解决方法有很多,这里提供一种。

首先我们找个绘制分面图的案例:

library(tidyverse)
iris %>%
as_tibble()
#> # A tibble: 150 x 5
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> <dbl> <dbl> <dbl> <dbl> <fct>
#> 1 5.1 3.5 1.4 0.2 setosa
#> 2 4.9 3 1.4 0.2 setosa
#> 3 4.7 3.2 1.3 0.2 setosa
#> 4 4.6 3.1 1.5 0.2 setosa
#> 5 5 3.6 1.4 0.2 setosa
#> 6 5.4 3.9 1.7 0.4 setosa
#> 7 4.6 3.4 1.4 0.3 setosa
#> 8 5 3.4 1.5 0.2 setosa
#> 9 4.4 2.9 1.4 0.2 setosa
#> 10 4.9 3.1 1.5 0.1 setosa
#> # … with 140 more rows

iris %>%
ggplot() +
geom_point(aes(Sepal.Length, Sepal.Width)) +
facet_wrap(~Species)

为了演示如何在小图的标题上添加摄氏度符号,我们把 Species 变量替换下:

unique(iris$Species)
#> [1] setosa versicolor virginica
#> Levels: setosa versicolor virginica

iris %>%
mutate(Species = case_when(
Species == "setosa" ~ "1&#176;C",
Species == "versicolor" ~ "2&#176;C",
Species == "virginica" ~ "3&#176;C",
)) -> iris2

这里的 &#176; 在 HTML 里面表示摄氏度的小圆圈,然后我们可以使用 ggtext 包的 element_textbox() 元素:

iris2 %>% 
ggplot() +
geom_point(aes(Sepal.Length, Sepal.Width)) +
facet_wrap(~Species) +
theme(strip.text = ggtext::element_textbox())

再例如:

iris2 %>% 
ggplot() +
geom_point(aes(Sepal.Length, Sepal.Width)) +
facet_wrap(~Species) +
labs(title = "&omicron;&para;&sect;&times;&frac14;&pi;") +
theme(strip.text = ggtext::element_textbox(),
plot.title = ggtext::element_textbox())

下面是一些的 HTML 特殊符号的表示方法:

符号 说明 对应编码(使用时去掉&后的空格) 英文读法
& AND 符号 & amp; ampersand
< 小于 & lt; little
·> 大于 & gt; great
不断行的空白格 & nbsp; number space
半方大的空白 & ensp;
全方大的空白 & emsp;
¿ 倒问号 & iquest; inverted question
? 问号 & quest; question
« 左书名号 & laquo; left angle quote
» 右书名号 & raquo; right angle quote
引号 & quot; quote
左单引号 & lsquo; left single quote
右单引号 & rsquo: right single quote
左双引号 & ldquo: left double quote
右双引号 & rdquo: right double quote
段落符号 & para; paragraph
§ 章节符 & sect; section
× 乘号 & times; times
÷ 除号 & divide; divide
± 加减号 & plusmn; plus minus
ƒ function & fnof;
根号 & radic; radic
无穷大 & infin; infinite
° & deg; degree
不等号 & ne; ne
恒等于 & equiv; equivalent
小于等于 & le; less than or equal to
大于等于 & ge; great than or equal to
垂直符号 & perp; perpendicular
左箭头 & larr; left arrow
右箭头 & rarr; right arrow
上箭头 & uarr; up arrow
下箭头 & darr; down arrow
水平箭头 & harr; horizontal arrow
竖直箭头 & varr; vertical arrow
双线左箭头 & lArr; left arrow
双线右箭头 & rArr; right arrow
双线上箭头 & uArr; up arrow
双线上箭头 & dArr; down arrow
双线水平双箭头 & hArr; horizontal arrow
双线竖直箭头 & vArr; vertical arrow
黑桃 & spades; spades
红桃 & hearts; hearts
梅花 & clubs; club
方块 & diams; diamonds
© 版权 & copy; copy right
® 注册商标 & reg; registration
商标 & trade; trade
¥ 人民币 & yen;
欧元 & euro; euro
¢ 美分 & cent; cent
£ 英磅 & pound; pound
& oplus;
½ 二分之一 & frac12; fraction
¼ 四分之一 & frac14; fraction
千分符号 & permil; per mille
所以 & there4; there fore
π 圆周率 & pi;
¹ 商标 1 & sup1; super 1
α alpha & alpha; alpha
β beta & beta; beta
γ gamma & gamma; gamma
δ delta & delta; delta
θ theta & theta; theta
λ lambda & lambda; lambda
σ sigma & sigma; sigma
τ tau & tau; tau