添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

How can I change the font size of stat_compare_means on the plot below? I.e, change the "Kruskal-Wallis, p = 1.5e-09" and the other p-values font size? I would like to use a smaller font size than the default one...

Following the data example...

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

Plot:

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

The solution is a bit copious but works. I couldn't find another way to overwrite the textsize parameter of the geom_signif layer that is created after the first call to stat_compare_means.

The parameter is stored here: p$layers[[2]]$aes_params$textsize and can manually be modified.

If you need this manipulation for another plot in which the order of the layers might differ from this example, you can use the which_layer function from the gginnards package to detect this layer (or any other) using the following code.

Thanks to @KGee for pointing out that the which_layer function was moved from the ggpmisc package as of version 0.3.0.

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

Change the textsize argument like shown above.

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

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.