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:
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...
Example code:
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(...)
})
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]
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…