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

时间:2018-01-31 20:01:10

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

1 个答案:

答案 0 :(得分: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

enter image description here

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

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

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

感谢@KGee指出which_layer函数自版本0.3.0起已从ggpmisc包移出。

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

如上所示更改textsize参数。

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