我希望看到因素组合的箱形图,并且被告知要使用晶格.我试过了,看起来像这样:
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))
更多推荐
如何在箱图中为每个组绘制其他统计信息?
发布评论