**R on Abhijit Dasgupta**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I was recently asked to do a panel of grouped boxplots of a continuous variable,

with each panel representing a categorical grouping variable. This seems easy enough with ggplot2 and the `facet_wrap`

function, but then my collaborator wanted p-values on the graphs! This post is my approach to the problem.

First of all, one caveat. I’m a huge fan of Hadley Wickham’s tidyverse and so most of my code will reflect this ethos, including packages and pipes. There are, of course, other approaches to doing this.

```
library(broom)
library(ggplot2)
library(tidyverse)
```

`## ── Attaching packages ────────────────────────── tidyverse 1.2.1 ──`

```
## ✔ tibble 1.4.2 ✔ purrr 0.2.4
## ✔ tidyr 0.8.0 ✔ dplyr 0.7.4
## ✔ readr 1.1.1 ✔ stringr 1.3.0
## ✔ tibble 1.4.2 ✔ forcats 0.3.0
```

```
## ── Conflicts ───────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
```

I will use the `warpbreaks`

dataset as an example. This data looks at the number of breaks in yarn, for two types of yarn (A and B) and three loom tensions (L, M, H)

```
data(warpbreaks)
glimpse(warpbreaks)
```

```
## Observations: 54
## Variables: 3
## $ breaks
``` 26, 30, 54, 25, 70, 52, 51, 26, 67, 18, 21, 29, 17, 12...
## $ wool A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, ...
## $ tension L, L, L, L, L, L, L, L, L, M, M, M, M, M, M, M, M, M, ...

### A single panel

Let’s first start with creating a grouped boxplot of breaks by wool type. This is pretty straightforward in ggplot2.

```
plt1 <- ggplot(warpbreaks, aes(x=wool, y=breaks))+
geom_boxplot()
plt1
```

Adding the p-value to this is also rather simple. We will perform a two-sample t.test and report the p-value on the plot

```
tst = t.test(breaks ~ wool, data=warpbreaks)
pval = tidy(tst)$p.value # from broom
plt1 + geom_text(aes(x=2, y = 60, # Where do we put the annotation
label=paste('P-value:', format.pval(pval, digits=2))))
```

### Multiple panels

I’ll now show how to do multiple panels. First, we have to transform the data into long format so that ggplot2 can leverage it.

`warp2 <- gather(warpbreaks, variable, value, -breaks)`

```
## Warning: attributes are not identical across measure variables;
## they will be dropped
```

`head(warp2)`

```
## breaks variable value
## 1 26 wool A
## 2 30 wool A
## 3 54 wool A
## 4 25 wool A
## 5 70 wool A
## 6 52 wool A
```

Now we can plot this using ggplot2

`ggplot(warp2, aes(x = value, y = breaks))+geom_boxplot()+facet_wrap(~variable, nrow=1)`

Ooops!! When we used `gather`

to melt the data frame and create one variable for the two categorical variables, with the levels all shared in the `variable`

column. So the plot shows all the possible categories. We can easily fix this.

`plt2 <- ggplot(warp2, aes(x=value, y=breaks))+geom_boxplot()+facet_wrap(~variable, nrow=1, scales='free_x')`

Next, we want to put a p-value on each plot. The trick is to create a new data frame for the p-values, with one row per level of the facetting variable. In our example, wool has two levels while tension has three, so to create one p-value per plot I’ll run ANOVA models and provide the p-value of the model effect, i.e., is there any difference in the average number of breaks by levels of each predictor. I’ll use the template Wickham suggests about running multiple models here.

```
pvalues <- warp2 %>%
nest(-variable) %>%
mutate(mods = map(data, ~anova(lm(breaks ~ value, data = .))),
pval = map_dbl(mods, ~tidy(.)$p.value[1])) %>%
select(variable, pval)
pvalues
```

```
## # A tibble: 2 x 2
## variable pval
##
```
## 1 wool 0.108
## 2 tension 0.00175

I’ll also include in this data frame (or `tibble`

as the tidyverse calls it) the location of the annotation on each graph, using the same variable names as the graph

```
pvalues <- pvalues %>%
mutate(value = c(1.5,2.5), breaks = 60)
pvalues
```

```
## # A tibble: 2 x 4
## variable pval value breaks
##
```
## 1 wool 0.108 1.5 60
## 2 tension 0.00175 2.5 60

Now, let’s plot these onto the facetted plot

```
plt2_annot <- plt2 +
geom_text(data=pvalues, aes(x=value, y=breaks,
label = paste('P-value:',
format.pval(pval, digits=1))))
plt2_annot
```

Obviously, if we wanted to make this graph publication quality, we’d need to modify the labels and perhaps the background, but this example gets you to the point where you can put p-values onto facetted plots. In fact, there’s nothing special about p-values here; you could add any facet-specific annotation onto your facetted plots using the same template.

**leave a comment**for the author, please follow the link and comment on their blog:

**R on Abhijit Dasgupta**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.