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

gui*_*oli 4 r ggplot2 kruskal-wallis ggpubr

如何更改下图中的字体大小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
Run Code Online (Sandbox Code Playgroud)

情节:

在此处输入图片说明

mar*_*kus 6

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
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

该解决方案有点丰富,但是可行。我找不到另一种方法来覆盖第一次调用之后创建textsizegeom_signif图层的参数stat_compare_means

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

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

感谢@KGee指出该which_layer功能已从ggpmisc0.3.0版开始从软件包中移出。

library(gginnards)
which_layers(p, "GeomSignif")
## [1] 2
Run Code Online (Sandbox Code Playgroud)

textsize像上面显示的那样更改参数。

p$layers[[which_layers(p, "GeomSignif")]]$aes_params$textsize <- your_font_size
Run Code Online (Sandbox Code Playgroud)

  • 对于任何偶然发现此问题的人,似乎which_layers()函数现在已移至gginnards软件包中,因此您需要首先安装它。 (2认同)