{"id":121,"date":"2013-06-09T15:26:24","date_gmt":"2013-06-09T14:26:24","guid":{"rendered":"http:\/\/borisleroy.com\/en\/?p=121"},"modified":"2013-08-13T10:27:24","modified_gmt":"2013-08-13T09:27:24","slug":"correlation-plots-in-r","status":"publish","type":"post","link":"https:\/\/borisleroy.com\/en\/2013\/06\/09\/correlation-plots-in-r\/","title":{"rendered":"Correlation plots in R"},"content":{"rendered":"<p>I have just released the version <del>1.2<\/del> 1.2-1 of the Rarity package, and this new version introduces a new function to make what I called &#8220;correlation plots&#8221;.<\/p>\n<p>These correlation plots provide a synthetic and convenient representation of the correlation between 2 or more variables, allowing an easy analysis.<\/p>\n<p><a href=\"http:\/\/borisleroy.com\/wp-content\/uploads\/2013\/06\/Rplot.png\"><img loading=\"lazy\" decoding=\"async\" alt=\"Correlation plot crabs\" src=\"http:\/\/borisleroy.com\/wp-content\/uploads\/2013\/06\/Rplot.png\" width=\"550\" height=\"550\" \/><\/a><\/p>\n<p>The above figure is an example with data of crab sizes (from the package MASS).<\/p>\n<p>The plot is split in two:<\/p>\n<ul>\n<li>the lower left triangle shows the scatter plots of pairs of variables<\/li>\n<li>the upper right triangle shows the values of correlations between pairs of variables with the chosen method (on the example above: Pearson) and the associated degree of significativity<\/li>\n<\/ul>\n<p>The degree of significativity is as follows:<\/p>\n<p>p \u2264 0.001 : \u2018***\u2019<br \/>\np \u2264 0.01 : \u2018**\u2019<br \/>\np \u2264 0.05 : \u2018*\u2019<br \/>\np \u2264 0.1 : \u2018-\u2019<\/p>\n<p>The plot is read by crossing pairs of variables as if we were reading a contingency table: for example, the top left scatter plots shows RW as a function of FL, and, on its mirror on the upper triangle is the value of the Pearson correlation coefficient (0.91), with its significativity (p &lt; 0.001).<\/p>\n<p>To create this function I largely took inspiration from the <a href=\"http:\/\/www.pnas.org\/content\/suppl\/2009\/05\/21\/0810306106.DCSupplemental\/0810306106SI.pdf\">plot on page 3 of the Supporting Information of Kier et al. 2009.<\/a><\/p>\n<p>The use of the fonction is fairly simple. It requires a data.frame with variables in columns, the choice of the method and voil\u00e0. The methods are:<\/p>\n<ul>\n<li>Pearson: in that case, the values of variables are plotted on the scatter plots<\/li>\n<li>Spearman or Kendall: for these rank-based methods, the ranks of variables are plotted on the scatter plots<\/li>\n<\/ul>\n<p>Here are a couple of simple examples with 2 variables (although this function is really interesting for more than two variables!):<\/p>\n<blockquote>\n<pre>&gt; library(Rarity)\r\n&gt; data(spid.occ) # Example with the occurrences of spiders of \r\nWestern France\r\n&gt; corPlot(spid.occ, method = \"pearson\")\r\n<a href=\"http:\/\/borisleroy.com\/wp-content\/uploads\/2013\/06\/Rplot01.png\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" src=\"http:\/\/borisleroy.com\/wp-content\/uploads\/2013\/06\/Rplot01.png\" width=\"450\" height=\"400\" \/><\/a>\r\n&gt; corPlot(spid.occ, method = \"spearman\")<a href=\"http:\/\/borisleroy.com\/wp-content\/uploads\/2013\/06\/Rplot02.png\">\r\n<img loading=\"lazy\" decoding=\"async\" alt=\"Rplot02\" src=\"http:\/\/borisleroy.com\/wp-content\/uploads\/2013\/06\/Rplot02.png\" width=\"450\" height=\"400\" \/><\/a>\r\n# With Spearman variable ranks are plotted. This method is \r\nparticularly appropriate when studying congruency between indices<\/pre>\n<\/blockquote>\n<p>NA values in variables are correctly handled by the function. Several options to customise the plots are available: number of digits for correlation values, axis labels, NA handling, title, and usual graphical options to customise plot contents. Feel free to send me any suggestion you might have!<\/p>\n<blockquote>\n<pre>&gt; corPlot(spid.occ, method = \"pearson\", pch = 16, cex = .5, digits = 3, \r\nxlab = c(\"Regional occurrence\", \"West Palearctic occurrence\"), \r\nylab = c(\"Regional occurrence\", \"West Palearctic occurrence\"),\r\ncol = \"#94B62D\")<a href=\"http:\/\/borisleroy.com\/en\/wp-content\/uploads\/sites\/2\/2013\/06\/Rplot05.png\">\r\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-125\" alt=\"Rplot05\" src=\"http:\/\/borisleroy.com\/en\/wp-content\/uploads\/sites\/2\/2013\/06\/Rplot05.png\" width=\"450\" height=\"400\" srcset=\"https:\/\/borisleroy.com\/en\/wp-content\/uploads\/sites\/2\/2013\/06\/Rplot05.png 450w, https:\/\/borisleroy.com\/en\/wp-content\/uploads\/sites\/2\/2013\/06\/Rplot05-300x266.png 300w, https:\/\/borisleroy.com\/en\/wp-content\/uploads\/sites\/2\/2013\/06\/Rplot05-168x150.png 168w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/a><\/pre>\n<\/blockquote>\n<p>Many thanks to Ivailo Stoyanov for his suggestions to improve the function.<\/p>\n<p><del>&#8211; edit-<\/del>Corrected and up-to-date on the CRAN!<\/p>\n<p><del>A small bug has slipped through the version 1.2 of the package: when Pearson&#8217;s method is used, the axes will always start from 0. This bug has been fixed and should be sent as soon as possible on the CRAN!<\/del><\/p>\n<p><del>Until the update is posted on CRAN, here is the corrected version: <a href=\"http:\/\/borisleroy.com\/wp-content\/uploads\/2013\/06\/Rarity_1.2-1.zip\">Rarity_1.2-1 (zip)<\/a> or <a href=\"http:\/\/borisleroy.com\/wp-content\/uploads\/2013\/06\/Rarity_1.2-1.tar_.gz\">Rarity_1.2-1.tar.gz<\/a>\u00a0(To install: choose install from zip file for R or &#8220;Install from: package archive file&#8221; from Rstudio)<\/del><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have just released the version 1.2 1.2-1 of the Rarity package, and this new version introduces a new function to make what I called &#8220;correlation plots&#8221;. These correlation plots provide a synthetic and convenient representation of the correlation between 2 or more variables, allowing an easy analysis. The above figure is an example with data of crab sizes (from the package MASS). The plot is split in two: the.. <a href=\"https:\/\/borisleroy.com\/en\/2013\/06\/09\/correlation-plots-in-r\/\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[9,18,8,6],"class_list":["post-121","post","type-post","status-publish","format-standard","hentry","category-news","tag-cran","tag-r","tag-r-package","tag-rarity"],"featured_image_src":null,"featured_image_src_square":null,"author_info":{"display_name":"Boris","author_link":"https:\/\/borisleroy.com\/en\/author\/farewell\/"},"_links":{"self":[{"href":"https:\/\/borisleroy.com\/en\/wp-json\/wp\/v2\/posts\/121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/borisleroy.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/borisleroy.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/borisleroy.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/borisleroy.com\/en\/wp-json\/wp\/v2\/comments?post=121"}],"version-history":[{"count":10,"href":"https:\/\/borisleroy.com\/en\/wp-json\/wp\/v2\/posts\/121\/revisions"}],"predecessor-version":[{"id":144,"href":"https:\/\/borisleroy.com\/en\/wp-json\/wp\/v2\/posts\/121\/revisions\/144"}],"wp:attachment":[{"href":"https:\/\/borisleroy.com\/en\/wp-json\/wp\/v2\/media?parent=121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/borisleroy.com\/en\/wp-json\/wp\/v2\/categories?post=121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/borisleroy.com\/en\/wp-json\/wp\/v2\/tags?post=121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}