<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/elegant-correlation-table-using-xtable-r-package/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.sthda.com/english/wiki/elegant-correlation-table-using-xtable-r-package"> Easy Guides</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p></p>
<p>  </p>
<div>
<p> <a rel="nofollow" href="http://www.sthda.com/english/wiki/correlation-matrix" target="_blank">Correlation matrix analysis</a> is an important method to find <strong>dependence</strong> between variables. Computing <strong>correlation matrix</strong> and drawing <strong>correlogram</strong> is explained <a rel="nofollow" href="http://www.sthda.com/english/wiki/correlation-matrix" target="_blank">here</a>. The aim of this article is to show you how to get the <strong>lower and the upper triangular part of a correlation matrix</strong>. We will also use the <strong>xtable R package</strong> to display a nice <strong>correlation table</strong> in html or latex formats.</p>
<p> <img alt="Elegant correlation table using xtable R package" src="https://i1.wp.com/www.sthda.com/english/sthda/RDoc/images/correlation-matrix-visualization-xtable.png?w=456" data-recalc-dims="1"></p>
<div>
<p><i></i> Note that online software is also available <a rel="nofollow" href="http://www.sthda.com/english/rsthda/correlation-matrix.php" target="_blank">here</a> to compute <strong>correlation matrix</strong> and to plot a <strong>correlogram</strong> without any installation.</p>
</div>
<p><strong>Contents:</strong></p>
<div>
<ul>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/elegant-correlation-table-using-xtable-r-package#correlation-matrix-analysis" target="_blank">Correlation matrix analysis</a></li>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/elegant-correlation-table-using-xtable-r-package#lower-and-upper-triangular-part-of-a-correlation-matrix" target="_blank">Lower and upper triangular part of a correlation matrix</a></li>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/elegant-correlation-table-using-xtable-r-package#use-xtable-r-package-to-display-nice-correlation-table-in-html-format" target="_blank">Use xtable R package to display nice correlation table in html format</a></li>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/elegant-correlation-table-using-xtable-r-package#combine-matrix-of-correlation-coefficients-and-significance-levels" target="_blank">Combine matrix of correlation coefficients and significance levels</a></li>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/elegant-correlation-table-using-xtable-r-package#conclusions" target="_blank">Conclusions</a></li>
</ul>
</div>
<div>
<h2>Correlation matrix analysis</h2>
<p>The following R code computes a <strong>correlation matrix</strong> using mtcars data. <a rel="nofollow" href="http://www.sthda.com/english/wiki/correlation-matrix" target="_blank">Click here</a> to read more.</p>
<pre><code>mcor&lt;-round(cor(mtcars),2)
mcor</code></pre>
<pre><code>       mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
mpg   1.00 -0.85 -0.85 -0.78  0.68 -0.87  0.42  0.66  0.60  0.48 -0.55
cyl  -0.85  1.00  0.90  0.83 -0.70  0.78 -0.59 -0.81 -0.52 -0.49  0.53
disp -0.85  0.90  1.00  0.79 -0.71  0.89 -0.43 -0.71 -0.59 -0.56  0.39
hp   -0.78  0.83  0.79  1.00 -0.45  0.66 -0.71 -0.72 -0.24 -0.13  0.75
drat  0.68 -0.70 -0.71 -0.45  1.00 -0.71  0.09  0.44  0.71  0.70 -0.09
wt   -0.87  0.78  0.89  0.66 -0.71  1.00 -0.17 -0.55 -0.69 -0.58  0.43
qsec  0.42 -0.59 -0.43 -0.71  0.09 -0.17  1.00  0.74 -0.23 -0.21 -0.66
vs    0.66 -0.81 -0.71 -0.72  0.44 -0.55  0.74  1.00  0.17  0.21 -0.57
am    0.60 -0.52 -0.59 -0.24  0.71 -0.69 -0.23  0.17  1.00  0.79  0.06
gear  0.48 -0.49 -0.56 -0.13  0.70 -0.58 -0.21  0.21  0.79  1.00  0.27
carb -0.55  0.53  0.39  0.75 -0.09  0.43 -0.66 -0.57  0.06  0.27  1.00</code></pre>
<p>The result is a table of <strong>correlation coefficients</strong> between all possible pairs of variables.</p>
</div>
<div>
<h2>Lower and upper triangular part of a correlation matrix</h2>
<p>To get the lower or the upper part of a <strong>correlation matrix</strong>, the R function <strong>lower.tri()</strong> or <strong>upper.tri()</strong> can be used. The formats of the functions are :</p>
<pre><code>lower.tri(x, diag = FALSE)
upper.tri(x, diag = FALSE)</code></pre>
<p><span> - <strong>x</strong> : is the <strong>correlation matrix</strong> - <strong>diag</strong> : if TRUE the diagonal are not included in the result. </span></p>
<p>The two functions above, return a matrix of logicals which has the same size of a the <strong>correlation matrix</strong>. The entries is TRUE in the lower or upper triangle :</p>
<pre><code>upper.tri(mcor)</code></pre>
<pre><code>       [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10] [,11]
 [1,] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
 [2,] FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
 [3,] FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
 [4,] FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
 [5,] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
 [6,] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
 [7,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE
 [8,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE
 [9,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE
[10,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
[11,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE</code></pre>
<pre><code># Hide upper triangle
upper&lt;-mcor
upper[upper.tri(mcor)]&lt;-&quot;&quot;
upper&lt;-as.data.frame(upper)
upper</code></pre>
<pre><code>       mpg   cyl  disp    hp  drat    wt  qsec    vs   am gear carb
mpg      1                                                         
cyl  -0.85     1                                                   
disp -0.85   0.9     1                                             
hp   -0.78  0.83  0.79     1                                       
drat  0.68  -0.7 -0.71 -0.45     1                                 
wt   -0.87  0.78  0.89  0.66 -0.71     1                           
qsec  0.42 -0.59 -0.43 -0.71  0.09 -0.17     1                     
vs    0.66 -0.81 -0.71 -0.72  0.44 -0.55  0.74     1               
am     0.6 -0.52 -0.59 -0.24  0.71 -0.69 -0.23  0.17    1          
gear  0.48 -0.49 -0.56 -0.13   0.7 -0.58 -0.21  0.21 0.79    1     
carb -0.55  0.53  0.39  0.75 -0.09  0.43 -0.66 -0.57 0.06 0.27    1</code></pre>
<pre><code>#Hide lower triangle
lower&lt;-mcor
lower[lower.tri(mcor, diag=TRUE)]&lt;-&quot;&quot;
lower&lt;-as.data.frame(lower)
lower</code></pre>
<pre><code>     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
mpg      -0.85 -0.85 -0.78  0.68 -0.87  0.42  0.66   0.6  0.48 -0.55
cyl              0.9  0.83  -0.7  0.78 -0.59 -0.81 -0.52 -0.49  0.53
disp                  0.79 -0.71  0.89 -0.43 -0.71 -0.59 -0.56  0.39
hp                         -0.45  0.66 -0.71 -0.72 -0.24 -0.13  0.75
drat                             -0.71  0.09  0.44  0.71   0.7 -0.09
wt                                     -0.17 -0.55 -0.69 -0.58  0.43
qsec                                          0.74 -0.23 -0.21 -0.66
vs                                                  0.17  0.21 -0.57
am                                                        0.79  0.06
gear                                                            0.27
carb                                                                </code></pre>
</div>
<div>
<h2>Use xtable R package to display nice correlation table in html format</h2>
<pre><code>library(xtable)
print(xtable(upper), type=&quot;html&quot;)</code></pre>
<p><br>
</p>
<table border="1">
<tr>
<th>
</th>
<th>
mpg
</th>
<th>
cyl
</th>
<th>
disp
</th>
<th>
hp
</th>
<th>
drat
</th>
<th>
wt
</th>
<th>
qsec
</th>
<th>
vs
</th>
<th>
am
</th>
<th>
gear
</th>
<th>
carb
</th>
</tr>
<tr>
<td align="right">
mpg
</td>
<td>
1
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
cyl
</td>
<td>
-0.85
</td>
<td>
1
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
disp
</td>
<td>
-0.85
</td>
<td>
0.9
</td>
<td>
1
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
hp
</td>
<td>
-0.78
</td>
<td>
0.83
</td>
<td>
0.79
</td>
<td>
1
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
drat
</td>
<td>
0.68
</td>
<td>
-0.7
</td>
<td>
-0.71
</td>
<td>
-0.45
</td>
<td>
1
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
wt
</td>
<td>
-0.87
</td>
<td>
0.78
</td>
<td>
0.89
</td>
<td>
0.66
</td>
<td>
-0.71
</td>
<td>
1
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
qsec
</td>
<td>
0.42
</td>
<td>
-0.59
</td>
<td>
-0.43
</td>
<td>
-0.71
</td>
<td>
0.09
</td>
<td>
-0.17
</td>
<td>
1
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
vs
</td>
<td>
0.66
</td>
<td>
-0.81
</td>
<td>
-0.71
</td>
<td>
-0.72
</td>
<td>
0.44
</td>
<td>
-0.55
</td>
<td>
0.74
</td>
<td>
1
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
am
</td>
<td>
0.6
</td>
<td>
-0.52
</td>
<td>
-0.59
</td>
<td>
-0.24
</td>
<td>
0.71
</td>
<td>
-0.69
</td>
<td>
-0.23
</td>
<td>
0.17
</td>
<td>
1
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
gear
</td>
<td>
0.48
</td>
<td>
-0.49
</td>
<td>
-0.56
</td>
<td>
-0.13
</td>
<td>
0.7
</td>
<td>
-0.58
</td>
<td>
-0.21
</td>
<td>
0.21
</td>
<td>
0.79
</td>
<td>
1
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
carb
</td>
<td>
-0.55
</td>
<td>
0.53
</td>
<td>
0.39
</td>
<td>
0.75
</td>
<td>
-0.09
</td>
<td>
0.43
</td>
<td>
-0.66
</td>
<td>
-0.57
</td>
<td>
0.06
</td>
<td>
0.27
</td>
<td>
1
</td>
</tr>
</table>
</div>
<div>
<h2>Combine matrix of correlation coefficients and significance levels</h2>
<p>Custom function <strong>corstars()</strong> is used to combine the <strong>correlation coefficients</strong> and the level of <strong>significance</strong>. The R code of the function is provided at the end of this article. It requires 2 packages :</p>
<ul>
<li>The <strong>Hmisc R package</strong> to compute the <a rel="nofollow" href="http://www.sthda.com/english/wiki/correlation-matrix" target="_blank"><strong>matrix of correlation coefficients</strong></a> and the corresponding <strong>p-values</strong>.
</li>
<li>The <strong>xtable R package</strong> for displaying in HTML or Latex format.</li>
</ul>
<div>
<p>
Before continuing with the following exercises, you should first copy and paste the source code the function <strong>corstars</strong>(), which you can find at the bottom of this article.
</p>
</div>
<pre><code>corstars(mtcars[,1:7], result=&quot;html&quot;)</code></pre>
<p><br>
</p>
<table border="1">
<tr>
<th>
</th>
<th>
mpg
</th>
<th>
cyl
</th>
<th>
disp
</th>
<th>
hp
</th>
<th>
drat
</th>
<th>
wt
</th>
</tr>
<tr>
<td align="right">
mpg
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
cyl
</td>
<td>
-0.85****
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
disp
</td>
<td>
-0.85****
</td>
<td>
0.90****
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
hp
</td>
<td>
-0.78****
</td>
<td>
0.83****
</td>
<td>
0.79****
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
drat
</td>
<td>
0.68****
</td>
<td>
-0.70****
</td>
<td>
-0.71****
</td>
<td>
-0.45**
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
wt
</td>
<td>
-0.87****
</td>
<td>
0.78****
</td>
<td>
0.89****
</td>
<td>
0.66****
</td>
<td>
-0.71****
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
qsec
</td>
<td>
0.42*
</td>
<td>
-0.59***
</td>
<td>
-0.43*
</td>
<td>
-0.71****
</td>
<td>
0.09
</td>
<td>
-0.17
</td>
</tr>
</table>
<p>p &lt; .0001 ‘****’; p &lt; .001 ‘***’, p &lt; .01 ‘**’, p &lt; .05 ‘*’</p>
<p><strong>The code of corstars function</strong> (The code is adapted from the one posted on this <a rel="nofollow" href="https://stat.ethz.ch/pipermail/r-help/2008-March/156583.html" target="_blank">forum</a> and on this <a rel="nofollow" href="http://myowelt.blogspot.fr/2008/04/beautiful-correlation-tables-in-r.html" target="_blank">blog</a> ):</p>
<pre><code># x is a matrix containing the data
# method : correlation method. &quot;pearson&quot;&quot; or &quot;spearman&quot;&quot; is supported
# removeTriangle : remove upper or lower triangle
# results :  if &quot;html&quot; or &quot;latex&quot;
  # the results will be displayed in html or latex format
corstars &lt;-function(x, method=c(&quot;pearson&quot;, &quot;spearman&quot;), removeTriangle=c(&quot;upper&quot;, &quot;lower&quot;),
                     result=c(&quot;none&quot;, &quot;html&quot;, &quot;latex&quot;)){
    #Compute correlation matrix
    require(Hmisc)
    x &lt;- as.matrix(x)
    correlation_matrix&lt;-rcorr(x, type=method[1])
    R &lt;- correlation_matrix$r # Matrix of correlation coeficients
    p &lt;- correlation_matrix$P # Matrix of p-value 
    
    ## Define notions for significance levels; spacing is important.
    mystars &lt;- ifelse(p &lt; .0001, &quot;****&quot;, ifelse(p &lt; .001, &quot;*** &quot;, ifelse(p &lt; .01, &quot;**  &quot;, ifelse(p &lt; .05, &quot;*   &quot;, &quot;    &quot;))))
    
    ## trunctuate the correlation matrix to two decimal
    R &lt;- format(round(cbind(rep(-1.11, ncol(x)), R), 2))[,-1]
    
    ## build a new matrix that includes the correlations with their apropriate stars
    Rnew &lt;- matrix(paste(R, mystars, sep=&quot;&quot;), ncol=ncol(x))
    diag(Rnew) &lt;- paste(diag(R), &quot; &quot;, sep=&quot;&quot;)
    rownames(Rnew) &lt;- colnames(x)
    colnames(Rnew) &lt;- paste(colnames(x), &quot;&quot;, sep=&quot;&quot;)
    
    ## remove upper triangle of correlation matrix
    if(removeTriangle[1]==&quot;upper&quot;){
      Rnew &lt;- as.matrix(Rnew)
      Rnew[upper.tri(Rnew, diag = TRUE)] &lt;- &quot;&quot;
      Rnew &lt;- as.data.frame(Rnew)
    }
    
    ## remove lower triangle of correlation matrix
    else if(removeTriangle[1]==&quot;lower&quot;){
      Rnew &lt;- as.matrix(Rnew)
      Rnew[lower.tri(Rnew, diag = TRUE)] &lt;- &quot;&quot;
      Rnew &lt;- as.data.frame(Rnew)
    }
    
    ## remove last column and return the correlation matrix
    Rnew &lt;- cbind(Rnew[1:length(Rnew)-1])
    if (result[1]==&quot;none&quot;) return(Rnew)
    else{
      if(result[1]==&quot;html&quot;) print(xtable(Rnew), type=&quot;html&quot;)
      else print(xtable(Rnew), type=&quot;latex&quot;) 
    }
} </code></pre>
</div>
<div>
<h2>Conclusions</h2>
<div>
<ul>
<li>Use <strong>cor()</strong> function to compute <strong>correlation matrix</strong>.</li>
<li>Use <strong>lower.tri()</strong> and <strong>upper.tri()</strong> functions to get the lower or upper part of the <strong>correlation matrix</strong></li>
<li>Use <strong>xtable</strong> R function to display a nice correlation matrix in latex or html format.</li>
</ul>
</div>
</div>
<div>
<h2>Infos</h2>
<pre><code>This analysis was performed using R (ver. 3.3.2).</code></pre>
</div>
<p></p>

</div>
<p><br>
</p>
<p></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="http://www.sthda.com/english/wiki/elegant-correlation-table-using-xtable-r-package"> Easy Guides</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/54qNjCX1neY" height="1"></a></div>