{"id":1998,"date":"2017-07-14T18:16:35","date_gmt":"2017-07-14T17:16:35","guid":{"rendered":"http:\/\/pcool.dyndns.org:8080\/statsbook\/?page_id=1998"},"modified":"2025-07-01T11:01:44","modified_gmt":"2025-07-01T10:01:44","slug":"correlation-plot","status":"publish","type":"page","link":"https:\/\/pcool.dyndns.org\/index.php\/correlation-plot\/","title":{"rendered":"Correlation Plot"},"content":{"rendered":"\n<p>A <a href=\"https:\/\/pcool.dyndns.org\/index.php\/correlation-coefficient\/\" data-type=\"page\" data-id=\"823\">correlation<\/a> plot can be really useful to show the correlation between different variables. To create a correlation plot, use the\u00a0corrplot package<sup class='sup-ref-note' id='note-zotero-ref-p1998-r1-o1'><a class='sup-ref-note' href='#zotero-ref-p1998-r1'>1<\/a><\/sup>. This package creates correlation plots from a correlation matrix.<\/p>\n\n\n\n<p>Base R contains a data frame &#8216;mtcars&#8217; with information about different cars. To show the first six observations <span style=\"color: #000000;\">(head) of the data frame and obtain further information about the variables in the data frame:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#f30202\" class=\"has-inline-color\">head(mtcars)\n<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0);color:#1002f2\" class=\"has-inline-color\">                   mpg cyl disp  hp drat    wt  qsec vs am gear carb\nMazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4\nMazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4\nDatsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1\nHornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1\nHornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2\nValiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0);color:#f30202\" class=\"has-inline-color\">\nstr(mtcars)\n<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0);color:#1802f2\" class=\"has-inline-color\">'data.frame':\t32 obs. of  11 variables:\n $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...\n $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...\n $ disp: num  160 160 108 258 360 ...\n $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...\n $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...\n $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...\n $ qsec: num  16.5 17 18.6 19.4 17 ...\n $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...\n $ am  : num  1 1 1 0 0 0 0 0 0 0 ...\n $ gear: num  4 4 4 3 3 3 3 4 4 4 ...\n $ carb: num  4 4 1 1 2 1 4 2 2 4 ...<\/mark><\/em><\/code><\/pre>\n\n\n\n<p>First, create a correlation matrix that contains the correlation coefficients between the different variables:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#f60202\" class=\"has-inline-color\">corr_matrix &lt;- cor(mtcars)\ncorr_matrix\n<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0216f5\" class=\"has-inline-color\">            mpg        cyl       disp         hp        drat         wt        qsec         vs          am       gear        carb\nmpg   1.0000000 -0.8521620 -0.8475514 -0.7761684  0.68117191 -0.8676594  0.41868403  0.6640389  0.59983243  0.4802848 -0.55092507\ncyl  -0.8521620  1.0000000  0.9020329  0.8324475 -0.69993811  0.7824958 -0.59124207 -0.8108118 -0.52260705 -0.4926866  0.52698829\ndisp -0.8475514  0.9020329  1.0000000  0.7909486 -0.71021393  0.8879799 -0.43369788 -0.7104159 -0.59122704 -0.5555692  0.39497686\nhp   -0.7761684  0.8324475  0.7909486  1.0000000 -0.44875912  0.6587479 -0.70822339 -0.7230967 -0.24320426 -0.1257043  0.74981247\ndrat  0.6811719 -0.6999381 -0.7102139 -0.4487591  1.00000000 -0.7124406  0.09120476  0.4402785  0.71271113  0.6996101 -0.09078980\nwt   -0.8676594  0.7824958  0.8879799  0.6587479 -0.71244065  1.0000000 -0.17471588 -0.5549157 -0.69249526 -0.5832870  0.42760594\nqsec  0.4186840 -0.5912421 -0.4336979 -0.7082234  0.09120476 -0.1747159  1.00000000  0.7445354 -0.22986086 -0.2126822 -0.65624923\nvs    0.6640389 -0.8108118 -0.7104159 -0.7230967  0.44027846 -0.5549157  0.74453544  1.0000000  0.16834512  0.2060233 -0.56960714\nam    0.5998324 -0.5226070 -0.5912270 -0.2432043  0.71271113 -0.6924953 -0.22986086  0.1683451  1.00000000  0.7940588  0.05753435\ngear  0.4802848 -0.4926866 -0.5555692 -0.1257043  0.69961013 -0.5832870 -0.21268223  0.2060233  0.79405876  1.0000000  0.27407284\ncarb -0.5509251  0.5269883  0.3949769  0.7498125 -0.09078980  0.4276059 -0.65624923 -0.5696071  0.05753435  0.2740728  1.00000000<\/mark><\/em><\/code><\/pre>\n\n\n\n<p>Such a matrix is clearly difficult to interpret and a plot is much more illustrative:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\"><em>library(corrplot)<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>corrplot(corr_matrix)<\/em><\/span><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"998\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/CorrPlot1-1024x998.png\" alt=\"\" class=\"wp-image-3027\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/CorrPlot1-1024x998.png 1024w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/CorrPlot1-300x292.png 300w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/CorrPlot1-768x748.png 768w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/CorrPlot1.png 1408w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>It is also possible to customise the plots; combine two types (mix number and colour) and cluster hierarchically:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><span style=\"color: #ff0000;\">corrplot.mixed(cor(mtcars), lower = 'number', upper = 'color', order = 'hclust')<\/span><\/em><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"983\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/CorrPlot2-1024x983.png\" alt=\"\" class=\"wp-image-3032\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/CorrPlot2-1024x983.png 1024w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/CorrPlot2-300x288.png 300w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/CorrPlot2-768x738.png 768w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/CorrPlot2.png 1412w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>For further options; refer to the manual ??corrplot<\/p>\n\n\n\n<p>Correlations can also be shown in a <a href=\"https:\/\/pcool.dyndns.org\/index.php\/hierarchical-edge-bundling-plot\/\" data-type=\"page\" data-id=\"1996\">hierarchical edge bundling\u00a0plot<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A correlation plot can be really useful to show the correlation between different variables. To create a correlation plot, use the\u00a0corrplot package. This package creates correlation plots from a correlation matrix. Base R contains a data frame &#8216;mtcars&#8217; with information about different cars. To show the first six observations (head) of the data frame and [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"inline_featured_image":false,"footnotes":""},"class_list":["post-1998","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/1998","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/comments?post=1998"}],"version-history":[{"count":2,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/1998\/revisions"}],"predecessor-version":[{"id":4698,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/1998\/revisions\/4698"}],"wp:attachment":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/media?parent=1998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}