本文介绍了在ggplot中正确放置多个重要性标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在箱图中展示两个小组随着时间的推移而发展的情况,并为每个小组添加有意义的标签(小组内比较).我有一个使用ggpubr::stat_compare_means的有效示例,但是我无法正确定位这两个几何.

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.

我尝试了position = position_dodge(width=0.5)和其他几个位置元素,但是由于某些原因,它们根本不会移动.我想要的输出将使每个标签集水平移动到每个组框上,并垂直调整为不重叠.

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.

使用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")))

推荐答案

尽管我真的不认为这对可视化是个好主意-这是一个解决方案.如果您使用ggpubr,请继续使用ggpubr语法.并使用构面进行分组.

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.改用一张桌子.

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
  )

这篇关于在ggplot中正确放置多个重要性标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 10:47