如何在ggplot2的geom_bracket中添加数学公式/下标?

7

你好!

我想在ggplot2中的geom_bracket中包含一个带下标的标签。我尝试了不同的方法,但都没有成功(在注释中列出了尝试的方法):

library(ggplot2)
ggplot(data = mtcars, aes(x = vs, y = disp)) +
  geom_point() +
  geom_bracket(xmin = .25, xmax = .75, y.position = 250
               ,label = paste0("p_b<", format(0.06, nsmall = 3))
               # ,label = paste0(expression(p[b]), "<", format(0.06, nsmall = 3))
               # ,label = TeX(paste0("p_b<", format(0.06, nsmall = 3)))
               )

我得到了什么:

enter image description here

下标不起作用。

谢谢

1个回答

5

geom_bracket()(来自于 ggpubr 而不是 ggplot2)使用一个参数来指定标签是表达式还是文本,因此您可以这样做:

library(ggplot2)
library(ggpubr)

ggplot(data = mtcars, aes(x = vs, y = disp)) +
  geom_point() +
  geom_bracket(xmin = .25, xmax = .75, y.position = 250,
               label = "p[b] < 0.06", type = "expression")

enter image description here


1
谢谢你,Brow!你救了我的一天!我忘记了那个!我在上面花费了太多时间了。。 - Guilherme Parreira
为了给答案添加更多信息:为了保持格式(带有尾随零),我必须执行以下操作:label = paste0("p[b] < ", deparse(format(0.06, nsmall = 3))) - Guilherme Parreira

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接