ggplot 标签中的上标、多行 -- Unicode 问题

问题描述 投票:0回答:1

来自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
):

enter image description here

如有任何帮助,我们将不胜感激!

r ggplot2 unicode
1个回答
0
投票

这里有两个选项可以实现您想要的结果。第一个使用

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()
  )

© www.soinside.com 2019 - 2024. All rights reserved.