问题
I want to present two groups with their development over time in a boxplot, and add labels of significance for each group (intra-group comparisons). I have a working example using ggpubr::stat_compare_means
, but I am unable to correctly position the two geoms.
I have attempted position = position_dodge(width=0.5)
and several other positional elements, but for some reasons they wont move at all. My desired output would have each label set horizontally shifted to be over each groups box, and vertically adjusted to not overlap.
Example code using diamonds
:
df <- filter(diamonds, color == "J" | color == "E")
ggplot(data = df, aes(x = cut, y = price, fill = color)) +
geom_boxplot() +
stat_compare_means(method = "t.test",data = filter(df, color == "J"), comparisons = list(c("Fair","Good"),c("Fair","Very Good"),c("Fair","Premium"))) +
stat_compare_means(method = "t.test",data = filter(df, color == "E"), comparisons = list(c("Fair","Good"),c("Fair","Very Good"),c("Fair","Premium")))
回答1:
Although I really don't think this is a good idea for visualisation - here is a solution. If you use ggpubr, stay in the ggpubr syntax. And use faceting for subgrouping.
P.S. Try a table instead.
library(tidyverse)
library(ggpubr)
mydf <- filter(diamonds, color == "J" | color == "E")
comparisons <- list(c("Fair", "Good"), c("Fair", "Very Good"), c("Fair", "Premium"))
ggboxplot(mydf, x = "cut", y = "price", facet.by = "color") +
stat_compare_means(
method = "t.test", ref.group = "Fair", label = "p.format",
comparisons = comparisons
)
Created on 2020-03-20 by the reprex package (v0.3.0)
来源:https://stackoverflow.com/questions/60772321/correct-positioning-of-multiple-significance-labels-in-ggplot