<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/a-simple-histogram-and-why-you-need-to-practice-it/"></div></div><div><a data-url="https://www.r-bloggers.com/a-simple-histogram-and-why-you-need-to-practice-it/" data-counturl="https://www.r-bloggers.com/a-simple-histogram-and-why-you-need-to-practice-it/" data-count="horizontal" data-text="A simple histogram (and why you need to practice it)" 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://sharpsightlabs.com/blog/annotated-histogram/"> r-bloggers – SHARP SIGHT LABS</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In data science, before doing almost anything else, you need to know your data.</p>
<p>This is why visualization is one of the pillars of data-science: visualization allows you to <i>see</i> your data and “know” it in <a href="http://sharpsightlabs.com/blog/data-visualizations-are-tools-and-what-youre-building/">a way that your mind is wired for</a>.</p>
<p>(And, it’s why I emphasize <a href="http://sharpsightlabs.com/blog/start-with-data-visualization-manipulation/">mastering data visualization before almost anything else</a>.)</p>
<p>In practice, “knowing your data” typically begins by using data visualizations and summary statistics to examine <i>individual variables</i>. You need to ask and answer questions: <i>What’s in the variable? How is it distributed? What is the mean?</i> </p>
<p>For answering some of these questions about individual variables, there are few visualization techniques that are <a href="http://sharpsightlabs.com/blog/histogram-in-r-basic/">simpler, or more useful, than the histogram</a>.</p>
<p>Let’s take a look at a simple histogram in  that has a few extra details and annotations to provide some additional information.</p>
<h2>Code</h2>
<pre class="wp-code-highlight">

#-------------
# LOAD GGPLOT2
#-------------
library(ggplot2)



#--------------------------------------
# CREATE VARIABLE, NORMALLY DISTRIBUTED
#--------------------------------------

# set &quot;seed&quot; for random numbers
set.seed(42)

# create variable
xvar_rand_norm &lt;- rnorm(1000, mean = 5)



#--------------------------------
# CREATE DATA FRAME FROM VARIABLE
#--------------------------------
df.xvar &lt;- data.frame(xvar_rand_norm)



#---------------------------------
# CALCULATE MEAN
#  we'll use this in an annotation
#---------------------------------

xvar_mean &lt;- mean(xvar_rand_norm)



#-----------------------------------------------
# PLOT
#  Here, we're going to plot the histogram
#  We'll also add a line at the calculated mean
#  and also add an annotation to specify the 
#  value of the calculated mean
#-----------------------------------------------

ggplot(data = df.xvar, aes(x = xvar_rand_norm)) +
  geom_histogram() +
  geom_vline(xintercept = xvar_mean, color = &quot;dark red&quot;) +
  annotate(&quot;text&quot;, label = paste(&quot;Mean: &quot;, round(xvar_mean,digits = 2)), x = xvar_mean, y = 30, color = &quot;white&quot;, size = 5)



</pre>
<p> </p>
<h3>The output plot</h3>
<p><img sizes="(max-width: 700px) 100vw, 700px" alt="histogram-with-mean-line_2016-11-22" class="wp-image-2790" src="https://i0.wp.com/sharpsightlabs.com/wp-content/uploads/2016/11/histogram-with-mean-line_2016-11-22.png?resize=456%2C354" data-recalc-dims="1"></p>
<h2>How this code works</h2>
<p>This is a pretty straightforward histogram with the addition of a vertical line to indicate where the mean is () and a text annotation to indicate the value of the mean ().</p>
<p>In case you’re not familiar with how  works, I’ll run through it:</p>
<p>First, we’re loading the  package using . </p>
<p>Next, we’re using the  function to generate a set of normally distributed random numbers. If you look a little closer, we’re using the  parameter to specify that we want these normally distributed numbers to have a mean of .</p>
<p>After creating the variable itself, we’re using  to create a data frame. This newly created data frame has the name . Keep in mind that the prefix “” has no special meaning in . In fact, in contrast to some other programming languages like Python or Java, the “” has no special meaning at all;  treats it just like any other character. Ultimately, adding a prefix like “” is simply a personal naming convention I use to keep data organized. This can be useful in large projects.</p>
<p>Next, we calculate the mean. This is extremely straightforward. The  function calculates the mean. </p>
<p>Finally, we use  to plot all of this information. </p>
<p>We initially call the  function to initiate plotting.  essentially says “we’re going to plot something.” The  parameter indicates the exact data frame that we’ll plot; it says “we’re going to be plotting some variables that are inside of the data frame .”</p>
<p>The  function allows us to specify a variable mapping. The idea of “mapping variables to aesthetic attributes” is a critical part of the <a href="https://www.amazon.com/Grammar-Graphics-Statistics-Computing/dp/0387245448"> conceptual framework</a>, but it’s somewhat beyond the scope of this post. To put this simply, the code “” inside of the  function lets  know that you want to plot the variable , and that visually, it is to be plotted on the x-axis.</p>
<p>After specifying which variable that we’re going to include in the plot, on the next line,  specifies the type of plot we want to draw. Again, the concept of a “geom” (AKA “geometric object”) is slightly outside of the scope of this post, but to put it simply, a “geom” is simply a geometric object that we want to draw. So  indicates that we want to draw a histogram.</p>
<p> tells  to add a vertical line. We’re indicating exactly where to draw that line with the parameter . So here, we’re specifying that we want to draw a vertical line that intercepts the x-axis at , which we already calculated in a previous part of the code.  does exactly what it looks like: it sets the color of the line to dark red.</p>
<p>Lastly, we’re using  to add a text annotation. Essentially,  indicates the exact text to use in the annotation. You can see that we’re using ““, but then we’re using the  function to convert the value of our calculated mean () into a string of characters, which we’re also including in the annotation. So, the annotation ends ups as “” (assuming that you’ve run this code with the seed ).</p>
<h2>You need to master the histogram</h2>
<p>We’re using a few useful techniques in this visualization, but the critical piece that you really need to know is the first two lines of  code.</p>
<pre class="wp-code-highlight">
ggplot(data = df.xvar, aes(x = xvar_rand_norm)) +
  geom_histogram()
</pre>
<p> </p>
<p>I’ve been beating this drum for well over a year now, but this bears repeating: </p>
<p>The histogram is one of the plots that you need to master. You’ll use it constantly in <a href="http://sharpsightlabs.com/blog/data-analysis-example-r-supercars-part1/">analysis and reporting</a>. When you move on to <a href="http://sharpsightlabs.com/blog/quick-introduction-machine-learning-r-caret/">more advanced topics like machine learning</a>, you’ll need to use the histogram to <a href="http://sharpsightlabs.com/blog/data-analysis-machine-learning-example-1/">examine how your variables are distributed</a> (although you can also use it’s fraternal twin, the density plot).</p>
<p>Here’s what I mean by “master”: you should be able to write the code for a histogram “in your sleep”. </p>
<p>You should be able to write the code to create a histogram <i>with your eyes closed</i>. </p>
<h2>Fluency with the basics: a critical milestone</h2>
<p>And not just the histogram. </p>
<p>You need the same level of fluency with all of the <a href="http://sharpsightlabs.com/blog/how-to-create-a-scatterplot-in-r/">other primary tools of visualization like the scatterplot</a>, <a href="http://sharpsightlabs.com/blog/r-bar-chart-basic/">the bar chart</a>, the <a href="http://sharpsightlabs.com/blog/r-line-chart-and-datavis-process/">line chart</a>. You also need that level of fluency with the <a href="http://sharpsightlabs.com/blog/dplyr-intro-data-manipulation-with-r/">basic data wrangling techniques of </a> and .</p>
<h2>You need to practice</h2>
<p>Achieving that level of fluency isn’t hard, but most people never practice, so they never get there. </p>
<p>I want to make this clear: in order <a href="http://sharpsightlabs.com/blog/learn-r-data-science/">to master </a>, master data science, <a href="http://sharpsightlabs.com/blog/start-with-data-visualization-manipulation/">master data visualization</a>, and master machine learning, you <i>need to practice</i>.</p>
<p>In this regard, learning data science is much like learning a musical instrument: you need to practice. Ideally, you need to practice <i>every day</i>. </p>
<p>Sound like work? It is. But the <a href="http://sharpsightlabs.com/blog/data-scientists-become-more-valuable-2015/">rewards are profound</a>.</p>
<h2>Discover how to <i>rapidly master data science</i></h2>
<p>The big barrier is that most people don’t know how to practice writing code. </p>
<p>If you want to discover how to <i>practice</i> data science and rapidly master the techniques, then <b><a href="http://sharpsightlabs.com">sign up for the Sharp Sight Labs email list</a></b>.</p>
<p>In the near future, Sharp Sight Labs will be publishing a lot more material about the strategies and systems for practicing data science and achieving rapid results.</p>
<p>The post <a target="_blank" rel="nofollow" href="http://sharpsightlabs.com/blog/annotated-histogram/">A simple histogram (and why you need to practice it)</a> appeared first on <a target="_blank" rel="nofollow" href="http://sharpsightlabs.com/">SHARP SIGHT LABS</a>.</p>
<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/a-simple-histogram-and-why-you-need-to-practice-it/"></div></div><div><a data-url="https://www.r-bloggers.com/a-simple-histogram-and-why-you-need-to-practice-it/" data-counturl="https://www.r-bloggers.com/a-simple-histogram-and-why-you-need-to-practice-it/" data-count="vertical" data-text="A simple histogram (and why you need to practice it)" 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://sharpsightlabs.com/blog/annotated-histogram/"> r-bloggers – SHARP SIGHT LABS</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/DquVOzrRIGs"></a></div>