<div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/ggplot2-mapping-vs-setting/"></div></div><div><a data-url="http://www.r-bloggers.com/ggplot2-mapping-vs-setting/" data-counturl="http://www.r-bloggers.com/ggplot2-mapping-vs-setting/" data-count="vertical" data-text="ggplot2: Mapping vs Setting" target="_blank" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<p><em>Guest post by Kirill Eremenko </em></p>
<p>Hi there!</p>
<p>My name is Kirill Eremenko and I teach R on <a target="_blank" href="http://click.linksynergy.com/fs-bin/click?id=eO5bByfCCNo&offerid=406329.4926&type=3&subid=0">Udemy</a>. In this quick article I wanted to share with you a valuable tip we cover off in my course <a target="_blank" href="http://click.linksynergy.com/fs-bin/click?id=eO5bByfCCNo&offerid=406329.5395&type=3&subid=0">R Programming A-Z™ on Udemy</a> (click <a target="_blank" href="http://click.linksynergy.com/fs-bin/click?id=eO5bByfCCNo&offerid=406329.5395&type=3&subid=0"><strong>here</strong></a> to get a 30% discount on the course).</p>
<p>Many R users are familiar with the ggplot2 package by Hadley Wickham. Though ggplot2 is extremely logical, and therefore easy to learn, there are certain challenges associated with getting your head even around this package. Today we will talk about one of these specific challenges: mapping vs setting aesthetics.</p>
<p>Let’s look at an example:</p>
<p><em>library(ggplot2)</em></p>
<p><em>p &lt;- ggplot(data=diamonds, aes(x=carat, y=price))</em></p>
<p><em>p + geom_point()</em></p>
<p>Here we’re visualizing the diamonds dataset, which comes with ggplot2. Running the code creates the following chart:</p>
<p><a target="_blank" href="http://i0.wp.com/www.r-bloggers.com/wp-content/uploads/2016/06/Rplot11.png"><img alt="Rplot11" class="wp-image-123078" src="http://i0.wp.com/www.r-bloggers.com/wp-content/uploads/2016/06/Rplot11.png?resize=450%2C338" data-recalc-dims="1"></a>This chart illustrates the sales prices (y axis) for over 50,000 diamonds over their weight in carats (x axis). Already an interesting peek into our data, however let’s now say that we want to make this visual even more insightful by adding colour to it.</p>
<p>How would we do that? Perhaps by adding a bit of code into the last line like this:</p>
<p><em>library(ggplot2)</em></p>
<p><em>p &lt;- ggplot(data=diamonds, aes(x=carat, y=price))</em></p>
<p><em>p + geom_point(aes(colour=clarity))</em></p>
<p>Here we are using colour to categorize the observations by the clarity of each diamond:</p>
<p><a target="_blank" href="http://i2.wp.com/www.r-bloggers.com/wp-content/uploads/2016/06/Rplot12.png"><img alt="Rplot12" class="wp-image-123079" src="http://i2.wp.com/www.r-bloggers.com/wp-content/uploads/2016/06/Rplot12.png?resize=450%2C338" data-recalc-dims="1"></a></p>
<p>Beautiful! As we see, ggplot2 has even automatically created a legend for us on the right.</p>
<p>Even without knowing anything about diamonds we can tell that the IF-graded clarity rocks sell at the highest price at a given size. This makes sense, because IF stands for internally flawless – only about 3% of diamonds ever receive this grading.</p>
<p>But we are getting side-tracked.</p>
<p>“What’s was so complicated about this?” – you might ask.</p>
<p>Indeed, all we did in that last bit of code is mapped the colour aesthetic of the plot to the clarity variable in our dataset. But let’s now look at a slightly different example. Let’s say we don’t want to use colour to help us see different categories – instead, we just want all of the observations to be red.</p>
<p>How would the code for such plot look? Maybe like this:</p>
<p><em>library(ggplot2)</em></p>
<p><em>p &lt;- ggplot(data=diamonds, aes(x=carat, y=price))</em></p>
<p><em>p + geom_point(aes(colour=“Red”))</em></p>
<p><em> <a target="_blank" href="http://i0.wp.com/www.r-bloggers.com/wp-content/uploads/2016/06/Rplot13.png"><img alt="Rplot13" class="wp-image-123080" src="http://i0.wp.com/www.r-bloggers.com/wp-content/uploads/2016/06/Rplot13.png?resize=450%2C338" data-recalc-dims="1"></a></em></p>
<p> </p>
<p>That looks about right… But is it?</p>
<p>The first thing that stands out is that the red doesn’t look exactly like red. It looks more orange than red.</p>
<p>The other unexpected thing you might notice is the legend on the right. Why is there a legend – we only wanted one colour, so there should not be a need for a legend. Right?</p>
<p>It all becomes clear if we try replace red with a different colour. Let’s say blue, for example:</p>
<p><em>library(ggplot2)</em></p>
<p><em>p &lt;- ggplot(data=diamonds, aes(x=carat, y=price))</em></p>
<p><em>p + geom_point(aes(colour=“Blue”))</em></p>
<p><a target="_blank" href="http://i2.wp.com/www.r-bloggers.com/wp-content/uploads/2016/06/Rplot14.png"><img alt="Rplot14" class="wp-image-123081" src="http://i2.wp.com/www.r-bloggers.com/wp-content/uploads/2016/06/Rplot14.png?resize=450%2C338" data-recalc-dims="1"></a></p>
<p>Now isn’t that interesting?</p>
<p>Nothing has changed about the chart – the only difference is that the legend now says “Blue”. However, the colour is still the same! What’s happening here?</p>
<p>What happened is that because of the way our code is structured ggplot is completely ignoring the meaning of the word “Blue” and rather using it for categorization of the points – just like it did with clarity in the earlier example.</p>
<p>But this makes no sense! We want ggplot to use the meaning of the word “Blue”.</p>
<p>The correct way to achieve that is like this:</p>
<p><em>library(ggplot2)</em></p>
<p><em>p &lt;- ggplot(data=diamonds, aes(x=carat, y=price))</em></p>
<p><em>p + geom_point(color=”Blue”)</em></p>
<p><a target="_blank" href="http://i0.wp.com/www.r-bloggers.com/wp-content/uploads/2016/06/Rplot15.png"><img alt="Rplot15" class="wp-image-123082" src="http://i0.wp.com/www.r-bloggers.com/wp-content/uploads/2016/06/Rplot15.png?resize=450%2C338" data-recalc-dims="1"></a></p>
<p>Now the colour is correct and there is no legend on the right. Everything is working, but let’s discuss in a little bit more detail what we changed.</p>
<p>First, we need to understand that any aesthetic in ggplot2 (such as colour, size, shape, etc.) can be used in two distinct ways in your plots:</p>
<p>Option 1 – you can use the aesthetic to reflect some properties of your data. For example, clarity of the diamonds, like we did in the first example. This is called MAPPING an aesthetic.</p>
<p>Option 2 – you can choose a certain value for an aesthetic. For example, make the colour blue for ALL points or make the shape a square for ALL points. This is called SETTING an aesthetic and the keyword here is ALL.</p>
<p>The choice between mapping and setting is a trade-off. When mapping you can convey more insights, whereas when setting you get more control of how your chart looks.</p>
<p>If we look at the code in our examples, we will see that difference is trivial. To map an aesthetic you need to use the aes() function, to set an aesthetic – simply omit the aes() altogether.</p>
<p>Compare:</p>
<p><em>p + geom_point(aes(colour=clarity))</em></p>
<p>Versus:</p>
<p><em>p + geom_point(colour=”Blue”)</em></p>
<p>The way I remembered it when I was learning R was that setting is a “simple” process – you just choose a value for everything. Whereas mapping is more “complex” – elements of your plot will end up having different visual properties. The aes() function is only needed to help out with the more “complex” mapping and not the “simple” setting <img alt=":-)" class="wp-smiley" src="http://i2.wp.com/www.r-bloggers.com/wp-includes/images/smilies/simple-smile.png?w=456" data-recalc-dims="1"></p>
<p><strong>Bonus</strong></p>
<p>We could have ended the article here. However, I know ggplot2 can get addictive so I’m going to throw in a little bonus. Let’s learn how we can specify certain colours which we want to be used in mapping.</p>
<p>Try this code:</p>
<p><em>library(ggplot2)</em></p>
<p><em>p &lt;- ggplot(data=diamonds, aes(x=carat, y=price))</em></p>
<p><em>p + geom_point(aes(colour=clarity)) +</em></p>
<p><em>scale_colour_manual(values=c(“Blue”,”Blue”,”Purple”,”Purple”,”Purple”,”Red”,”Red”,”Red”))</em></p>
<p><a target="_blank" href="http://i1.wp.com/www.r-bloggers.com/wp-content/uploads/2016/06/Rplot16.png"><img alt="Rplot16" class="wp-image-123083" src="http://i1.wp.com/www.r-bloggers.com/wp-content/uploads/2016/06/Rplot16.png?resize=450%2C338" data-recalc-dims="1"></a></p>
<p>As you can see, the colour on the plot is still representing clarity of the diamonds (refer to the legend on the right). This means that we are using Option 1: Mapping.</p>
<p>This might seem a bit complex, but it really isn’t. The only difference to the previous Mapping example is that this time we are also specifying the range of colours to which the aesthetic should be mapped to.</p>
<p>And one final trick for today – let’s add some transparency:</p>
<p><a target="_blank" href="http://i2.wp.com/www.r-bloggers.com/wp-content/uploads/2016/06/Rplot17.png"><img alt="Rplot17" class="wp-image-123084" src="http://i2.wp.com/www.r-bloggers.com/wp-content/uploads/2016/06/Rplot17.png?resize=450%2C338" data-recalc-dims="1"></a></p>
<p><em>library(ggplot2)</em></p>
<p><em>p &lt;- ggplot(data=diamonds, aes(x=carat, y=price))</em></p>
<p><em>p + geom_point(aes(colour=clarity), alpha=0.3) +</em></p>
<p><em>scale_colour_manual(values=c(“Blue”,”Blue”,”Purple”,”Purple”,”Purple”,”Red”,”Red”,”Red”))</em></p>
<p>Alpha sets how transparent the points will be on a scale from 0 to 1 (with 0 being most transparent). This allows us to better see points which are behind each other as well as to better identify clusters of observations.</p>
<p>I hope you enjoyed these quick tips on ggplot(). And if you did, then I welcome you into my course <a target="_blank" href="http://click.linksynergy.com/fs-bin/click?id=eO5bByfCCNo&offerid=406329.5395&type=3&subid=0">R Programming A-Z™</a> on Udemy which is full of R tips, tricks and hacks.</p>
<p>If you are just starting out into R this course will help you get over the steep learning curve while having fun and completing five meaningful real-life analytics projects. Don’t miss the opportunity to secure your spot in this 10+ hour monster course!</p>
<p>I look forward to seeing you inside,</p>
<p>Sincerely,</p>
<p>Kirill Eremenko</p>
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/Mm2nWW6g7S4">