ggpubr:更改 stat_compare_means Kruskal-Wallis p 值的字体大小

ggpubr: change font size of stat_compare_means Kruskal-Wallis p-values

如何更改下图中 stat_compare_means 的字体大小?即,更改 "Kruskal-Wallis, p = 1.5e-09" 和其他 p 值字体大小?我想使用比默认字体更小的字体...

按照数据示例...

library(ggpubr)
data("ToothGrowth")
compare_means(len ~ dose,  data = ToothGrowth)

# Visualize: Specify the comparisons I want
my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )

# Plotting
ggboxplot(ToothGrowth, x = "dose", y = "len",
          color = "dose", palette = "jco")+ 
stat_compare_means(comparisons = my_comparisons)+ # Add pairwise comparisons p-value
stat_compare_means(label.y = 50)     # Add global p-value

剧情:

your_font_size <- 2

p <- ggboxplot(ToothGrowth, x = "dose", y = "len", color = "dose", palette = "jco") + 
 stat_compare_means(comparisons = my_comparisons) + 
 stat_compare_means(label.y = 50, size = your_font_size)

p$layers[[2]]$aes_params$textsize <- your_font_size
p

该解决方案有点丰富但有效。我找不到另一种方法来覆盖在第一次调用 stat_compare_means 后创建的 geom_signif 层的 textsize 参数。

参数存储在这里:p$layers[[2]]$aes_params$textsize,可以手动修改。

如果您需要对另一个图进行此操作,其中层的顺序可能与此示例不同,您可以使用 gginnards 包中的 which_layer 函数来检测该层(或任何其他)使用以下代码。

感谢 @KGee for pointing out that the which_layer function was moved from the ggpmisc 版本 0.3.0 的软件包。

library(gginnards)
which_layers(p, "GeomSignif")
## [1] 2

如上所示更改 textsize 参数。

p$layers[[which_layers(p, "GeomSignif")]]$aes_params$textsize <- your_font_size