Correct positioning of multiple significance labels in ggplot

依然范特西╮ 提交于 2020-05-14 12:11:05

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!