<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/r-correlation-tutorial/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.datacamp.com/community/blog/r-correlation-tutorial"> DataCamp Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p></p>
<p><img alt="" src="https://i1.wp.com/community.datacamp.com.s3.amazonaws.com/community/production/ckeditor_assets/pictures/438/content_top_post_image.png?w=456" data-recalc-dims="1"></p>
<p>In this tutorial, you explore a number of data visualization methods and their underlying statistics. Particularly with regard to identifying trends and relationships between variables in a data frame.</p>
<p>That’s right, you’ll focus on concepts such as correlation and regression! First, you’ll get introduced to correlation in R. Then, you’ll see how you can plot correlation matrices in R, using packages such as <code>ggplot2</code> and <code>GGally</code>. Lastly, you’ll see what types of correlations exist and how they matter for your further analysis.</p>
<p>If you’re interested in diving deeper into this topic, consider taking DataCamp’s <a target="_blank" rel="nofollow" href="https://www.datacamp.com/courses/correlation-and-regression">Correlation and Regression</a> course.</p>
<div>
<h2>Background</h2>
<p>In today’s tutorial, you’ll be working with a data set of movies acquired from <a target="_blank" rel="nofollow" href="https://www.kaggle.com/">Kaggle</a> to discover how you can better understand relationships among variables better.</p>
<p>I have lightly cultivated the data so that our analysis is an “apples-to-apples” one by ensuring that things like currency use the same units. Without that step, our statistical analysis of variables like <code>gross</code>, <code>budget</code>, and <code>profit</code> would be misleading. You can access the original data set <a target="_blank" rel="nofollow" href="https://www.kaggle.com/deepmatrix/imdb-5000-movie-dataset">here</a>.</p>
</div>
<div>
<h2>Importing The Data</h2>
<p>In order to access the <code>movies</code> data set and put it to use, you can use the <code>read.csv()</code> function to import your data into a data frame and store it in the variable with the stunningly original name <code>movies</code>!</p>
<pre>
<code>movies &lt;- read.csv(url(&quot;http://s3.amazonaws.com/dcwoods2717/movies.csv&quot;))</code></pre>
<p>That’s all it takes to get started!</p>
<div>
<h3>Basic Inspection of Your Data</h3>
<p>It’s a good idea, once a data frame has been imported, to get an idea about your data. First, check out the structure of the data that is being examined. Below you can see the results of using this super-simple, helpful function <code>str()</code>:</p>
<pre>
<code>str(movies)</code></pre>
<pre>
<code>## 'data.frame':    2961 obs. of  11 variables:
##  $ title              : Factor w/ 2907 levels &quot;10 Cloverfield Lane&quot;,..: 1560 2143 34 2687 1405 1896 2633 894 1604 665 ...
##  $ genre              : Factor w/ 17 levels &quot;Action&quot;,&quot;Adventure&quot;,..: 6 12 5 5 5 3 2 8 3 8 ...
##  $ director           : Factor w/ 1366 levels &quot;Aaron Schneider&quot;,..: 474 472 781 828 175 1355 1328 1328 968 747 ...
##  $ year               : int  1920 1929 1933 1935 1936 1937 1939 1939 1940 1946 ...
##  $ duration           : int  110 100 89 81 87 83 102 226 88 144 ...
##  $ gross              : int  3000000 2808000 2300000 3000000 163245 184925485 22202612 198655278 84300000 20400000 ...
##  $ budget             : int  100000 379000 439000 609000 1500000 2000000 2800000 3977000 2600000 8000000 ...
##  $ cast_facebook_likes: int  4 109 995 824 352 229 2509 1862 1178 2037 ...
##  $ votes              : int  5 4546 7921 13269 143086 133348 291875 215340 90360 6304 ...
##  $ reviews            : int  2 107 162 164 331 349 746 863 252 119 ...
##  $ rating             : num  4.8 6.3 7.7 7.8 8.6 7.7 8.1 8.2 7.5 6.9 ...</code></pre>
<p>In this particular data frame, you can see from the console that 2961 observations of 11 variables are present.</p>
<p>As a side note, even if each movie was only an hour long, you’d need to watch movies non-stop for over four months to see them all!</p>
<p>The console also lists each variable by name, the class of each variable, and a few instances of each variable. This gives us a pretty good idea of what is in the data frame, the understanding of which is crucial to our analytic endeavors.</p>
<p>Another great function to help us perform a quick, high-level overview of our data frame is <code>summary()</code>. Note the similarities and differences between the output produced by running <code>str()</code>.</p>
<pre>
<code>summary(movies)</code></pre>
<pre>
<code>##                          title            genre    
##  Home                       :   3   Comedy   :848  
##  A Nightmare on Elm Street  :   2   Action   :738  
##  Across the Universe        :   2   Drama    :498  
##  Alice in Wonderland        :   2   Adventure:288  
##  Aloha                      :   2   Crime    :202  
##  Around the World in 80 Days:   2   Biography:135  
##  (Other)                    :2948   (Other)  :252  
##               director         year         duration    
##  Steven Spielberg :  23   Min.   :1920   Min.   : 37.0  
##  Clint Eastwood   :  19   1st Qu.:1999   1st Qu.: 95.0  
##  Martin Scorsese  :  16   Median :2004   Median :106.0  
##  Tim Burton       :  16   Mean   :2003   Mean   :109.6  
##  Spike Lee        :  15   3rd Qu.:2010   3rd Qu.:119.0  
##  Steven Soderbergh:  15   Max.   :2016   Max.   :330.0  
##  (Other)          :2857                                 
##      gross               budget          cast_facebook_likes
##  Min.   :      703   Min.   :      218   Min.   :     0     
##  1st Qu.: 12276810   1st Qu.: 11000000   1st Qu.:  2241     
##  Median : 34703228   Median : 26000000   Median :  4604     
##  Mean   : 58090401   Mean   : 40619384   Mean   : 12394     
##  3rd Qu.: 75590286   3rd Qu.: 55000000   3rd Qu.: 16926     
##  Max.   :760505847   Max.   :300000000   Max.   :656730     
##                                                             
##      votes            reviews           rating     
##  Min.   :      5   Min.   :   2.0   Min.   :1.600  
##  1st Qu.:  19918   1st Qu.: 199.0   1st Qu.:5.800  
##  Median :  55749   Median : 364.0   Median :6.500  
##  Mean   : 109308   Mean   : 503.3   Mean   :6.389  
##  3rd Qu.: 133348   3rd Qu.: 631.0   3rd Qu.:7.100  
##  Max.   :1689764   Max.   :5312.0   Max.   :9.300  
## </code></pre>
<p>With a single command, you’ve had R return some key statistical information for each variable in our data frame. Now that you know what you’re working with, let’s dive in and explore the data some further!</p>
</div>
</div>
<div>
<h2>Feature Engineering: Calculating Profit</h2>
<p>In reviewing the variables available to you, it appears that some of the numeric variables can be manipulated to provide new insights into our data frame.</p>
<p>For instance, you’ve got <code>gross</code> and <code>budget</code> variables, so why not use a little subsetting to calculate the profit for each movie?</p>
<p>You can calculate profit by using the formula <code>profit = gross - budget</code>. Try it out in the DataCamp Light chunk below!</p>
<div data-height="300" data-datacamp-exercise="" data-encoded="true">eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6Im1vdmllcyA8LSByZWFkLmNzdih1cmwoXCJodHRwOi8vczMuYW1hem9uYXdzLmNvbS9kY3dvb2RzMjcxNy9tb3ZpZXMuY3N2XCIpKSIsInNhbXBsZSI6IiMgRmlsbCBpbiB0aGUgYXBwcm9wcmlhdGUgdmFyaWFibGUgbmFtZXMgdG8gc3Vic2V0IVxucHJvZml0IDwtIG1vdmllcyQuLi4uLiAtIG1vdmllcyQuLi4uLlxuXG4jIENoZWNrIHRoZSByZXN1bHRcbnN1bW1hcnkocHJvZml0KSIsInNvbHV0aW9uIjoiIyBGaWxsIGluIHRoZSBhcHByb3ByaWF0ZSB2YXJpYWJsZSBuYW1lcyB0byBzdWJzZXQhXG5wcm9maXQgPC0gbW92aWVzJGdyb3NzIC0gbW92aWVzJGJ1ZGdldFxuXG4jIENoZWNrIHRoZSByZXN1bHRcbnN1bW1hcnkocHJvZml0KSIsInNjdCI6InRlc3Rfb2JqZWN0KFwicHJvZml0XCIsIHVuZGVmaW5lZF9tc2c9XCJEaWQgeW91IGNvcnJlY3RseSBkZWZpbmUgdGhlIHZhcmlhYmxlIGBwcm9maXRgP1wiLCBpbmNvcnJlY3RfbXNnID0gXCJOb3QgcXVpdGUhXCIpXG50ZXN0X2Vycm9yKClcbnN1Y2Nlc3NfbXNnKFwiRXhjZWxsZW50IVwiKSJ9</div>
<p>Awesome!</p>
<p>You can see that there is quite a collection of money-makers and money-pits in our data frame.</p>
<p>The <code>profit</code> of our movies can probably be used for some interesting analyses down the road, so let’s go ahead and add <code>profit</code> as a new column to our data frame.</p>
<div data-height="300" data-datacamp-exercise="" data-encoded="true">eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6Im1vdmllcyA8LSByZWFkLmNzdihcImh0dHA6Ly9zMy5hbWF6b25hd3MuY29tL2Rjd29vZHMyNzE3L21vdmllcy5jc3ZcIikiLCJzYW1wbGUiOiIjIEFkZCBhIGNvbHVtbiBmb3IgYHByb2ZpdGAgdG8gYG1vdmllc2AgXG5tb3ZpZXMkLi4uLi4gPC0gbW92aWVzJC4uLi4uIC0gbW92aWVzJC4uLi4uIiwic29sdXRpb24iOiIjIEFkZCBhIGNvbHVtbiBmb3IgYHByb2ZpdGAgdG8gYG1vdmllc2Bcbm1vdmllcyRwcm9maXQgPC0gbW92aWVzJGdyb3NzIC0gbW92aWVzJGJ1ZGdldCJ9</div>
</div>
<div>
<h2>Correlation</h2>
<p>Now that <code>profit</code> has been added as a new column in our data frame, it’s time to take a closer look at the relationships between the variables of your data set.</p>
<p>Let’s check out how <code>profit</code> fluctuates relative to each movie’s <code>rating</code>.</p>
<p>For this, you can use R’s built in <code>plot</code> and <code>abline</code> functions, where <code>plot</code> will result in a scatter plot and <code>abline</code> will result in a regression line or “line of best fit” due to our inclusion of the linear model argument as you will see below.</p>
<div data-height="300" data-datacamp-exercise="" data-encoded="true">eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6Im1vdmllcyA8LSByZWFkLmNzdih1cmwoXCJodHRwOi8vczMuYW1hem9uYXdzLmNvbS9kY3dvb2RzMjcxNy9tb3ZpZXMuY3N2XCIpKVxubW92aWVzJHByb2ZpdCA8LSBtb3ZpZXMkZ3Jvc3MgLSBtb3ZpZXMkYnVkZ2V0XG5vcHRpb25zKHNjaXBlbiA9IDk5OSkiLCJzYW1wbGUiOiIjIENyZWF0ZSB0aGUgc2NhdHRlciBwbG90IHdpdGggYHJhdGluZ3NgIG9uIHRoZSB4LWF4aXMgYW5kIGBwcm9maXRgIG9uIHRoZSB5LWF4aXNcbnBsb3QobW92aWVzJC4uLi4sIG1vdmllcyQuLi4uKVxuXG4jIEFkZCBhIHJlZ3Jlc3Npb24gbGluZXdpdGggdGhlIGZvcm0gYGFibGluZShsbSh5IH4geCkpYFxuXG5hYmxpbmUobG0obW92aWVzJC4uLi4gfiBtb3ZpZXMkLi4uLikpIiwic29sdXRpb24iOiIjIENyZWF0ZSB0aGUgc2NhdHRlciBwbG90IHdpdGggYHJhdGluZ3NgIG9uIHRoZSB4LWF4aXMgYW5kIGBwcm9maXRgIG9uIHRoZSB5LWF4aXNcbnBsb3QobW92aWVzJHJhdGluZywgbW92aWVzJHByb2ZpdClcblxuIyBBZGQgYSByZWdyZXNzaW9uIGxpbmUgd2l0aCB0aGUgZm9ybSBgYWJsaW5lKGxtKHkgfiB4KSlgIFxuYWJsaW5lKGxtKG1vdmllcyRwcm9maXQgfiBtb3ZpZXMkcmF0aW5nKSkifQ==</div>
<p>Was the output pretty much what you expected?</p>
<p><img alt="R regression" src="https://i2.wp.com/community.datacamp.com.s3.amazonaws.com/community/production/ckeditor_assets/pictures/437/content_rplot01.png?w=456" data-recalc-dims="1"></p>
<p>In general, it appears that movies with a higher <code>rating</code> tend to have higher <code>profit</code>. Another way to phrase this statement would be to say that a positive correlation exists between <code>rating</code> and <code>profit</code>, at least in our data frame.</p>
<p>That said, even a cursory glance at the plot reveals that there are plenty of highly rated movies that weren’t exactly blockbusters, and there are a number of very profitable movies that got relatively low ratings.</p>
<p>Correlation does NOT imply causation!</p>
<p>One anecdote to help you understand correlation versus causation is as follows: I run an ice cream stand at the beach. The average number of jaywalkers in the city tends to increase when my ice cream sales do, but is my ice cream causing people to disregard traffic laws, or is some other force in play? My ice cream is admittedly awesome, but the sunshine outside probably has something to do with people wanting ice cream and people not wanting to stand around getting sunburnt at crosswalks. A relationsip (correlation) does exist between my ice cream sales and the number of jay walkers, but you can’t definitively state that it’s a causal relationship.</p>
<p>Keep that line of reasoning in mind as you proceed through this tutorial!</p>
</div>
<div>
<h2>Calculating Correlation in R</h2>
<p>So what types of relationships exist between the variables in <code>movies</code>, and how can you evaluate those relationships quantitatively?</p>
<p>The first way is to produce correlations and correlation matrices with <code>cor()</code>:</p>
<div data-height="300" data-datacamp-exercise="" data-encoded="true">eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6Im1vdmllcyA8LSByZWFkLmNzdihcImh0dHA6Ly9zMy5hbWF6b25hd3MuY29tL2Rjd29vZHMyNzE3L21vdmllcy5jc3ZcIilcbm1vdmllcyRwcm9maXQgPC0gbW92aWVzJGdyb3NzIC0gbW92aWVzJGJ1ZGdldFxub3B0aW9ucyhzY2lwZW4gPSA5OTkpIiwic2FtcGxlIjoiIyBDb21wdXRlIFBlYXJzb24gY29ycmVsYXRpb25cbmNvcihtb3ZpZXMkcmF0aW5nLCBtb3ZpZXMkcHJvZml0KVxuXG4jIENvcnJlbGF0aW9uIE1hdHJpeFxuY29yKG1vdmllc1ssNDoxMF0pIn0=</div>
<p><strong>Note</strong> that you can also specify the method that you want to indicate which correlation coefficient you want to compute. Be careful, because there are always some assumptions that these correlations work with: the Kendall and Spearman methods only make sense for ordered inputs. This means that you’ll need to order your data before calculating the correlation coefficient.</p>
<p>Additionally, the default method, the Pearson correlation, assumes that your variables are normally distributed, that there is a straight line relationship between each of the variables and that the data is normally distributed about the regression line.</p>
<p>Note also that you can use <code>rcorr()</code>, which is part of the <code>Hmisc</code> package to compute the significance levels for pearson and spearman correlations.</p>
<p>You’ll also see that the correlation matrix is actually a table that shows correlation coefficients between sets of variables. This is already a great first way to get an idea of which relationships exist between the variables fo your data set, but let’s go a bit deeper into this in the next section.</p>
</div>
<div>
<h2>Visually Exploring Correlation: The R Correlation Matrix</h2>
<p>In this next exploration, you’ll plot a correlation matrix using the variables available in your <code>movies</code> data frame. This simple plot will enable you to quickly visualize which variables have a negative, positive, weak, or strong correlation to the other variables.</p>
<p>To pull this wizardry off, you’ll be using a nifty package called <code>GGally</code> and a function called <code>ggcorr()</code>.</p>
<p>The form of this function call will be <code>ggcorr(df)</code>, where <code>df</code> is the name of the data frame you’re calling the function on. The output of this function will be a triangular-shaped, color-coded matrix labelled with our variable names. The correlation coefficient of each variable relative to the other variables can be found by reading across and / or down the matrix, depending on the variable’s location in the matrix.</p>
<p>It may sound confusing, but it’s really simple in practice, so give it a shot in the following code chunk!</p>
<div data-height="300" data-datacamp-exercise="" data-encoded="true">eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6Im1vdmllcyA8LSByZWFkLmNzdih1cmwoXCJodHRwOi8vczMuYW1hem9uYXdzLmNvbS8vZGN3b29kczI3MTcvbW92aWVzLmNzdlwiKSlcbm1vdmllcyRwcm9maXQgPC0gbW92aWVzJGdyb3NzIC0gbW92aWVzJGJ1ZGdldFxubGlicmFyeShcIkdHYWxseVwiKSIsInNhbXBsZSI6IiMgUmVwbGFjZSB0aGUgXCIuLi4uLlwiIHdpdGggdGhlIG5hbWUgb2YgdGhlIGRhdGEgZnJhbWUgXG5nZ2NvcnIoLi4uLi4pIiwic29sdXRpb24iOiIjIFJlcGxhY2UgdGhlIFwiLi4uLi5cIiB3aXRoIHRoZSBuYW1lIG9mIHRoZSBkYXRhIGZyYW1lIFxuZ2djb3JyKG1vdmllcykifQ==</div>
<p><img alt="correlation coefficient" src="https://i2.wp.com/community.datacamp.com.s3.amazonaws.com/community/production/ckeditor_assets/pictures/436/content_rplot02.png?w=456" data-recalc-dims="1"></p>
<p>So in the correlation matrix you’ve just created (great job, by the way!), you can go to the row or column associated with a variable, such as <code>year</code> and see its correlation coefficient as indicated by the color of the cell that corresponds with another variable.</p>
<p>In examining <code>year</code>, for example, you can see that there is a weak, positive correlation with <code>budget</code> and a similarly weak, negative correlation with <code>rating</code>.</p>
<p>Correlation coefficients are always between -1 and 1, inclusive. A correlation coefficient of -1 indicates a perfect, negative fit in which y-values decrease at the same rate than x-values increase. A correlation coefficient of 1 indicates a perfect, positive fit in which <code>y</code>-values increase at the same rate that <code>x</code>-values increase.</p>
<p>In most cases, such as your <code>year</code> example above, the correlation coefficient will be somewhere between -1 and 1.</p>
<p>Did you notice anything a bit odd about the variables shown in the correlation matrix?</p>
<p>Not all of the variables in <code>movies</code> are present!</p>
<p>That’s because not all of the variables in <code>movies</code> are numeric. The <code>ggcorr</code> function disregards non-numeric variables automatically, which saves some time by exempting the user from “subsetting-out” such variables prior to creating the matrix.</p>
<p>If you want your correlation matrix to really “pop” (or maybe you’re a little colorblind, like me), there are a few simple tweaks you can make to produce more visually-compelling, data-loaded matrices.</p>
<p>In the code chunk that follows, you’ve included the <code>label</code> argument, which can be set equal to either <code>TRUE</code> or <code>FALSE</code>. The default setting is <code>FALSE</code>, but if you add <code>label = TRUE</code>, the correlation coefficient of each relationship is included in the appropriate cell. This keeps you from guessing the value of each coefficient based off the color scale.</p>
<p>The <code>label_alpha</code> argument allows you to increase or decrease the opacity of each label based on the strength of the correlation coefficient. This is super helpful in quick, visual data analysis.</p>
<p>Don’t take my word for it, try it for yourself!</p>
<p>Now let’s plot a better correlation matrix:</p>
<div data-height="300" data-datacamp-exercise="" data-encoded="true">eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6Im1vdmllcyA8LSByZWFkLmNzdih1cmwoXCJodHRwOi8vczMuYW1hem9uYXdzLmNvbS8vZGN3b29kczI3MTcvbW92aWVzLmNzdlwiKSlcbm1vdmllcyRwcm9maXQgPC0gbW92aWVzJGdyb3NzIC0gbW92aWVzJGJ1ZGdldFxubGlicmFyeShcIkdHYWxseVwiKSIsInNhbXBsZSI6IiMgRmlsbCBpbiBcIlRSVUVcIiBvciBcIkZBTFNFXCIgdG8gc2VlIGhvdyB0aGUgY29ycmVsYXRpb24gbWF0cml4IGNoYW5nZXNcbmdnY29ycihtb3ZpZXMsIFxuICAgICAgIGxhYmVsID0gLi4uLi4sIFxuICAgICAgIGxhYmVsX2FscGhhID0gLi4uLi4pIiwic29sdXRpb24iOiIjIEZpbGwgaW4gXCJUUlVFXCIgb3IgXCJGQUxTRVwiIHRvIHNlZSBob3cgdGhlIGNvcnJlbGF0aW9uIG1hdHJpeCBjaGFuZ2VzXG5nZ2NvcnIobW92aWVzLCBcbiAgICAgICBsYWJlbCA9IFRSVUUsIFxuICAgICAgIGxhYmVsX2FscGhhID0gVFJVRSkifQ==</div>
<p><img alt="correlation matrix" src="https://i1.wp.com/community.datacamp.com.s3.amazonaws.com/community/production/ckeditor_assets/pictures/435/content_rplot03.png?w=456" data-recalc-dims="1"></p>
<p>In the plots that follow, you will see that when a plot with a “strong” correlation is created, the slope of its regression line (<code>x</code>/<code>y</code>) is closer to 1/1 or -1/1, while a “weak” correlation’s plot may have a regression line with barely any slope. A slope closer to 1/1 or -1/1 implies that the two variables plotted are closely related.</p>
<p>In the case of our <code>movies</code> data frame, such a regression line can lend powerful insights about the nature of your variables and may indicate an interdependence of those variables.</p>
<p>For instance, in your previous plot of <code>profit</code> over <code>rating</code>, you saw that our regression line had a moderate, positive slope. Taking a look back at your correlation matrix, you see that the correlation coefficient of these two variables is 0.3.</p>
<p>Makes sense, right?</p>
</div>
<div>
<h2>Correlation Types</h2>
<p>Let’s now check out additional plots illustrating the different types of correlations that you have seen in the previous section!</p>
<div>
<h3>Strong Correlation: Plotting Votes Versus Reviews</h3>
<p>Lets begin by plotting two variables with a strong, positive correlation.</p>
<p>In looking at our correlation matrix, it seems that <code>votes</code> versus <code>reviews</code> meets your criteria, with a correlation value of 0.8. That means that our slope should be relatively close to 1/1.</p>
<p>You will be implementing the <code>ggplot2</code> package in this exercise and the exercises that follow. The <code>ggplot2</code> package is a versatile toolset that can be used to create compelling, data visualizations.</p>
<p>In this case, you’re going to take advantage of the <code>qplot</code> function within the <code>ggplot2</code> package, which can produce many kinds of plots based on what plot type is passed to the <code>geom</code> (geometry) argument.</p>
<p>In the code chunk that follows, <code>geom</code> has been set to a vector containing two types of geometry, <code>point</code> and <code>smooth</code>, where <code>point</code> results in a scatterplot and <code>smooth</code> generates a trendline. You’ll also notice that <code>method</code> has been set to <code>lm</code>, which means the trendline will be the familiar, straight regression line that was created in our plot of <code>profit</code> over <code>years</code> previously.</p>
<p>In short, you’re about to learn how easy it is to generate killer data visualizations in a flash!</p>
<div data-height="300" data-datacamp-exercise="" data-encoded="true">eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImxpYnJhcnkoXCJnZ3Bsb3QyXCIpXG5tb3ZpZXMgPC0gcmVhZC5jc3YodXJsKFwiaHR0cDovL3MzLmFtYXpvbmF3cy5jb20vL2Rjd29vZHMyNzE3L21vdmllcy5jc3ZcIikpXG5tb3ZpZXMkcHJvZml0IDwtIG1vdmllcyRncm9zcyAtIG1vdmllcyRidWRnZXRcbm9wdGlvbnMoc2NpcGVuID0gOTk5KSIsInNhbXBsZSI6IiMgUGxvdCB2b3RlcyB2cyByZXZpZXdzXG5xcGxvdChtb3ZpZXMkLi4uLi4sIFxuICAgICAgbW92aWVzJC4uLi4uLCBcbiAgICAgIGRhdGEgPSAuLi4uLiwgXG4gICAgICBnZW9tID0gYyhcInBvaW50XCIsIFwic21vb3RoXCIpLCBcbiAgICAgIG1ldGhvZCA9IFwibG1cIiwgXG4gICAgICBhbHBoYSA9IEkoMSAvIDUpLCBcbiAgICAgIHNlID0gRkFMU0UpIiwic29sdXRpb24iOiIjIFBsb3Qgdm90ZXMgdnMgcmV2aWV3c1xucXBsb3QobW92aWVzJHZvdGVzLCBcbiAgICAgIG1vdmllcyRyZXZpZXdzLCBcbiAgICAgIGRhdGEgPSBtb3ZpZXMsIFxuICAgICAgZ2VvbSA9IGMoXCJwb2ludFwiLCBcInNtb290aFwiKSwgXG4gICAgICBtZXRob2QgPSBcImxtXCIsIFxuICAgICAgYWxwaGEgPSBJKDEgLyA1KSwgXG4gICAgICBzZSA9IEZBTFNFKSJ9</div>
<p><img alt="R positive correlation" src="https://i1.wp.com/community.datacamp.com.s3.amazonaws.com/community/production/ckeditor_assets/pictures/434/content_rplot04.png?w=456" data-recalc-dims="1"></p>
<p>You may have recognized the <code>alpha</code> argument included in the <code>qplot</code> function above. The use of <code>alpha</code> in <code>qplot</code> applies a gradient of opacity to the points in the scatter plot similar to how it changed the opacity of the correlation coefficient labels in <code>ggcorr</code>.</p>
<p>In the configuration you used, total opacity is only achieved when a point is overlapped by 5 other points. Increasing or decreasing the number in the denominator of <code>alpha</code> will affect the number of overlapping points required to change a point’s opacity. One reason to use this aesthetic is that it can help users quickly identify concentrations of data points in their plots, which in turn can bring new insights about our data to light with only a glance.</p>
<p>Go ahead and tinker with the code and see how it works for yourself!</p>
</div>
<div>
<h3>Weak Correlation: Plotting Profit Over Years</h3>
<p>Now let’s see what a “weak”, negative correlation looks like by plotting <code>profit</code> versus <code>years</code>. This time, no <code>method</code> has been specified, so our trendline is going to vary according to the data in a curvilinear fashion (a fitted curve), which is the default for this function. You’ll notice that along certain parts of the trendline there is a light gray area that increases or decreases in size according to the confidence interval of the trendline.</p>
<p>If you’re not familiar with the concept of a confidence interval, don’t worry! R will do the work for you, and the concept will be better explained after this exercise.</p>
<p>For now, just fill in the blanks with the necessary code and observe!</p>
<div data-height="300" data-datacamp-exercise="" data-encoded="true">eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImxpYnJhcnkoXCJnZ3Bsb3QyXCIpXG5tb3ZpZXMgPC0gcmVhZC5jc3YodXJsKFwiaHR0cDovL3MzLmFtYXpvbmF3cy5jb20vL2Rjd29vZHMyNzE3L21vdmllcy5jc3ZcIikpXG5tb3ZpZXMkcHJvZml0IDwtIG1vdmllcyRncm9zcyAtIG1vdmllcyRidWRnZXRcbm9wdGlvbnMoc2NpcGVuID0gOTk5KSIsInNhbXBsZSI6IiMgUGxvdCBwcm9maXQgb3ZlciB5ZWFyc1xucXBsb3QobW92aWVzJHllYXIsIFxuICAgICAgbW92aWVzJHByb2ZpdCwgXG4gICAgICBkYXRhID0gbW92aWVzLCBcbiAgICAgIGdlb20gPSBjKFwicG9pbnRcIiwgXCJzbW9vdGhcIiksIFxuICAgICAgYWxwaGEgPSBJKDEgLyA1KSkiLCJzb2x1dGlvbiI6IiMgUGxvdCBwcm9maXQgb3ZlciB5ZWFyc1xucXBsb3QobW92aWVzJHllYXIsIFxuICAgICAgbW92aWVzJHByb2ZpdCwgXG4gICAgICBkYXRhID0gbW92aWVzLCBcbiAgICAgIGdlb20gPSBjKFwicG9pbnRcIiwgXCJzbW9vdGhcIiksIFxuICAgICAgYWxwaGEgPSBJKDEgLyA1KSkifQ==</div>
<p><img alt="negative R correlation" src="https://i0.wp.com/community.datacamp.com.s3.amazonaws.com/community/production/ckeditor_assets/pictures/433/content_rplot05.png?w=456" data-recalc-dims="1"></p>
<p>So now that you’ve had a chance to see the confidence interval and smoothing curve in action, what observations can you make?</p>
<ul>
<li>First, you see that in the initial portion of the plot, <code>profit</code> seems to be increasing each <code>year</code>.</li>
<li>Second, you notice that the grey area offset from your smoothing curve is initially quite large and the number of data points (or observations) is quite small.</li>
<li>Third, as the curve moves into a larger concentration of observations, the grey area diminishes and practically hugs the curve, which begins to decline in later <code>year</code>s.</li>
</ul>
<p>In conclusion, plotting the confidence interval in conjunction with the smoothing curve allows us to see the amount of uncertainty associated with our regression line. That uncertainy increases with fewer observations and decreases with more observations.</p>
<p>This is very helpful in visualizing how the relationship between our variables changes throughout the data frame and reveals how concentrations of observations alter the plotted curve.</p>
<p>The caveat here is that the plot may be a bit misleading, as it’s hard to visually discern whether there is a positive or negative correlation of your variables.</p>
<p>Let’s now plot <code>profit</code> over <code>year</code>s in the same way you plotted <code>votes</code> versus <code>reviews</code>: simply replace the “…..” in each argument so that years are plotted and on <code>x</code>-axis and <code>profit</code> is on the y-axis. Make sure to specify which data frame that “data” equals!</p>
<div data-height="300" data-datacamp-exercise="" data-encoded="true">eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImxpYnJhcnkoXCJnZ3Bsb3QyXCIpXG5tb3ZpZXMgPC0gcmVhZC5jc3YodXJsKFwiaHR0cDovL3MzLmFtYXpvbmF3cy5jb20vL2Rjd29vZHMyNzE3L21vdmllcy5jc3ZcIikpXG5tb3ZpZXMkcHJvZml0IDwtIG1vdmllcyRncm9zcyAtIG1vdmllcyRidWRnZXRcbm9wdGlvbnMoc2NpcGVuID0gOTk5KSIsInNhbXBsZSI6IiMgUGxvdCB0aGUgeWVhcnMgb24gdGhlIHgtYXhpcywgcHJvZml0IG9uIHRoZSB5LWF4aXNcbnFwbG90KG1vdmllcyQuLi4uLiwgXG4gICAgICBtb3ZpZXMkLi4uLi4sIFxuICAgICAgZGF0YSA9IC4uLi4uLCBcbiAgICAgIGdlb20gPSBjKFwicG9pbnRcIiwgXCJzbW9vdGhcIiksIFxuICAgICAgbWV0aG9kID0gXCJsbVwiLCBcbiAgICAgIGFscGhhID0gSSgxIC8gNSksIFxuICAgICAgc2UgPSBGQUxTRSkiLCJzb2x1dGlvbiI6IiMgUGxvdCB0aGUgeWVhcnMgb24gdGhlIHgtYXhpcywgcHJvZml0IG9uIHRoZSB5LWF4aXNcbnFwbG90KG1vdmllcyR5ZWFyLCBcbiAgICAgIG1vdmllcyRwcm9maXQsIFxuICAgICAgZGF0YSA9IG1vdmllcywgXG4gICAgICBnZW9tID0gYyhcInBvaW50XCIsIFwic21vb3RoXCIpLCBcbiAgICAgIG1ldGhvZCA9IFwibG1cIiwgXG4gICAgICBhbHBoYSA9IEkoMSAvIDUpLCBcbiAgICAgIHNlID0gRkFMU0UpIn0=</div>
<p><img alt="weak correlation R" src="https://i0.wp.com/community.datacamp.com.s3.amazonaws.com/community/production/ckeditor_assets/pictures/432/content_rplot06.png?w=456" data-recalc-dims="1"></p>
<p>Looking back to your correlation matrix, you see that the correlation coefficient of <code>profit</code> and <code>year</code> is -0.1, which is perhaps more readily visualized in the plot that incorporates a line of best fit than it is in the plot that used a fitted curve.</p>
</div>
</div>
<div>
<h2>Tying it all together</h2>
<p>Let’s now take a look at another powerful function available in <code>GGally</code> called <code>ggpairs</code>. This function is great because it allows users to create a matrix that shows the correlation coefficient of multiple variables in conjunction with a scatterplot (including a line of best fit with a confidence interval) and a density plot.</p>
<p>Using this one function, you can effectively combine everything you’ve covered in this tutorial in a concise, readily comprehensible fashion.</p>
<p>In the following code chunk, pick your three favorite variables from the data frame (no subsetting required!), plug them in, and play!</p>
<div data-height="300" data-datacamp-exercise="" data-encoded="true">eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImxpYnJhcnkoXCJnZ3Bsb3QyXCIpXG5saWJyYXJ5KFwiR0dhbGx5XCIpXG5tb3ZpZXMgPC0gcmVhZC5jc3YodXJsKFwiaHR0cDovL3MzLmFtYXpvbmF3cy5jb20vL2Rjd29vZHMyNzE3L21vdmllcy5jc3ZcIikpXG5tb3ZpZXMkcHJvZml0IDwtIG1vdmllcyRncm9zcyAtIG1vdmllcyRidWRnZXRcbm9wdGlvbnMoc2NpcGVuID0gOTk5KSIsInNhbXBsZSI6IiMgUGx1ZyBpbiB5b3VyIHRocmVlIGZhdm9yaXRlIHZhcmlhYmxlcyBhbmQgdGlua2VyIGF3YXkhXG5nZ3BhaXJzKG1vdmllcywgXG4gICAgICAgIGNvbHVtbnMgPSBjKFwiLi4uLi5cIiwgXCIuLi4uLlwiLCBcIi4uLi4uXCIpLCBcbiAgICAgICAgdXBwZXIgPSBsaXN0KGNvbnRpbnVvdXMgPSB3cmFwKFwiY29yXCIsIFxuICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgc2l6ZSA9IDEwKSksIFxuICAgICAgICBsb3dlciA9IGxpc3QoY29udGludW91cyA9IFwic21vb3RoXCIpKSIsInNvbHV0aW9uIjoiIyBQbHVnIGluIHlvdXIgdGhyZWUgZmF2b3JpdGUgdmFyaWFibGVzIGFuZCB0aW5rZXIgYXdheSFcbmdncGFpcnMobW92aWVzLCBcbiAgICAgICAgY29sdW1ucyA9IGMoXCJjYXN0X2ZhY2Vib29rX2xpa2VzXCIsIFwiYnVkZ2V0XCIsIFwicHJvZml0XCIpLCBcbiAgICAgICAgdXBwZXIgPSBsaXN0KGNvbnRpbnVvdXMgPSB3cmFwKFwiY29yXCIsIFxuICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgc2l6ZSA9IDEwKSksIFxuICAgICAgICBsb3dlciA9IGxpc3QoY29udGludW91cyA9IFwic21vb3RoXCIpKSJ9</div>
<p><img alt="Correlation Matrix R" src="https://i0.wp.com/community.datacamp.com.s3.amazonaws.com/community/production/ckeditor_assets/pictures/431/content_rplot07.png?w=456" data-recalc-dims="1"></p>
<p>As you may have guessed, there are many scenarios in which one type of plot is ideal – the key is to learn what visualization techniques are available, how to use them, and what the end result means!</p>
</div>
<div>
<h2>Going Forward</h2>
<p>You’ve covered a lot of ground in this tutorial, so congratulations for making it through to the end!</p>
<p>I hope you are able to put these concepts to work in your own analytical adventures! These are really the basics of data exploration in R and there’s so much more that you can do to make sure that you get a good feel for your data before you start analyzing and modeling it. Why not take your efforts up a notch and start DataCamp’s <a target="_blank" rel="nofollow" href="https://www.datacamp.com/courses/exploratory-data-analysis">Exploratory Data Analysis</a> course?</p>
<p>In the mean time, I encourage you to head on over to <a target="_blank" rel="nofollow" href="https://www.kaggle.com/">Kaggle</a> and find a rich, fascinating data set of your own to explore if you don’t want to continue with the <code>movies</code> data set!</p>
</div>
<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.datacamp.com/community/blog/r-correlation-tutorial"> DataCamp 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/4CClShx88FI"></a></div>