使用 stat_compare_means 和自己的 p.signif 标签

use stat_compare_means with own p.signif labels

我正在努力处理同时使用 ggplot2ggpubr 的情节,我猜一方面是字体问题。另一方面,更通用的解决方案是使用自己的用户定义标签...

当我使用stat_compare_means功能时,一切都很好,除了没有输入星号而是上标,这通常是一种字体设置。我想将 * 替换为带有居中星号的 unicode 字符串,但要到达那里,我需要手动向 stat_compare_means 函数提供数据。我通过使用 compare_means 函数输出来尝试这个,并想用我个人喜欢的标签更新 p.signif 列......但这根本不起作用,我不明白错误消息在那时候。如果要覆盖p.signif字符串,需要如何提供数据?

df <- data.frame(
  name = rep(letters[1:5], each=4), 
  value = c(25.93, 25.77, 25.97, 26.26, 26.44, 26.24, 26.23, 26.35, 26.14, 
            26.21, 26.38, 26.29, 25.83, 25.69, 25.76, 25.59, 26.23, 26.3, 
            26.4, 26.49))


ggplot(df, aes(x=name, y=value, color=name)) + 
  geom_boxplot(outlier.shape=NA) +
  geom_point(color='black', position=position_jitter(width=.15), alpha=.4) +
  # stat_compare_means(ref.group='e', label = "p.signif", method='t.test')  
  stat_compare_means(data=compare_means(value ~ name, data=df, method='t.test', ref.group='e'),
                     label = "p.signif")

通过取消注释该行,情节很好,除了星号,我想将它们更改为不同的字符串。

编辑

所以我离解决方案越来越近了,因为现在我想,label=psignif 不是列名,而是函数本身的设置,我仔细研究了函数的描述。我找到了应该覆盖首选标签的 symnum.args 参数。正是我要找的东西,但由于某种原因它无法处理我的星号 unicode 字符 (I tested some of those) ... 通过使用希腊字母进行测试它起作用了。仅居中的星号未显示。有人有想法吗?

ggplot(df, aes(x=name, y=value, color=name)) + 
  geom_boxplot(outlier.shape=NA) +
  geom_point(color='black', position=position_jitter(width=.15), alpha=.4) +
  # stat_compare_means(ref.group='e', label = "p.signif", method='t.test')  
  # stat_compare_means(data=compare_means(value ~ name, data=df, method='t.test', ref.group='e'),
  #                    label = "p.signif") 
  stat_compare_means(ref.group='e', label = "p.signif", method='t.test', 
                     symnum.args=list(
                       cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, 1), 
                       # symbols = c("\u2217\u2217\u2217\u2217", "\u2217\u2217\u2217", "\u2217\u2217", "\u2217", "ns")))
                       # symbols = c("****", "***", "**", "*", "ns")))
                       symbols = c("\u00B5\u00B5\u00B5\u00B5", "\u00B5\u00B5\u00B5", "\u00B5\u00B5", "\u00B5", "ns")))

我通过设置 stat_compare_means 字体 family='mono' 设法使您的代码正常工作,即

ggplot(df, aes(x=name, y=value, color=name)) + 
  geom_boxplot(outlier.shape=NA) +
  geom_point(color='black', position=position_jitter(width=.15), alpha=.4) +
  stat_compare_means(ref.group='e', label = "p.signif", method='t.test', 
                     symnum.args=list(
                       cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, 1), 
                       symbols = c("\u2217\u2217\u2217\u2217", "\u2217\u2217\u2217", "\u2217\u2217", "\u2217", "ns")),
                       family='mono')
ggsave('test.pdf', width=11.69, height=8.27, device=cairo_pdf)

设置 family='sans'family='serif' 导致出现空框。也许您可以调整字体设置以获得所需的结果。