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

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

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:

library(ggplot2)
library(ggstatsplot)
library(patchwork)

df <- data.frame(Recall = c(rep(8:10 * 10, c(2, 4, 10)),
                            rep(5:10 * 10, c(4, 2, 2, 2, 2, 4))),
                 x = rep(c("Expert-defined", "User-defined"), each = 16))

p1 <- ggwithinstats(df, x, Recall, bf.message = FALSE, results.subtitle = FALSE,
                   point.args = list(position = "identity"),
                   centrality.path = FALSE, xlab = "")

dat <- anova(lm(Recall ~ x, df))

p2 <- data.frame(x = 1:5, 
           lab = c("User-\nDefined", "Expert-\nDefined", dat[1, 2],
                   round(dat[1, 4], 2), scales::pvalue(dat[1, 5]))) |>
  ggplot(aes(x, y = 1, label = lab)) +
  annotate("rect", xmin = 4.5, xmax = 5.5, ymin = 0.5, ymax = 1.5,
           fill = "gray") +
  geom_hline(yintercept = c(1, 2, 4) - 0.5) +
  geom_text() +
  annotate("text", x = c(3:5, 4), y = c(2, 2, 2, 3), 
           label = c("Sum of Squares", "F statistic",
                     "p value", "ANOVA")) +
  annotate("line", x = c(2.5, 5.5), y = 2.5) +
  theme_void() 

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:

library(ggplot2)
library(ggstatsplot)
library(patchwork)

df &lt;- data.frame(Recall = c(rep(8:10 * 10, c(2, 4, 10)),
                            rep(5:10 * 10, c(4, 2, 2, 2, 2, 4))),
                 x = rep(c(&quot;Expert-defined&quot;, &quot;User-defined&quot;), each = 16))

p1 &lt;- ggwithinstats(df, x, Recall, bf.message = FALSE, results.subtitle = FALSE,
                   point.args = list(position = &quot;identity&quot;),
                   centrality.path = FALSE, xlab = &quot;&quot;)

dat &lt;- anova(lm(Recall ~ x, df))

p2 &lt;- data.frame(x = 1:5, 
           lab = c(&quot;User-\nDefined&quot;, &quot;Expert-\nDefined&quot;, dat[1, 2],
                   round(dat[1, 4], 2), scales::pvalue(dat[1, 5]))) |&gt;
  ggplot(aes(x, y = 1, label = lab)) +
  annotate(&quot;rect&quot;, xmin = 4.5, xmax = 5.5, ymin = 0.5, ymax = 1.5,
           fill = &quot;gray&quot;) +
  geom_hline(yintercept = c(1, 2, 4) - 0.5) +
  geom_text() +
  annotate(&quot;text&quot;, x = c(3:5, 4), y = c(2, 2, 2, 3), 
           label = c(&quot;Sum of Squares&quot;, &quot;F statistic&quot;,
                     &quot;p value&quot;, &quot;ANOVA&quot;)) +
  annotate(&quot;line&quot;, x = c(2.5, 5.5), y = 2.5) +
  theme_void() 

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:

确定