来自this帖子的答案解决了第二行具有多行和指数的ggplots的间距问题。具体是这样的:
library(ggplot2)
library(magrittr)
iris %>%
ggplot(aes(x = Sepal.Length, y = Sepal.Length)) +
geom_point() +
labs(x = expression(paste('This is the first line of the x label\n', x^-1, ')')))
但是,我似乎不知道如何从 unicode 中获取上标减号。我尝试了所有这些选项。这是一个获得减号但没有上标减号的示例:
iris %>%
ggplot(aes(x = Sepal.Length, y = Sepal.Length)) +
geom_point() +
labs(x = 'This is the first line of the x label\n(x\U2212\u00b9)')
尝试其他 unicode 值会在 Rstudio 中产生一个像这样的框(尝试
\u207B
):
如有任何帮助,我们将不胜感激!
这里有两个选项可以实现您想要的结果。第一个使用
atop
,第二个使用 ggtext
和 HTML <sup>
标签:
library(ggplot2)
iris |>
ggplot(aes(x = Sepal.Length, y = Sepal.Length)) +
geom_point() +
labs(x = expression(
atop(
"This is the first line of the x label",
(x^{"\u2212" * 1})
)
))
library(ggtext)
iris |>
ggplot(aes(x = Sepal.Length, y = Sepal.Length)) +
geom_point() +
labs(x = "This is the first line of the x label<br>(x<sup>\u22121</sup>)") +
theme(
axis.title = ggtext::element_markdown()
)