使用带有自己的p.signif标签的stat_compare_means

时间:2019-12-10 08:22:04

标签: r ggpubr

我在一起使用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")))

1 个答案:

答案 0 :(得分:1)

我设法通过设置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'导致出现空白框。也许您可以尝试使用字体设置以获得所需的结果。

enter image description here