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

7
我该如何更改下面绘图中 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

情节:

enter image description here

1个回答

12
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

enter image description here

这个解决方案有点繁琐,但是有效。我找不到另一种方法来覆盖第一次调用 stat_compare_means 后创建的 geom_signif 层的 textsize 参数。
该参数存储在此处:p$layers[[2]]$aes_params$textsize,可以手动修改。
如果您需要在另一个图中进行此操作,其中图层的顺序可能与此示例不同,则可以使用 gginnards 包中的 which_layer 函数来检测此图层(或任何其他图层),使用以下代码。
感谢 @KGee 指出自版本0.3.0起,which_layer 函数已从 ggpmisc 包中移动。
library(gginnards)
which_layers(p, "GeomSignif")
## [1] 2

像上面所示更改textsize参数。

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

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