如何将统计测试的结果作为 ggplot2 facet 中的 plotmath 表达式包含在内

复仇封印

我希望在分面 ggplot 图表中包含多个统计测试的结果。

我已经找到了很多关于如何在标题或注释中包含类似内容的优秀示例(如this),但是,我的兴趣在于将其包含为文本注释,以便我可以在一个图上显示许多测试的结果。

我已经能够使用标准文本注释来做到这一点,但是我想使用polymath/显示我的结果,expressions以便我可以生成一个注释,该注释遵循包[ggstatsplot]1 中实现的 APA 样式指南,请参见下面的示例:

在此处输入图片说明

我已经使用diamonds来自ggplot2. 我尝试过的一些事情包括:

  • 尝试存储bquoteexpression对象为在列wilcox_stats对象-但dplyr似乎并不喜欢它
  • 试图将这一切从ggplot- 但是它变得非常混乱,试图排除所有geom_text想要打印的注释

您可以提供的任何帮助或指示将不胜感激。

# LOAD REQUIRED PACKAGES

library(ggplot2)
library(tidyverse)
library(rstatix)

# CREATE SAMPLE DATA

sample_data <- diamonds %>%
  select(cut, color, table) %>%
  filter(color == c("E","J")) %>%
  mutate(time = factor(case_when(
    table %% 2 == 0 ~ "Before",
    TRUE ~ "After"))) %>%
  group_by(color, time) %>%
  sample_n(100) %>%
  ungroup() %>%
  mutate(numeric_cut = case_when(
    cut == "Ideal" ~ 1, 
    cut == "Premium" ~ 2,     
    cut == "Very Good" ~ 3,
    cut == "Good" ~ 4,
    cut == "Fair" ~ 5))

# STAT TESTS

wilcox_test <- sample_data %>%
  group_by(color) %>%
  wilcox_test(numeric_cut ~ time, paired = TRUE, detailed = TRUE) %>%
  select(color, statistic, p, n1)

wilcox_es <- sample_data %>%
  group_by(color) %>%
  wilcox_effsize(numeric_cut ~ time, paired = TRUE, ci = TRUE) %>%
  select(color, effsize, conf.low, conf.high)

## EXTRACT ELEMENTS OF STAT TESTS AND USE THEM TO CREATE ANNOTATION

wilcox_stats <- left_join(wilcox_test, wilcox_es) %>%
  mutate(statistic = round(statistic, 1)) %>%
  mutate(effsize = round(effsize, 2)) %>%
  mutate(p = round(p, 3)) %>%
  mutate(result = deparse(bquote(
    V[Wilcoxon]==.(statistic)~ #this code does not work
    italics(p)==.p~ 
    hat(r) == .effsize~
    "CI"["95%"]~
    .conf.low~.conf.high~
    n[pairs]==.n1)))

## PREPARE PLOT DATA

plot_data <- sample_data %>%
  group_by(time, cut, color) %>%
  tally() %>%
  ungroup() %>%
  group_by(color) %>%
  mutate(total_n = sum(n)) %>%
  mutate(percent = (n/total_n)*100) %>%
  mutate(percent = round(percent, 1)) %>%
  ungroup() %>%
  left_join(wilcox_stats) %>%
  mutate(result = case_when(
    time == "Before" & cut == "Ideal" ~ "",
    time == "After" & cut == "Ideal" ~ "",
    time == "Before" & cut == "Premium" ~ "",
    time == "After" & cut == "Premium" ~ "",
    time == "Before" & cut == "Very Good" ~ "",
    time == "After" & cut == "Very Good" ~ result,
    time == "Before" & cut == "Good" ~ "",
    time == "After" & cut == "Good" ~ "",
    time == "Before" & cut == "Fair" ~ "",
    time == "After" & cut == "Fair" ~ "")) %>%
  mutate(time = factor(time, levels = c("Before", "After", ordered = TRUE)))

## PLOT RESULTS

plot <- plot_data %>%
  ggplot() +
  aes(x = cut, y = percent, fill = cut) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = result, y = 30), size = 5, parse = TRUE) +
  facet_grid(color ~ time)

下图显示了我希望创建的输出的要点...

在此处输入图片说明

杰博

我可能会使用 paste 创建表达式,(tbh,因为我发现包含变量更容易)。

我稍微缩短了代码,也没有使用你的完整表达,但我认为它应该足以让你明白这个想法。

library(tidyverse)

sample_data <- diamonds %>%
  select(cut, color, table) %>%
  filter(color == c("E","J")) %>%
  mutate(time = if_else(table %% 2 == 0, "Before", "After")) %>%
  group_by(color, time) %>%
  sample_n(100) %>%
  ungroup() %>%
  mutate(numeric_cut = as.numeric(cut))

wilcox_test <- sample_data %>%
  group_by(color) %>%
  rstatix::wilcox_test(numeric_cut ~ time, paired = TRUE, detailed = TRUE) %>%
  select(color, statistic, p, n1)

wilcox_es <- sample_data %>%
  group_by(color) %>%
  rstatix::wilcox_effsize(numeric_cut ~ time, paired = TRUE, ci = TRUE) %>%
  select(color, effsize, conf.low, conf.high)

这里是关键的一点

wilcox_stats <- left_join(wilcox_test, wilcox_es) %>%
  mutate(statistic = round(statistic, 1),
         effsize = round(effsize, 2),
         p = round(p, 3),
         label = paste('V[Wilcoxon]==', statistic, '~italic(p)==~', p))
#> Joining, by = "color"
plot_data <- sample_data %>%
  count(time, cut, color) %>%
  group_by(color) %>%
  mutate(total_n = sum(n),
         percent = round((n/total_n)*100,1)) %>%
  ungroup() %>%
  left_join(wilcox_stats) %>%
  mutate(result = if_else(time == "After" & cut == "Very Good", label, ""))
#> Joining, by = "color"

plot_data %>%
  ggplot() +
  aes(x = cut, y = percent, fill = cut) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = result, y = 30), parse = TRUE) +
  facet_grid(color ~ time)

reprex 包(v0.3.0)于 2020 年 4 月 26 日创建

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何通过ggplot2中的facet_wrap排序多个图

如何在ggplot2中使用facet_grid制作甜甜圈图?

通过表达式生成时如何在ggplot2中对齐标题和字幕

使用plotmath在ggplot2中上标

ggplot2 facet_wrap与数学表达式

在ggplot2中,如何在facet_wrap中按组添加文本?

使用R中的facet_grid的ggplot2 boxplot中的表达式分别更改x标签

r ggplot动态使用plotmath表达式

ggplot2中的两个网格的数学表达式在带有facet_grid的两行中标记

如何在ggplot2中按组为facet_grid着色?

控制facet_wrap ggplot2中的x标签

在ggplot2字幕的plotmath表达式中包括条件元素

如何在ggplot2 :: label_parsed的plotmath中包含美元符号

ggplot2 facet_grid:如何修复geom_col中列之间的不同间距

R plotmath表达式以显示ggplot中的值范围

在保留其大小的同时将表达式更改为ggplot2中的粗体

使用ggplot2将方面标签中的新行和plotmath合并

正则表达式捕获:将整个匹配项包含在捕获组中

将plotmath字符串替换为表达式

如何将正则表达式包含在字符串数组中?

在ggplot2和facet_wrap中具有表达式的as_labeller

ggplot2中的facet_grid错误

如何将正则表达式包含在词汇无赖结构中?

ggplot2中具有数学运算符的Plotmath表达式

Bash:将格式化日期包含在重命名正则表达式中

GGplot2:从面板中移除部件 (facet_wrap)

如何在要在 ggplot2 图形中使用的表达式()中包含带有保存文本的对象?

如何在 ggplot facet 标签中使用 plotmath 符号和换行符

如何在 R ggplot2 中的 geom_smooth (facet_wrap) 中传递多个公式?