如何使用 ggplot 绘制 F 统计量和 p 值

huangapple go评论168阅读模式
英文:

How to plot F statistics and p-value with ggplot

问题

在ggplot中,如何在图下方嵌入F统计量和p值信息,就像以下示例图中所示:如何使用 ggplot 绘制 F 统计量和 p 值

英文:

In ggplot, how to embedded F-statistics and p-value information below the plot like shown in the following plot
如何使用 ggplot 绘制 F 统计量和 p 值

答案1

得分: 3

看起来这个图表是使用 ggstatsplot 创建的,但据我所知,该包内部没有机制可以轻松地在图表下方插入一个表格。我怀疑这个表格是使用一个单独的包创建的,然后后来添加进去的。

如果你想在 ggplot 框架中重新创建整个图(除了显示 F 统计量 / 方差分析结果),是可以做到的,但会有一些麻烦。这里是一个完整的 reprex:

  1. library(ggplot2)
  2. library(ggstatsplot)
  3. library(patchwork)
  4. df <- data.frame(Recall = c(rep(8:10 * 10, c(2, 4, 10)),
  5. rep(5:10 * 10, c(4, 2, 2, 2, 2, 4))),
  6. x = rep(c("Expert-defined", "User-defined"), each = 16))
  7. p1 <- ggwithinstats(df, x, Recall, bf.message = FALSE, results.subtitle = FALSE,
  8. point.args = list(position = "identity"),
  9. centrality.path = FALSE, xlab = "")
  10. dat <- anova(lm(Recall ~ x, df))
  11. p2 <- data.frame(x = 1:5,
  12. lab = c("User-\nDefined", "Expert-\nDefined", dat[1, 2],
  13. round(dat[1, 4], 2), scales::pvalue(dat[1, 5]))) |>
  14. ggplot(aes(x, y = 1, label = lab)) +
  15. annotate("rect", xmin = 4.5, xmax = 5.5, ymin = 0.5, ymax = 1.5,
  16. fill = "gray") +
  17. geom_hline(yintercept = c(1, 2, 4) - 0.5) +
  18. geom_text() +
  19. annotate("text", x = c(3:5, 4), y = c(2, 2, 2, 3),
  20. label = c("Sum of Squares", "F statistic",
  21. "p value", "ANOVA")) +
  22. annotate("line", x = c(2.5, 5.5), y = 2.5) +
  23. theme_void()
  24. p1 / p2 + patchwork::plot_layout(heights = c(3, 1))

如何使用 ggplot 绘制 F 统计量和 p 值

创建于 2023-07-06,使用 reprex v2.0.2

英文:

It looks as though this plot was created with ggstatsplot, but to the best of my knowledge, there is no mechanism within that package to easily insert a table underneath the plot. I suspect the table was created with a separate package and added later.

If you wanted to recreate the whole thing in a ggplot framework (except displaying an F statistic / ANOVA result), it would be possible, but a little fiddly. Here's a full reprex:

  1. library(ggplot2)
  2. library(ggstatsplot)
  3. library(patchwork)
  4. df &lt;- data.frame(Recall = c(rep(8:10 * 10, c(2, 4, 10)),
  5. rep(5:10 * 10, c(4, 2, 2, 2, 2, 4))),
  6. x = rep(c(&quot;Expert-defined&quot;, &quot;User-defined&quot;), each = 16))
  7. p1 &lt;- ggwithinstats(df, x, Recall, bf.message = FALSE, results.subtitle = FALSE,
  8. point.args = list(position = &quot;identity&quot;),
  9. centrality.path = FALSE, xlab = &quot;&quot;)
  10. dat &lt;- anova(lm(Recall ~ x, df))
  11. p2 &lt;- data.frame(x = 1:5,
  12. lab = c(&quot;User-\nDefined&quot;, &quot;Expert-\nDefined&quot;, dat[1, 2],
  13. round(dat[1, 4], 2), scales::pvalue(dat[1, 5]))) |&gt;
  14. ggplot(aes(x, y = 1, label = lab)) +
  15. annotate(&quot;rect&quot;, xmin = 4.5, xmax = 5.5, ymin = 0.5, ymax = 1.5,
  16. fill = &quot;gray&quot;) +
  17. geom_hline(yintercept = c(1, 2, 4) - 0.5) +
  18. geom_text() +
  19. annotate(&quot;text&quot;, x = c(3:5, 4), y = c(2, 2, 2, 3),
  20. label = c(&quot;Sum of Squares&quot;, &quot;F statistic&quot;,
  21. &quot;p value&quot;, &quot;ANOVA&quot;)) +
  22. annotate(&quot;line&quot;, x = c(2.5, 5.5), y = 2.5) +
  23. theme_void()
  24. p1 / p2 + patchwork::plot_layout(heights = c(3, 1))

如何使用 ggplot 绘制 F 统计量和 p 值

<sup>Created on 2023-07-06 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年7月7日 03:34:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76632046.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定