3

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:

enter image description here 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]
Community
  • 1
  • 1
Tomas
  • 57,621
  • 49
  • 238
  • 373

1 Answers1

3

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))

enter image description here

Arun
  • 116,683
  • 26
  • 284
  • 387
  • Thanks Arun! But please how to remove the awful ggplot grid? :) BTW very elegant use of datatables! Didn't know they are so elegant! – Tomas Aug 20 '13 at 12:53
  • better :) and if I want to get rid of it completely (so that it doesn't look like it is from ggplot :-)) – Tomas Aug 20 '13 at 12:56
  • BTW the label placement by `x="river", y=2.5` - isn't there something more flexible / independent of the actual coordinates? – Tomas Aug 20 '13 at 12:57
  • not sure what you mean by "doesn't look like it's from ggplot". I've removed all default colours from the plot, if that's what you mean. `geom_text` requires `x` and `y` aesthetic. You can either create it yourself directly from your data in `pval` or you can provide it yourself. You can just add two columns `x` and `y` in `pval` and generate whatever values you want and give `x=x, y=y` in the plot. – Arun Aug 20 '13 at 13:04
  • Arun, I only meant to remove the grid *completely*. As for the flexible text placement - there must be something in ggplot! Even in the base package for example, you can place legend and specify the position e.g. like "topright"! – Tomas Aug 20 '13 at 13:12
  • About the grid, please check edit. You can place the *legend* at different positions as you mention. But this is a *text geom*, *not a legend*. Have a look [here](http://docs.ggplot2.org/0.9.3.1/geom_text.html). – Arun Aug 20 '13 at 13:46
  • Arun, I didn't meant the bounding box (that was perfectly fine), I just meant the **[grid](https://www.google.cz/search?q=graph+grid&safe=off&source=lnms&tbm=isch&sa=X&biw=1213&bih=700)** inside of each panel. – Tomas Aug 20 '13 at 14:24
  • 2
    # Avoid ugly ggplot shading: theme_set(theme_bw()+theme(panel.margin=grid::unit(0,"lines"))) – Dieter Menne Aug 20 '13 at 14:33
  • Thanks @DieterMenne, I've added your suggestion. However, wrapping with `theme_set` doesn't seem to work for me. – Arun Aug 20 '13 at 14:38
  • 1
    You can use the them_set as written, but it also can be set globally to remove the ggplot-hyped-background overall. – Dieter Menne Aug 20 '13 at 14:56