首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用rstatix在ggplots图上的不正确p值位置

使用rstatix在ggplots图上的不正确p值位置
EN

Stack Overflow用户
提问于 2020-12-02 05:34:45
回答 1查看 1.2K关注 0票数 0

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

代码语言:javascript
复制
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值的位置太高了。

代码语言:javascript
复制
## 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 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}")

我想在第二个数据文件中有一个不同之处,它导致了这些问题,但我无法弄清楚。谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-26 20:34:07

facet_wrap上的facet_wrap选项一样,在add_xy_position上有一个控制p值位置的scales选项。由于我使用的是"facet_wrap(...,scales = "free")",所以我应该使用add_xy_position(...,scales = "free")来确保位置匹配。

在我的例子中:

代码语言:javascript
复制
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}")

作者的吉特布页面的回答。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65102760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档