<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/perform-pairwise-wilcoxon-test-classify-groups-by-significance-and-plot-results/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://fabiomarroni.wordpress.com/2017/03/25/perform-pairwise-wilcoxon-test-classify-groups-by-significance-and-plot-results/"> R – Fabio Marroni's Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>This post is the result of work performed in collaboration with my colleague Eleonora Paparelli (who actually did most of the work!). We wanted to compare several distributions using Wilcoxon test and summarize results (i.e. indicate the comparisons showing significant differences).<br>
R base includes pairwise.wilcox.test to perform Wilcoxon rank sum test between all pairs of samples in a study.<br>
A common way to represent significance in pairwise comparisons is the use of letters. Samples sharing a letter are not different from each other. Samples not sharing letters are different.<br>
Library multcompView in R can take a square matrix of p-values and return letters for all samples.<br>
Sadly enough, pairwise.wilcox.test returns a triangular matrix, so I had to write a small function – named tri.to.squ – to take the output of pairwise.wilcox.test and convert it in a suitable input for the multcompLetters function of multcompView.<br>
Now we can easily plot the distributions as box plot and add the letters as text. </p>
<pre>
library(multcompView)

first.set&lt;-cbind(rnorm(100,mean=1.8),1)
second.set&lt;-cbind(rnorm(100,mean=0.9),2)
third.set&lt;-cbind(rnorm(100,mean=1),3)

full&lt;-rbind(first.set,second.set,third.set)

pp&lt;-pairwise.wilcox.test(full[,1], full[,2], p.adjust.method = &quot;none&quot;, paired = FALSE)
mymat&lt;-tri.to.squ(pp$p.value)
myletters&lt;-multcompLetters(mymat,compare=&quot;&lt;=&quot;,threshold=0.05,Letters=letters)

boxplot(full[,1] ~ full[,2],ylab=&quot;Something nice&quot;,names=c(&quot;Set 1&quot;,&quot;Set 2&quot;,&quot;Set 3&quot;),ylim=c(min(full[,1]),0.2+max(full[,1])))
text(c(1,2,3),0.1+max(full[,1]),c(myletters$Letters[1],myletters$Letters[2],myletters$Letters[3]))

tri.to.squ&lt;-function(x)
{
rn&lt;-row.names(x)
cn&lt;-colnames(x)
an&lt;-unique(c(cn,rn))
myval&lt;-x[!is.na(x)]
mymat&lt;-matrix(1,nrow=length(an),ncol=length(an),dimnames=list(an,an))
for(ext in 1:length(cn))
{
    for(int in 1:length(rn))
    {
    if(is.na(x[row.names(x)==rn[int],colnames(x)==cn[ext]])) next
    mymat[row.names(mymat)==rn[int],colnames(mymat)==cn[ext]]&lt;-x[row.names(x)==rn[int],colnames(x)==cn[ext]]
    mymat[row.names(mymat)==cn[ext],colnames(mymat)==rn[int]]&lt;-x[row.names(x)==rn[int],colnames(x)==cn[ext]]
    }

}
return(mymat)
}
</pre>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/fabiomarroni.wordpress.com/281/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/fabiomarroni.wordpress.com/281/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://fabiomarroni.wordpress.com/2017/03/25/perform-pairwise-wilcoxon-test-classify-groups-by-significance-and-plot-results/"> R – Fabio Marroni's Blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/trmBP_ZX7tI"></a></div>