user1836894 - 4 months ago 59

R Question

I would like to plot each column of a matrix as a boxplot and then label the outliers in each boxplot as the row name they belong to in the matrix. To use an example:

`vv=matrix(c(1,2,3,4,8,15,30),nrow=7,ncol=4,byrow=F)`

rownames(vv)=c("one","two","three","four","five","six","seven")

boxplot(vv)

I would like to label the outlier in each plot (in this case 30) as the row name it belongs to, so in this case 30 belongs to row 7. Is there an easy way to do this? I have seen similar questions to this asked but none seemed to have worked the way I want it to.

Answer

In the example given it's a bit boring because they are all the same row. but here is the code:

```
bxpdat <- boxplot(vv)
text(bxpdat$group, # the x locations
bxpdat$out, # the y values
rownames(vv)[which(vv == bxpdat$out, arr.ind=TRUE)[, 1]], # the labels
pos = 4)
```

This picks the rownames that have values equal to the "out" list (i.e., the outliers) in the result of `boxplot`

. Boxplot calls and returns the values from `boxplot.stats`

. Take a look at:

```
str(bxpdat)
```