<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/the-importance-of-data-visualization/"></div></div><div><a data-url="https://www.r-bloggers.com/the-importance-of-data-visualization/" data-counturl="https://www.r-bloggers.com/the-importance-of-data-visualization/" data-count="horizontal" data-text="The importance of Data Visualization" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://datascienceplus.com/the-importance-of-data-visualization/"> DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Before we perform any analysis and come up with any assumptions about the distributions of and relationships between variables in our datasets, it is always a good idea to visualize our data in order to understand their properties and identify appropriate analytics techniques. In this post, let’s see the dramatic differences in conclutions that we can make based on (1) simple statistics only, and (2) data visualization.</p>
<h3>The four data sets</h3>
<p>The Anscombe dataset, which is found in the base R datasets packege, is handy for showing the importance of data visualization in data analysis. It consists of four datasets and each dataset consists of eleven (x,y) points.</p>
<pre>anscombe 
<em>
x1 x2 x3 x4 y1 y2 y3 y4
1 10 10 10 8 8.04 9.14 7.46 6.58
2 8 8 8 8 6.95 8.14 6.77 5.76
3 13 13 13 8 7.58 8.74 12.74 7.71
4 9 9 9 8 8.81 8.77 7.11 8.84
5 11 11 11 8 8.33 9.26 7.81 8.47
6 14 14 14 8 9.96 8.10 8.84 7.04
7 6 6 6 8 7.24 6.13 6.08 5.25
8 4 4 4 19 4.26 3.10 5.39 12.50
9 12 12 12 8 10.84 9.13 8.15 5.56
10 7 7 7 8 4.82 7.26 6.42 7.91
11 5 5 5 8 5.68 4.74 5.73 6.89 </em></pre>
<p>Let’s make some massaging to make the data more convinient for analysis and plotting</p>
<p>Create four groups: setA, setB, setC and setD.</p>
<pre>
library(ggplot2)
library(dplyr)
library(reshape2)

setA=select(anscombe, x=x1,y=y1)
setB=select(anscombe, x=x2,y=y2)
setC=select(anscombe, x=x3,y=y3)
setD=select(anscombe, x=x4,y=y4)
</pre>
<p>Add a third column which can help us to identify the four groups.</p>
<pre>
setA$group ='SetA'
setB$group ='SetB'
setC$group ='SetC'
setD$group ='SetD'

head(setA,4)  # showing sample data points from setA
<em> x y group
1 10 8.04 SetA
2 8 6.95 SetA
3 13 7.58 SetA
4 9 8.81 SetA</em>
</pre>
<p>Now, let’s merge the four datasets.</p>
<pre>
all_data=rbind(setA,setB,setC,setD)  # merging all the four data sets
all_data[c(1,13,23,43),]  # showing sample
<em>
x y group
1 10 8.04 SetA
13 8 8.14 SetB
23 10 7.46 SetC
43 8 7.91 SetD</em>
</pre>
<h3>Compare their summary statistics</h3>
<pre>
summary_stats =all_data%>%group_by(group)%>%summarize(&quot;mean x&quot;=mean(x),
                                       &quot;Sample variance x&quot;=var(x),
                                       &quot;mean y&quot;=round(mean(y),2),
                                       &quot;Sample variance y&quot;=round(var(y),1),
                                       'Correlation between x and y '=round(cor(x,y),2)
                                      )
models = all_data %>% 
      group_by(group) %>%
      do(mod = lm(y ~ x, data = .)) %>%
      do(data.frame(var = names(coef(.$mod)),
                    coef = round(coef(.$mod),2),
                    group = .$group)) %>%
dcast(., group~var, value.var = &quot;coef&quot;)

summary_stats_and_linear_fit = cbind(summary_stats, data_frame(&quot;Linear regression&quot; =
                                    paste0(&quot;y = &quot;,models$&quot;(Intercept)&quot;,&quot; + &quot;,models$x,&quot;x&quot;)))

summary_stats_and_linear_fit
<em>
group mean x Sample variance x mean y Sample variance y Correlation between x and y 1 SetA 9 11 7.5 4.1 0.82
2 SetB 9 11 7.5 4.1 0.82
3 SetC 9 11 7.5 4.1 0.82
4 SetD 9 11 7.5 4.1 0.82
Linear regression
1 y = 3 + 0.5x
2 y = 3 + 0.5x
3 y = 3 + 0.5x
4 y = 3 + 0.5x</em>
</pre>
<p>If we look only at the simple summary statistics shown above, we would conclude that these four data sets are identical.</p>
<h3>What if we plot the four data sets?</h3>
<pre>
 ggplot(all_data, aes(x=x,y=y)) +geom_point(shape = 21, colour = &quot;red&quot;, fill = &quot;orange&quot;, size = 3)+
    ggtitle(&quot;Anscombe's data sets&quot;)+geom_smooth(method = &quot;lm&quot;,se = FALSE,color='blue') + 
    facet_wrap(~group, scales=&quot;free&quot;)
</pre>
<p><a title="" data-rel="lightbox-0" target="_blank" href="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/08/fig1.png"><img sizes="(max-width: 490px) 100vw, 490px" alt="fig1" class="wp-image-4582" src="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/08/fig1-490x317.png?resize=456%2C295" data-recalc-dims="1"></a><br>
As we can see from the figures above, the datasets are very different from each other. The Anscombe’s quartet is a good example that shows that we have to visualize the relatonships, distributuions and outliers of our data and we shoul not rely only on simple statistics.</p>
<h2>Summary</h2>
<p>We should look at the data graphically before we start analysis. Further, we should understand that basic statistics properties can often fail to capture real-world complexities (such as outliers, relationships and complex distributions) since summary statistics do not capture all of the complexities of the data.</p>
<ul></ul>
<p><strong>
<p>Related Post</p>
<p>
<ol>
<li><a title="Permanent Link toggplot2 themes examples" target="_blank" rel="bookmark" href="http://datascienceplus.com/ggplot2-themes-examples/">ggplot2 themes examples</a></li>
<li><a title="Permanent Link toMap the Life Expectancy in United States with data from Wikipedia" target="_blank" rel="bookmark" href="http://datascienceplus.com/map-the-life-expectancy-in-united-states-with-data-from-wikipedia/">Map the Life Expectancy in United States with data from Wikipedia</a></li>
<li><a title="Permanent Link toWhat can we learn from the statistics of the EURO 2016 – Application of factor analysis" target="_blank" rel="bookmark" href="http://datascienceplus.com/what-can-we-learn-from-the-statistics-of-the-euro-2016-application-of-factor-analysis/">What can we learn from the statistics of the EURO 2016 – Application of factor analysis</a></li>
<li><a title="Permanent Link toVisualizing obesity across United States by using data from Wikipedia" target="_blank" rel="bookmark" href="http://datascienceplus.com/visualizing-obesity-across-united-states-by-using-data-from-wikipedia/">Visualizing obesity across United States by using data from Wikipedia</a></li>
<li><a title="Permanent Link toPlotting App for ggplot2 – Part 2" target="_blank" rel="bookmark" href="http://datascienceplus.com/plotting-app-for-ggplot2-part-2/">Plotting App for ggplot2 – Part 2</a></li>
</ol></strong>
<div>
<a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=BfxEGOR8jC4:aRzpDS4pFOQ:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/datascienceplus?i=BfxEGOR8jC4:aRzpDS4pFOQ:D7DqB2pKExk" border="0"></a> <a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=BfxEGOR8jC4:aRzpDS4pFOQ:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/datascienceplus?d=qj6IDK7rITs" border="0"></a> <a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=BfxEGOR8jC4:aRzpDS4pFOQ:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/datascienceplus?d=yIl2AUoC8zA" border="0"></a>
</div>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/the-importance-of-data-visualization/"></div></div><div><a data-url="https://www.r-bloggers.com/the-importance-of-data-visualization/" data-counturl="https://www.r-bloggers.com/the-importance-of-data-visualization/" data-count="vertical" data-text="The importance of Data Visualization" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://datascienceplus.com/the-importance-of-data-visualization/"> DataScience+</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/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=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/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/6e0XuLP8Yhs"></a></div>