英文:
How to plot F statistics and p-value with ggplot
问题
在ggplot中,如何在图下方嵌入F统计量和p值信息,就像以下示例图中所示:
英文:
In ggplot, how to embedded F-statistics and p-value information below the plot like shown in the following plot
答案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))
创建于 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 <- 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))
<sup>Created on 2023-07-06 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论