热衷于在 ggplot 中添加 x̄= 17 标签?

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

我想出了如何在 ggplot 标签中添加 x̄ :

library(ggplot2)
ggplot() +
  geom_text(mapping= aes(x= 5, y= 5, label= "bar(x)"), parse= TRUE)

但是我需要做的是标记类似 x̄= 17 的内容。不幸的是,

geom_text(mapping= aes(x= 5, y= 5, label= "bar(x)= 17"), parse= TRUE)
不起作用,因为
=
也会被解析。到目前为止我能做的是:

ggplot() +
  geom_text(mapping= aes(x= 5, y= 5, label= "bar(x)"), parse= TRUE) +
  geom_text(mapping= aes(x= 5.3, y= 5, label= "=17")) +
  scale_x_continuous(limits= c(0, 10))

这看起来确实符合预期,但问题是我使用不同的数据,具有不同的比例,并且我的一些图使用

facet_grid()
。因此,在我的图中,
=17
有时会分开。像这样的东西:

ggplot() +
  geom_text(mapping= aes(x= 5, y= 5, label= "bar(x)"), parse= TRUE) +
  geom_text(mapping= aes(x= 5.3, y= 5, label= "=17")) +
  scale_x_continuous(limits= c(5, 6))

如何在ggplot中正确添加x̄= 17标签?谢谢!

r ggplot2 unicode label
1个回答
0
投票

你可以这样做:

install.packages('latex2exp')
library(latex2exp)
ggplot() +
  geom_text(mapping= aes(x= 5, y= 5, label= TeX(r'($\bar{x} = 17$)', output = "character")), size= 10, parse = TRUE)

感谢this博客文章让我复习了在 ggplot 图中嵌入 LaTeX

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