如何在箱图中为每个组绘制其他统计信息?

编程入门 行业动态 更新时间:2024-10-19 11:38:31
本文介绍了如何在箱图中为每个组绘制其他统计信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我希望看到因素组合的箱形图,并且被告知要使用晶格.我试过了,看起来像这样:

I would like to see boxplots of combination of factors and I was told to use lattice for that. I tried it and it looks like this:

但是现在我还要向每个组添加方差分析统计数据.统计信息可能应在每个面板中显示p值(例如澳大利亚"下方的白色).如何做到这一点?请注意,我根本不坚持使用晶格...

But now I would like to also add an ANOVA statistics to each of the groups. Possibly the statistics should display the p-value in each panel (in the white below the e.g. "Australia"). How to do this in lattice? Note that I don't insist on lattice at all...

示例代码:

set.seed(123) n <- 300 country <- sample(c("Europe", "Africa", "Asia", "Australia"), n, replace = TRUE) type <- sample(c("city", "river", "village"), n, replace = TRUE) month <- sample(c("may", "june", "july"), n, replace = TRUE) x <- rnorm(n) df <- data.frame(x, country, type, month) bwplot(x ~ type|country+month, data = df, panel=function(...) { panel.abline(h=0, col="green") panel.bwplot(...) })

为其中一个组执行ANOVA并提取p值的代码是:

The code to perform ANOVA for one of the groups and to extract p-value is this:

model <- aov(x ~ type, data = df[df$country == 'Africa' & df$month == 'may',]) p_value <- summary(model)[[1]][["Pr(>F)"]][2]

推荐答案

这是使用ggplot2的一种方法.首先,我们可以分别计算每个月/国家/地区组合的p值(我使用data.table.您可以使用自己喜欢的任何一种方式).然后,添加geom_text并指定pvalue作为标签,并指定文本应在每个构面内的x和y坐标.

Here's one way using ggplot2. First we can compute the p-values separately for every month/country combination (I use data.table. you can use whichever way you're comfortable with). Then, we add geom_text and specify pvalue as the label and specify x and y coordinates where the text should be within each facet.

require(data.table) dt <- data.table(df) pval <- dt[, list(pvalue = paste0("pval = ", sprintf("%.3f", summary(aov(x ~ type))[[1]][["Pr(>F)"]][1]))), by=list(country, month)] ggplot(data = df, aes(x=type, y=x)) + geom_boxplot() + geom_text(data = pval, aes(label=pvalue, x="river", y=2.5)) + facet_grid(country ~ month) + theme_bw() + theme(panel.margin=grid::unit(0,"lines"), # thanks to @DieterMenne strip.background = element_rect(fill = NA), panel.grid.major = element_line(colour=NA), panel.grid.minor = element_line(colour=NA))

更多推荐

如何在箱图中为每个组绘制其他统计信息?

本文发布于:2023-10-27 11:08:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533194.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:统计信息   图中   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!