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

标签 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

剧情:

enter image description here

最佳答案

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指出 which_layer 函数已从 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

关于r - ggpubr:更改 stat_compare_means Kruskal-Wallis p 值的字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48550525/

相关文章:

r - 在具有多个组的箱线图中使用 ggplot2 指示显着性

python - Python 中 Kruskal-Wallis 检验的输入格式

r - 使用箱线图查找和可视化最好和最差的项目

r - 有没有办法在 scale_discrete_manual 中定义多种美学?

r - R中多边形的Cating核密度估计

r - 将 ggplot 省略号限制为现实/可能的值

r - 如何更改 R 中随机森林的分割标准?

r - ggplot geom_bar 如果值高于阈值,我可以突出显示不同颜色的条吗

matlab - 如何处理Matlab中Kruskal-Wallis检验中的缺失值?

R,时间序列分解中的不完全元素