<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-best-r-package-for-learning-to-think-about-visualization/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://sharpsightlabs.com/blog/r-package-think-about-visualization/"> 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>As a beginning data scientist, you’ll have quite a few subject areas that you need to learn (and <a href="http://sharpsightlabs.com/blog/master-r-obsolete/">eventually master</a>).</p>
<p>While you’ll certainly need to learn some math and statistics, math and stats are <b>not</b> the <a href="http://sharpsightlabs.com/blog/machine-learning-prerequisite-isnt-math/">first things I recommend</a> to most beginners.</p>
<p>Almost always, I recommend that people start with data visualization.</p>
<p>The reason for this, is that data visualization is so critical to almost every part of getting things done as a data scientist: reporting, analysis, exploratory analysis (e.g., <a href="http://sharpsightlabs.com/blog/data-analysis-machine-learning-example-1/">EDA prior to machine learning</a>). You need data visualization constantly. It’s <i>necessary</i> for nearly every data scientist at all levels.</p>
<p>Furthermore, I’ve argued that at junior levels of a data team job hierarchy, data visualization (when combined with data manipulation) is <i>sufficient</i> for being productive. If you’re a <i>junior</i> member of a data team, your core responsibilities may exclusively revolve around visualization (i.e., reporting, analysis, etc).</p>
<p>Because it’s necessary (and in some cases, sufficient) for productivity, it’s a skill that you need to master early.</p>
<h2> is the visualization tool I recommend</h2>
<p>Of course, the question is, what tool should you use for data visualization? </p>
<p>Long time readers of the Sharp Sight blog will know where I stand on this: I think that  is a best-in-class data visualization tool, and arguably, <i>the</i> best data visualization tool.</p>
<p>As it turns out, a recent 2016 survey by O’Reilly media also showed that  is the <a href="http://sharpsightlabs.com/blog/r-recommend-data-science/">most frequently used</a> data visualization tool among employed data scientists. This provides some evidence that suggests that <i>you</i> should learn it, if you want to get a job as a data scientist.</p>
<h2> teaches you how to think about visualization</h2>
<p>But setting aside the popularity of  and it’s usefulness as a baseline productivity tool, there’s a deep-seated reason why I am so assertive about suggesting :</p>
<p><u><b> teaches you to how to think about visualizing your data.</b></u></p>
<p>It teaches you how to think about visualization, because there are two deep principles that underly the syntax (and a third principle that sort of arises as a result of the first two).</p>
<h2>3 critical principles of visualization</h2>
<p>Two important data visualization principles are sort of hard-wired into the structure of :</p>
<ol>
<li>mapping data to aesthetics</li>
<li>layering</li>
</ol>
<p>There’s also a third principle that sort of arises as a result of layering:</p>
<ol start="3">
<li>building plots iteratively</li>
</ol>
<p>Understanding these will sharpen your intuition about how to visualize data and how to attack particular problems for which visual tools are a good solution.</p>
<p>To understand these principles, how they operate, and why they’re so important, let’s look at an example.</p>
<h3>Principle 1: Mapping Data to aesthetics</h3>
<p>Let’s say that we have a dataset:</p>
<pre class="wp-code-highlight">
#LOAD PACKAGE: tidyverse
library(tidyverse)

# This is the data we're going to plot ...
foo &lt;- c(-122.419416,-121.886329,-71.05888,-74.005941,-118.243685,-117.161084,-0.127758,-77.036871,116.407395,-122.332071,-87.629798,-79.383184,-97.743061,121.473701,72.877656,2.352222,77.594563,-75.165222,-112.074037,37.6173)
bar &lt;- c(37.77493,37.338208,42.360083,40.712784,34.052234,32.715738,51.507351,38.907192,39.904211,47.60621,41.878114,43.653226,30.267153,31.230416,19.075984,48.856614,12.971599,39.952584,33.448377,55.755826)
zaz &lt;- c(6471,4175,3144,2106,1450,1410,842,835,758,727,688,628,626,510,497,449,419,413,325,318)


# CREATE DATA FRAME
df.dummy &lt;- data_frame(foo,bar)


# INSPECT
glimpse(df.dummy)
head(df.dummy)
</pre>
<p>It has several numerical variables, so let’s <a href="http://sharpsightlabs.com/blog/how-to-create-a-scatterplot-in-r/">make a quick scatterplot</a> out of two of them,  and .</p>
<p><a target="_blank" rel="nofollow" href="http://sharpsightlabs.com/join-newsletter-ggplot2/"><br>
<img alt="" class="wp-image-2981" src="https://i1.wp.com/sharpsightlabs.com/wp-content/uploads/2017/01/1_basic_scatterplot_ggplot2.png?resize=437%2C394" data-recalc-dims="1"><br>
</a></p>
<p> </p>
<p>Seemingly, not much to see here, but the code to accomplish this is pretty straightforward (if you’ve learned the basic  syntax):</p>
<pre class="wp-code-highlight">
#-----------------------------------------------------------
# LOAD GGPLOT
# note: strictly speaking, we don't need to load this
#  since we already loaded &quot;tidyverse&quot;
#  however, this _is_ a blog post about ggplot2 after all ...
#-----------------------------------------------------------

library(ggplot2)

#----------
# PLOT DATA
#----------

ggplot(data = df.dummy, aes(x = foo, y = bar)) +
  geom_point()
</pre>
<p>Again, syntactically this is uncomplicated. Importantly though, underneath the syntax is a deep data visualization principle at work. </p>
<p>Once you get this principle, your understanding of data visualization will change forever (and you’ll become much more proficient with ).</p>
<p>When we create this chart, we’re actually <i><u>mapping data</u> to <u>aesthetic attributes</u></i>.</p>
<p>To explain what that means, let’s dissect the example a little bit.</p>
<p>The <i>points</i> in the scatterplot are “geometric objects” that we draw. In  lingo, the points are “geoms.” More specifically, the points are “point geoms” that we can denote syntactically with . </p>
<p>But all “geometric objects” have “aesthetic attributes.” Aesthetic attributes are things like:</p>
<ul>
<li>x-position</li>
<li>y-position</li>
<li>color</li>
<li>size</li>
<li>transparency</li>
</ul>
<p>When we create a data visualization in , we’re ultimately creating a <u>mapping</u> between variables in our data and the aesthetic attributes of the geometric objects in our visualization.</p>
<p>I’m going to repeat that, because it’s very important:</p>
<blockquote><p>
When we visualize data, we are <u>mapping</u> between the variables in our data and the aesthetic attributes of the geometric objects that we plot.
</p>
</blockquote>
<p>To bring this back to our simple scatterplot example, when we create this plot, we are mapping  to the x-position aesthetic, and we’re mapping  to the y-position aesthetic.</p>
<p> </p>
<p><a target="_blank" rel="nofollow" href="http://sharpsightlabs.com/join-newsletter-ggplot2/"><br>
<img sizes="(max-width: 763px) 100vw, 763px" alt="" class="wp-image-2990" src="https://i2.wp.com/sharpsightlabs.com/wp-content/uploads/2017/01/2_ggplot2-scatter_mapping-vars.png?resize=456%2C246" data-recalc-dims="1"><br>
</a></p>
<p> </p>
<h3>Mapping variables is a really important concept …</h3>
<p>I know what you’re thinking:</p>
<p> ” Yeah, I get it,<br>
… ‘foo’ on the x-axis and ‘bar’ on the y-axis.<br>
… I can do that in Excel. ”</p>
<p>Not so fast.</p>
<p>Understand: this is a simple example, but there’s a very deep principle at work here.</p>
<p>Theoretically, geometric objects (i.e., the things that we draw in a plot, like points) don’t just have attributes like x-position and y-position. As I mentioned above, geometric objects have a variety of other aesthetic attributes like transparency, color, size, etc. Moreover, if we can map variables to attributes like x-position and y-position, we should be able to map variables to attributes like color and size, right?</p>
<p>Yes. </p>
<p>… and this is exactly what  allows you to do.</p>
<p>Mapping variables to parts of your plot is not limited to the x and y axes in . This is where  begins to differentiate itself.</p>
<p> allows us to manipulate that larger set of aesthetic attributes like color, size, transparency, and shape.</p>
<p>More importantly, it allows us to <u>map variables</u> to essentially any of these aesthetics.</p>
<p>To show you this, let’s extend our example and create a bubble chart.</p>
<h3>Extended example: mapping a variable to size</h3>
<p>All we need to do is <u>map a new variable</u> to the  aesthetic.</p>
<pre class="wp-code-highlight">
#------------------------------------
# NEW PLOT
# - map a variable to size and replot
#------------------------------------

ggplot(data = df.dummy, aes(x = foo, y = bar)) +
  geom_point(aes(size = zaz))

</pre>
<p><a target="_blank" rel="nofollow" href="http://sharpsightlabs.com/join-newsletter-ggplot2/"><br>
<img alt="" class="wp-image-2983" src="https://i1.wp.com/sharpsightlabs.com/wp-content/uploads/2017/01/3_bubble-chart_ggplot2.png?resize=456%2C369" data-recalc-dims="1"><br>
</a></p>
<p> </p>
<p>What have we done here?</p>
<p>We’ve <i>transformed</i> the simple scatterplot <a href="http://sharpsightlabs.com/blog/bubble-chart-in-r-basic/">into a bubble chart</a> by mapping a new variable to the  aesthetic. </p>
<p> </p>
<p><a target="_blank" rel="nofollow" href="http://sharpsightlabs.com/join-newsletter-ggplot2/"><br>
<img sizes="(max-width: 776px) 100vw, 776px" alt="" class="wp-image-2984" src="https://i1.wp.com/sharpsightlabs.com/wp-content/uploads/2017/01/4_bubble-chart_mapping-vars.png?resize=456%2C244" data-recalc-dims="1"><br>
</a></p>
<p> </p>
<p>Let me say that again. We just changed a scatterplot to a bubble chart simply by <u>mapping a new variable to the size aesthetic</u>. </p>
<p>And it doesn’t end there. </p>
<p>As I already noted, there are other aesthetics to which you can map variables beyond , , and . (Other aesthetics for point geoms would include shape and transparency.)</p>
<p>In some simplistic sense, that’s all we’re really doing when we visualize data.</p>
<p>When we create a visualization, we’re ultimately creating a mapping from variables in the data to aesthetic attributes of the geometric objects that we draw.</p>
<p>It’s simple, but critical: any visualization you see can be deconstructed into geom specifications and mappings from data to the aesthetic attributes of those geometric objects.</p>
<p>That might not sound like a big deal, but once you “get it” – once you really understand what this means – your approach to visualizing data will be changed forever. You’ll look at <a href="http://sharpsightlabs.com/blog/mapping-seattle-crime/">more complex visualizations</a> and understand that that they are easy to produce, if you know what geom to specify and how to map your variables. Nearly all visualizations become much easier to produce.</p>
<h3>Principle 2: Build plots in layers</h3>
<p>In addition to learning to conceptualize visualizations as “mappings from data to aesthetics” there’s another principle you need to understand: building plots in layers.</p>
<p>The principle of <b>layering</b> is important because to create more advanced visualizations, you’ll often need to:</p>
<ul>
<li>Plot multiple datasets, or </li>
<li>Plot a dataset with additional contextual information that’s contained in a second dataset, or</li>
<li>Plot summaries or statistical transformations over the raw data</li>
</ul>
<p>To see what I mean, let’s modify the bubble chart that I just showed you above. </p>
<p>We’re going to:</p>
<ol>
<li>Get some additional information</li>
<li>Store it in a new data frame</li>
<li>Plot it as a new layer, underneath the bubbles</li>
</ol>
<pre class="wp-code-highlight">
#--------------------------
# GET ANOTHER LAYER OF DATA
#--------------------------
library(maps)
df.more_data &lt;- map_data(&quot;world&quot;)


# PLOT
ggplot(data = df.dummy, aes(x = foo, y = bar)) +
  geom_polygon(data = df.more_data, aes(x = long, y = lat, group = group)) +
  geom_point(aes(size = zaz), color = &quot;red&quot;) 
</pre>
<p>And this is what the new chart looks like:</p>
<p><a target="_blank" rel="nofollow" href="http://sharpsightlabs.com/join-newsletter-ggplot2/"><br>
<img sizes="(max-width: 807px) 100vw, 807px" alt="" class="wp-image-2985" src="https://i2.wp.com/sharpsightlabs.com/wp-content/uploads/2017/01/5_dot-distribution-map_first-itteration.png?resize=456%2C276" data-recalc-dims="1"><br>
</a></p>
<p>Are you starting to get it?</p>
<p>This is just the bubble chart from earlier in the post with a <u>new layer</u> added. That’s. It. </p>
<p>We just <i>transformed</i> a bubble chart into a new visualization called a “dot distribution map,” which is <a href="http://sharpsightlabs.com/blog/data-visualizations-are-tools-and-what-youre-building/">much more insightful</a> and much more visually interesting. </p>
<p>In the beginning of the post (when we created our dataset), I didn’t tell you that this is geospatial data. I didn’t tell you, because I wanted you to see that this dot distribution map is essentially the same as a bubble chart, with a new layer of contextual information plotted underneath the bubbles. </p>
<h3>Mapping and layering allow us to create complex charts</h3>
<p>Moreover, as we saw earlier, the bubble chart is just a modified scatter plot. It’s a scatterplot with an additional variable mapped to the  parameter.</p>
<p>So, this dot distribution map is just a bubble chart, and the bubble chart was just a scatterplot. </p>
<p>Ultimately, we used two of our data visualization principles – mapping and layering – in order to build this visualization from a scatter plot, to bubble chart, to the dot distribution map that we now see:</p>
<ol>
<li>To create the scatterplot, we <b>mapped</b> foo to the x-aesthetic and mapped bar to the y-aesthetic</li>
<li>To create the bubble chart, we <b>mapped</b> a new variable to the size-aesthetic</li>
<li>To create the dot distribution map, we added a <b>layer</b> of polygon data under the bubbles.</li>
</ol>
<p>Mapping and layering. That’s really the essence of it.</p>
<p>To be come great a data visualization, you need to understand <i>mapping variables to aesthetics</i> and <i>building plots in layers</i>.</p>
<p>These are two critical ideas that you need to understand, both technically (in order to write  code), but also <i>conceptually</i>. You need to start viewing data visualizations in this way. Once you do, you can begin deconstructing complex visualizations into simple, modular components.</p>
<p>Once you understand <i>mapping</i> and <i>layering</i>, you’ll begin to see that many “complex” visualizations are in fact, quite simple to make (if you know how to think about putting them together).</p>
<h3>Principle 3: iteration</h3>
<p>There’s actually a third principle at work here, that I haven’t mentioned yet: building plots iteratively.</p>
<p>This principle is only related to the syntax in a cursory way, but it does arise as a consequence of the  syntax. </p>
<p>Part of becoming a data scientist is not only learning syntax, but also learning workflow. You need to learn <i>processes</i>.</p>
<p>You won’t learn workflow directly when you learn  syntax, but learning visualization workflow is easier when you learn , primarily because of the “layerability” of the syntax. </p>
<p>Let me explain.</p>
<p>When we build plots in layers, we are ultimately <a href="http://sharpsightlabs.com/blog/r-line-chart-and-datavis-process/">building a plot iteratively</a>: we layer in new information, piece by piece, or modify existing parts of the plot, piece by piece.</p>
<p>As an example, let’s go back to the chart that we created above. </p>
<p>We ultimately created <a href="http://sharpsightlabs.com/blog/mapping-san-francisco-crime/">a dot distribution map</a>, but step-by-step, how did we actually build it?</p>
<p>We followed this basic process:</p>
<ol>
<li>Plotted a scatterplot by mapping variables to the  and  axes</li>
<li>We created a bubble chart by modifying the scatterplot. We essentially mapped a new variable to the “” aesthetic.</li>
<li>We layered in polygons to show the shape of the countries underneath the points.</li>
</ol>
<p>Ultimately, we can break down the creation of the dot distribution map into discrete steps. We built the map iteratively.</p>
<p>If we wanted to go further, we could continue to polish the map by performing additional steps:</p>
<ol start="4">
<li>Add a legend title</li>
<li>Modify the size scale</li>
<li>Modify the colors (note that even getting the colors perfect requires a lot of iterative, trial-and-error tinkering</li>
</ol>
<p>If we performed these last few steps, our work could ultimately <a href="http://sharpsightlabs.com/blog/mapping-vc-investment/">lead to a chart like this</a>:</p>
<p><a target="_blank" rel="nofollow" href="http://sharpsightlabs.com/join-newsletter-ggplot2/"><br>
<img sizes="(max-width: 1186px) 100vw, 1186px" alt="" class="wp-image-2986" src="https://i0.wp.com/sharpsightlabs.com/wp-content/uploads/2017/01/6_dot-distribution-map_finalized.png?resize=456%2C209" data-recalc-dims="1"><br>
</a></p>
<p>To a beginner, this finalized chart probably looks difficult to create. But, once you understand how to build a plot iteratively (in layers) it becomes easy.</p>
<p>My point is that ‘s syntactic <i>layerability</i> enables and rewards <i>iteration</i>. </p>
<p>The structure of the syntax sort of requires you to build plots in layers, and this in turn builds your intuition about iteration and data visualization <i>workflow</i>.</p>
<p>Ultimately, this knowledge about workflow is language-agnostic and transferable if you move to another tool.</p>
<h2>To learn how to think about visualization, learn </h2>
<p>This is why I think that  is truly a best-in-class data visualization tool, and the best tool to learn if you’re a beginner: </p>
<ol>
<li> makes complex visualizations relatively easy, by allowing you to break down complex visualizations into simple mappings and layers</li>
<li> enables, and in some sense encourages, iterative creation</li>
<li> trains you to how to think about visualization (i.e., it trains you to think about visualizations as mappings and layers, and encourages you to work iteratively)</li>
</ol>
<p> is an excellent tool for getting things done as a real world data scientist, but it also trains your mind <i>how to think about visualizing data</i>.</p>
<p>Now, I will admit that  has a bit of a learning curve when you first get started, but once you “get it,” data visualization becomes much easier.</p>
<p>So by learning , you are not just learning a toolkit. You also learn deep principles underlying data visualization. Once you learn these principles, your approach to visualizing data will change. Your <a href="http://sharpsightlabs.com/blog/data-analysis-example-r-supercars-part2/">ability to analyze data</a> and create sophisticated visualizations will improve <i>dramatically</i>.</p>
<p>In turn, by mastering visualization – a core, necessary skill – you’ll become a better data scientist. You’ll be better at getting things done. And when you want to move on to higher-level skills like advanced visualization or machine learning, you’ll have the foundation you need.</p>
<h2>Sign up to learn </h2>
<p>Discover how to rapidly learn  (and other critical R packages).</p>
<p><a href="http://sharpsightlabs.com/join-newsletter-ggplot2/">Sign up for our email list HERE</a>.</p>
<p>If you sign up, you’ll get <i>free</i> tutorials about  and other R tools, delivered to your inbox.</p>
<p>The post <a target="_blank" rel="nofollow" href="http://sharpsightlabs.com/blog/r-package-think-about-visualization/">The best R package for learning to “think about visualization”</a> appeared first on <a target="_blank" rel="nofollow" href="http://sharpsightlabs.com/">SHARP SIGHT LABS</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://sharpsightlabs.com/blog/r-package-think-about-visualization/"> 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/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/Tzd_-XZxqjE"></a></div>