<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/7-visualizations-you-should-learn-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.tatvic.com/blog/7-visualizations-learn-r-2/"> Tatvic Blog » R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" rel="nofollow" href="https://i1.wp.com/www.tatvic.com/blog/wp-content/uploads/2016/12/Pic_1_featured_Image.png"><img alt="Visualizations in R" class="wp-image-6453" src="https://i1.wp.com/www.tatvic.com/blog/wp-content/uploads/2016/12/Pic_1_featured_Image.png?resize=456%2C249" data-recalc-dims="1"></a></p>
<h1>7 Visualizations You Should Learn in R</h1>
<p>With ever increasing volume of data, it is impossible to tell stories without visualizations. Data visualization is an art of how to turn numbers into useful knowledge.</p>
<p>R Programming lets you learn this art by offering a set of inbuilt functions and libraries to build visualizations and present data. Before the technical implementations of the visualization, let’s see first how to select the right chart type.</p>
<h3>Selecting the Right Chart Type</h3>
<h4>There are four basic presentation types:</h4>
<ol>
<li>Comparison</li>
<li>Composition</li>
<li>Distribution</li>
<li>Relationship</li>
</ol>
<p>To determine which amongst these is best suited for your data, I suggest you should answer a few questions like,</p>
<ul>
<li>How many variables do you want to show in a single chart?</li>
<li>How many data points will you display for each variable?</li>
<li>Will you display values over a period of time, or among items or groups?</li>
</ul>
<p>Below is a great explanation on selecting a <a target="_blank" rel="nofollow" href="http://extremepresentation.typepad.com/.shared/image.html?/photos/uncategorized/choosing_a_good_chart.jpg">right chart type</a> by Dr. Andrew Abela.</p>
<p><a target="_blank" rel="nofollow" href="https://i1.wp.com/www.tatvic.com/blog/wp-content/uploads/2016/12/Pic_2.png"><img alt=" best visualizations in R" class="wp-image-6454" src="https://i1.wp.com/www.tatvic.com/blog/wp-content/uploads/2016/12/Pic_2.png?resize=456%2C330" data-recalc-dims="1"></a></p>
<p>In your day-to-day activities, you’ll come across the below listed 7 charts most of the time.</p>
<ol>
<li>Scatter Plot</li>
<li>Histogram</li>
<li>Bar &amp; Stack Bar Chart</li>
<li>Box Plot</li>
<li>Area Chart</li>
<li>Heat Map</li>
<li>Correlogram</li>
</ol>
<p>We’ll use ‘Big Mart data’ example as shown below to understand how to create visualizations in R. You can download the full dataset from <a target="_blank" rel="nofollow" href="https://docs.google.com/spreadsheets/d/1PR5StHxg2jlMCb4IUilGSEwhylXn-3q3EJucSaVolCU/edit#gid=0">here</a>.</p>
<p><a target="_blank" rel="nofollow" href="https://i2.wp.com/www.tatvic.com/blog/wp-content/uploads/2016/12/Pic_3.png"><img alt="best R visualizations" class="wp-image-6455" src="https://i2.wp.com/www.tatvic.com/blog/wp-content/uploads/2016/12/Pic_3.png?resize=456%2C212" data-recalc-dims="1"></a></p>
<p>Now let’s see how to use these visualizations in R</p>
<h3>1. Scatter Plot</h3>
<p><strong>When to use</strong>: Scatter Plot is used to see the relationship between two continuous variables.</p>
<p>In our above mart dataset, if we want to visualize the items as per their cost data, then we can use scatter plot chart using two continuous variables, namely Item_Visibility &amp; Item_MRP as shown below.</p>
<p><a target="_blank" rel="nofollow" href="https://i0.wp.com/www.tatvic.com/blog/wp-content/uploads/2016/12/Pic_4.png"><img alt="data visualizations R" class="wp-image-6456" src="https://i0.wp.com/www.tatvic.com/blog/wp-content/uploads/2016/12/Pic_4.png?resize=456%2C277" data-recalc-dims="1"></a></p>
<p>Here is the R code for simple scatter plot using function <strong>ggplot()</strong> with geom_point().</p>
<pre><strong><span><code>library(ggplot2)          // ggplot2 is an R library for visualizations train.
ggplot(train, aes(Item_Visibility, Item_MRP)) + geom_point() + scale_x_continuous(&quot;Item Visibility&quot;, breaks = seq(0,0.35,0.05))+ scale_y_continuous(&quot;Item MRP&quot;, breaks = seq(0,270,by = 30))+ theme_bw() </code></span></strong></pre>
<p>Now, we can view a third variable also in same chart, say a categorical variable (Item_Type) which will give the characteristic (item_type) of each data set. Different categories are depicted by way of different color for item_type in below chart.</p>
<p><a target="_blank" rel="nofollow" href="https://i1.wp.com/www.tatvic.com/blog/wp-content/uploads/2016/12/Pic_5.png"><img alt="R data visualization tutorial" class="wp-image-6457" src="https://i1.wp.com/www.tatvic.com/blog/wp-content/uploads/2016/12/Pic_5.png?resize=456%2C276" data-recalc-dims="1"></a></p>
<p>R code with an addition of category:</p>
<pre><strong><span><code>ggplot(train, aes(Item_Visibility, Item_MRP)) + geom_point(aes(color = Item_Type)) + 
  scale_x_continuous(&quot;Item Visibility&quot;, breaks = seq(0,0.35,0.05))+
  scale_y_continuous(&quot;Item MRP&quot;, breaks = seq(0,270,by = 30))+
  theme_bw() + labs(title=&quot;Scatterplot&quot;)
</code></span></strong></pre>
<p>We can even make it more visually clear by creating separate scatter plots for each separate Item_Type as shown below.<br>
<a target="_blank" rel="nofollow" href="https://i1.wp.com/www.tatvic.com/blog/wp-content/uploads/2016/12/Pic_6.png"><img alt="visualize data in R" class="wp-image-6458" src="https://i1.wp.com/www.tatvic.com/blog/wp-content/uploads/2016/12/Pic_6.png?resize=456%2C277" data-recalc-dims="1"></a><br>
R code for separate category wise chart:</p>
<pre><span><strong><code>ggplot(train, aes(Item_Visibility, Item_MRP)) + geom_point(aes(color = Item_Type)) + 
  scale_x_continuous(&quot;Item Visibility&quot;, breaks = seq(0,0.35,0.05))+
  scale_y_continuous(&quot;Item MRP&quot;, breaks = seq(0,270,by = 30))+ 
  theme_bw() + labs(title=&quot;Scatterplot&quot;) + facet_wrap( ~ Item_Type)
</code></strong></span></pre>
<p>Here, facet_wrap works superb &amp; wraps Item_Type in rectangular layout.</p>
<h3>2. Histogram</h3>
<p><strong>When to use</strong>: Histogram is used to plot continuous variable. It breaks the data into bins and shows frequency distribution of these bins. We can always change the bin size and see the effect it has on visualization.</p>
<p>From our mart dataset, if we want to know the count of items on basis of their cost, then we can plot histogram using continuous variable Item_MRP as shown below.</p>
<p><a target="_blank" rel="nofollow" href="https://i0.wp.com/www.tatvic.com/blog/wp-content/uploads/2016/12/Pic_7.png"><img alt="scatter plot in R" class="wp-image-6459" src="https://i0.wp.com/www.tatvic.com/blog/wp-content/uploads/2016/12/Pic_7.png?resize=456%2C274" data-recalc-dims="1"></a><br>
Here is the R code for simple histogram plot using function <strong>ggplot()</strong> with <strong>geom_histogram()</strong>.</p>
<pre><strong><span><code>ggplot(train, aes(Item_MRP)) + geom_histogram(binwidth = 2)+
  scale_x_continuous(&quot;Item MRP&quot;, breaks = seq(0,270,by = 30))+
  scale_y_continuous(&quot;Count&quot;, breaks = seq(0,200,by = 20))+
  labs(title = &quot;Histogram&quot;)
</code></span></strong></pre>
<h3>3. Bar &amp; Stack Bar Chart</h3>
<p><strong>When to use</strong>: Bar charts are recommended when you want to plot a categorical variable or a combination of continuous and categorical variable.</p>
<p>From our dataset, if we want to know number of marts established in particular year, then bar chart would be most suitable option, use variable Establishment Year as shown below.</p>
<p><a target="_blank" rel="nofollow" href="https://i1.wp.com/www.tatvic.com/blog/wp-content/uploads/2016/12/Pic_8.png"><img alt="Pic_8" class="wp-image-6460" src="https://i1.wp.com/www.tatvic.com/blog/wp-content/uploads/2016/12/Pic_8.png?resize=456%2C274" data-recalc-dims="1"></a></p>
<p>Here is the R code for simple bar plot using function ggplot() for a single continuous variable.</p>
<pre><strong><span><code>ggplot(train, aes(Outlet_Establishment_Year)) + geom_bar(fill = &quot;red&quot;)+theme_bw()+
  scale_x_continuous(&quot;Establishment Year&quot;, breaks = seq(1985,2010)) + 
  scale_y_continuous(&quot;Count&quot;, breaks = seq(0,1500,150)) +
  coord_flip()+ labs(title = &quot;Bar Chart&quot;) + theme_gray()
</code></span></strong></pre>
<h4><span>Vertical Bar Chart:</span></h4>
<p>As a variation, you can remove coord_flip() parameter to get the above bar chart vertically.<br>
<a target="_blank" rel="nofollow" href="https://i2.wp.com/www.tatvic.com/blog/wp-content/uploads/2016/12/Pic_9.png"><img alt="selecting the right chart type in R" class="wp-image-6461" src="https://i2.wp.com/www.tatvic.com/blog/wp-content/uploads/2016/12/Pic_9.png?resize=456%2C235" data-recalc-dims="1"></a></p>
<p>To know item weights (continuous variable) on basis of Outlet Type (categorical variable) on single bar chart, use following code:<br>
<strong><span><code>ggplot(train, aes(Item_Type, Item_Weight)) + geom_bar(stat = &quot;identity&quot;, fill = &quot;darkblue&quot;) + scale_x_discrete(&quot;Outlet Type&quot;)</code></span></strong><strong><span><code>+ scale_y_continuous(&quot;Item Weight&quot;, breaks = seq(0,15000, by = 500))</code></span></strong><strong><span><code>+ theme(axis.text.x = element_text(angle = 90, vjust = 0.5)) + labs(title = &quot;Bar Chart&quot;)</code></span></strong></p>
<h4><span>Stacked Bar chart:</span></h4>
<p>Stacked bar chart is an advanced version of bar chart, used for visualizing a combination of categorical variables.</p>
<p>From our dataset, if we want to know the count of outlets on basis of categorical variables like its type (Outlet Type) and location (Outlet Location Type) both, stack chart will visualize the scenario in most useful manner.<br>
<a target="_blank" rel="nofollow" href="https://i1.wp.com/www.tatvic.com/blog/wp-content/uploads/2016/12/Pic_10.png"><img alt="Visualizations in R" class="wp-image-6462" src="https://i1.wp.com/www.tatvic.com/blog/wp-content/uploads/2016/12/Pic_10.png?resize=456%2C235" data-recalc-dims="1"></a><br>
Here is the R code for simple stacked bar chart using function ggplot<strong>()</strong>.</p>
<pre><strong><span><code>ggplot(train, aes(Outlet_Location_Type, fill = Outlet_Type)) + geom_bar()+
labs(title = &quot;Stacked Bar Chart&quot;, x = &quot;Outlet Location Type&quot;, y = &quot;Count of Outlets&quot;)
</code></span></strong></pre>
<h3>4. Box Plot</h3>
<p><strong>When to use</strong>: Box Plots are used to plot a combination of categorical and continuous variables. This plot is useful for visualizing the spread of the data and detect outliers. It shows five statistically significant numbers- the minimum, the 25th percentile, the median, the 75th percentile and the maximum.</p>
<p>From our dataset, if we want to identify each outlet’s detailed item sales including minimum, maximum &amp; median numbers, box plot can be helpful. In addition, it also gives values of outliers of item sales for each outlet as shown in below chart.<br>
<a target="_blank" rel="nofollow" href="https://i0.wp.com/www.tatvic.com/blog/wp-content/uploads/2016/12/Pic_11.png"><img alt="best visualizations in R" class="wp-image-6463" src="https://i0.wp.com/www.tatvic.com/blog/wp-content/uploads/2016/12/Pic_11.png?resize=456%2C294" data-recalc-dims="1"></a></p>
<p>The black points are outliers. Outlier detection and removal is an essential step of successful data exploration.</p>
<p>Here is the R code for simple box plot using function ggplot<strong>()</strong> with geom_boxplot.</p>
<pre><strong><span><code>ggplot(train, aes(Outlet_Identifier, Item_Outlet_Sales)) + geom_boxplot(fill = &quot;red&quot;)+
scale_y_continuous(&quot;Item Outlet Sales&quot;, breaks= seq(0,15000, by=500))+
labs(title = &quot;Box Plot&quot;, x = &quot;Outlet Identifier&quot;)
</code></span></strong></pre>
<h3>5. Area Chart</h3>
<p><strong>When to use</strong>: Area chart is used to show continuity across a variable or data set. It is very much same as line chart and is commonly used for time series plots. Alternatively, it is also used to plot continuous variables and analyze the underlying trends.</p>
<p>From our dataset, when we want to analyze the trend of item outlet sales, area chart can be plotted as shown below. It shows count of outlets on basis of sales.<br>
<a target="_blank" rel="nofollow" href="https://i0.wp.com/www.tatvic.com/blog/wp-content/uploads/2016/12/Pic_12.png"><img alt=" R data visualization tutorial" class="wp-image-6464" src="https://i0.wp.com/www.tatvic.com/blog/wp-content/uploads/2016/12/Pic_12.png?resize=456%2C250" data-recalc-dims="1"></a><br>
Here is the R code for simple area chart showing continuity of Item Outlet Sales using function ggplot<strong>()</strong> with geom_area.</p>
<pre><strong><code><span>ggplot(train, aes(Item_Outlet_Sales)) + geom_area(stat = &quot;bin&quot;, bins = 30, fill = &quot;steelblue&quot;) + scale_x_continuous(breaks = seq(0,11000,1000))+ labs(title = &quot;Area Chart&quot;, x = &quot;Item Outlet Sales&quot;, y = &quot;Count&quot;)</span> 
</code></strong></pre>
<h3>6. Heat Map</h3>
<p><strong>When to use</strong>: Heat Map uses intensity (density) of colors to display relationship between two or three or many variables in a two dimensional image. It allows you to explore two dimensions as the axis and the third dimension by intensity of color.</p>
<p>From our dataset, if we want to know cost of each item on every outlet, we can plot heatmap as shown below using three variables Item MRP, Outlet Identifier &amp; Item Type from our mart dataset.</p>
<p><a target="_blank" rel="nofollow" href="https://i1.wp.com/www.tatvic.com/blog/wp-content/uploads/2016/12/Pic_13.png"><img alt="electing the right chart type in R" class="wp-image-6465" src="https://i1.wp.com/www.tatvic.com/blog/wp-content/uploads/2016/12/Pic_13.png?resize=456%2C236" data-recalc-dims="1"></a><br>
The dark portion indicates Item MRP is close 50. The brighter portion indicates Item MRP is close to 250.</p>
<p>Here is the R code for simple heat map using function ggplot().</p>
<pre><strong><code><span> ggplot(train, aes(Outlet_Identifier, Item_Type))+
geom_raster(aes(fill = Item_MRP))+
labs(title =&quot;Heat Map&quot;, x = &quot;Outlet Identifier&quot;, y = &quot;Item Type&quot;)+
scale_fill_continuous(name = &quot;Item MRP&quot;)</span> 
</code></strong></pre>
<h3>7. Correlogram</h3>
<p><strong>When to use</strong>: Correlogram is used to test the level of co-relation among the variable available in the data set. The cells of the matrix can be shaded or colored to show the co-relation value.</p>
<p>Darker the color, higher the co-relation between variables. Positive co-relations are displayed in blue and negative correlations in red color. Color intensity is proportional to the co-relation value.</p>
<p>From our dataset, let’s check co-relation between Item cost, weight, visibility along with Outlet establishment year and Outlet sales from below plot.</p>
<p>In our example, we can see that Item cost &amp; Outlet sales are positively correlated while Item weight &amp; its visibility are negatively correlated.<br>
<a target="_blank" rel="nofollow" href="https://i0.wp.com/www.tatvic.com/blog/wp-content/uploads/2016/12/Pic_14.png"><img alt=",data visualizations R" class="wp-image-6466" src="https://i0.wp.com/www.tatvic.com/blog/wp-content/uploads/2016/12/Pic_14.png?resize=456%2C233" data-recalc-dims="1"></a></p>
<p>Here is the R code for simple correlogram using function <strong>corrgram()</strong>.</p>
<pre><strong><code><span>install.packages(&quot;corrgram&quot;)
library(corrgram)
corrgram(train, order=NULL, panel=panel.shade, text.panel=panel.txt,
main=&quot;Correlogram&quot;)</span> 
</code></strong></pre>
<p>Now I guess it should be easy for you to visualize the data using <strong>ggplot2</strong> library in R Programming.</p>
<p>Apart from visualizations, you can learn about data mining in R through our webinar recording on <a target="_blank" rel="nofollow" href="http://www.tatvic.com/webinar/r-google-analytics/">Google Analytics Data Mining with R</a> (includes 3 Real Applications)</p>
<p>To know more or for any assistance on R programming, please drop us a comment with your details &amp; we will be glad to assist you!!</p>
<p>The post <a target="_blank" rel="nofollow" href="http://www.tatvic.com/blog/7-visualizations-learn-r-2/">7 Visualizations You Should Learn in R</a> appeared first on <a target="_blank" rel="nofollow" href="http://www.tatvic.com/blog">Tatvic Blog</a>.</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.tatvic.com/blog/7-visualizations-learn-r-2/"> Tatvic Blog » R</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/vADO7Pk4iMw"></a></div>