0

我无法使用 rstatix 将 p 值放置在 ggplot 的 y 轴上的正确位置。我可以让包作者博客上提供的示例正常工作,但是当我更改值时,位置不正确。工作版本:

library(tidyverse)
library(rstatix)

##Example provided by the package author which works correctly

df <- ToothGrowth%>%
  as_tibble()

#Check df
df

#Stats calculation
stat.test <- df %>%
  group_by(dose) %>%
  t_test(len ~ supp) %>%
  adjust_pvalue(method = "bonferroni") %>%
  add_significance()

# Make facet and add p-values
stat.test <- stat.test %>% add_xy_position(x = "supp", fun = "max")

#Check p value positions - y.position looks good
stat.test

#Plot
ggplot(df, aes(x = supp, y = len)) +
  geom_boxplot() +
  geom_jitter() +
  facet_wrap( ~ dose, scales = "free") +
  stat_pvalue_manual(stat.test, hide.ns = F,
                     label = "{p.adj}")

但是当我更改值时,p 值的位置太高了。

## My example which plots incorrectly

##--- This is a very inelegant way to change the values!!
df <- ToothGrowth %>%
  mutate(helper = paste0(supp, dose))

df$RecordingNo <- ave(seq.int(nrow(df)), df$helper, FUN = seq_along)

df <- df %>%
  select(-helper) %>%
  pivot_wider(names_from = c(dose), values_from = len) %>%
  mutate(`0.5` = `0.5` * 0.1) %>%
  mutate(`2` = `2` * 10) %>%
  select(-RecordingNo) %>%
  pivot_longer(-supp) %>%
  rename(len = value, dose = name) %>%
  mutate(dose = as_factor(dose)) %>%
  as_tibble()

#Check df
df

##------

#This is code is exactly the same as the working code above.

#Stats calculation
stat.test <- df %>%
  group_by(dose) %>%
  t_test(len ~ supp) %>%
  adjust_pvalue(method = "bonferroni") %>%
  add_significance()

# Make facet and add p-values
stat.test <- stat.test %>% add_xy_position(x = "supp", fun = "max")

#Check p value positions - y.position looks incorrect
stat.test

ggplot(df, aes(x = supp, y = len)) +
  geom_boxplot() +
  geom_jitter() +
  facet_wrap( ~ dose, scales = "free") +
  stat_pvalue_manual(stat.test, hide.ns = F,
                     label = "{p.adj}")

我想导致问题的第二个数据帧存在差异,但我无法弄清楚。谢谢!

4

1 回答 1

1

scales选项 on一样facet_wrap,还有一个scales选项 on add_xy_position可以控制 p 值的位置。当我使用时,"facet_wrap(...,scales = "free")"我应该使用add_xy_position(...,scales = "free")来确保位置匹配。

在我的例子中:

stat.test <- stat.test %>% add_xy_position(x = "supp", fun = "max",scales = "free")

ggplot(df, aes(x = supp, y = len)) +
  geom_boxplot() +
  geom_jitter() +
  facet_wrap( ~ dose, scales = "free") +
  stat_pvalue_manual(stat.test, hide.ns = F,
                     label = "{p.adj}")

来自作者的 Github页面的回答。

于 2021-02-26T20:34:07.913 回答