问题
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:
回答1:
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
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
来源:https://stackoverflow.com/questions/48550525/ggpubr-change-font-size-of-stat-compare-means-kruskal-wallis-p-values