<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/pretty-histograms-with-ggplot2/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://drsimonj.svbtle.com/pretty-histograms-with-ggplot2"> blogR</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://twitter.com/drsimonj">@drsimonj</a> here to make pretty histograms with ggplot2!</p>
<p>In this post you’ll learn how to create histograms like this:</p>
<p><a target="_blank" rel="nofollow" href="https://i0.wp.com/svbtleusercontent.com/woy7lw0s9fdn0q.jpg?ssl=1"><img alt="init-example-1.jpg" src="https://i1.wp.com/svbtleusercontent.com/woy7lw0s9fdn0q_small.jpg?w=456&ssl=1" data-recalc-dims="1"></a></p>
<h2><a target="_blank" rel="nofollow" href="http://drsimonj.svbtle.com/pretty-histograms-with-ggplot2#the-data_2"> </a>The data</h2>
<p>Let’s simulate data for a continuous variable <code>x</code> in a data frame <code>d</code>:</p>
<pre><code>set.seed(070510)
d &lt;- data.frame(x = rnorm(2000))

head(d)
#>            x
#> 1  1.3681661
#> 2 -0.0452337
#> 3  0.0290572
#> 4 -0.8717429
#> 5  0.9565475
#> 6 -0.5521690
</code></pre>
<h2><a target="_blank" rel="nofollow" href="http://drsimonj.svbtle.com/pretty-histograms-with-ggplot2#basic-histogram_2"> </a>Basic Histogram</h2>
<p>Create the basic ggplot2 histogram via:</p>
<pre><code>library(ggplot2)

ggplot(d, aes(x)) +
    geom_histogram()
</code></pre>
<p><a target="_blank" rel="nofollow" href="https://i1.wp.com/svbtleusercontent.com/aadby5nilzba.jpg?ssl=1"><img alt="basic-1.jpg" src="https://i2.wp.com/svbtleusercontent.com/aadby5nilzba_small.jpg?w=456&ssl=1" data-recalc-dims="1"></a></p>
<h2><a target="_blank" rel="nofollow" href="http://drsimonj.svbtle.com/pretty-histograms-with-ggplot2#adding-colour_2"> </a>Adding Colour</h2>
<p>Time to jazz it up with colour! The method I’ll present was motivated by my answer to <a target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/43795211/coloring-a-geom-histogram-by-gradient/43795338#43795338">this StackOverflow question</a>.</p>
<p>We can add colour by exploiting the way that ggplot2 stacks colour for different groups. Specifically, we fill the bars with the same variable (<code>x</code>) but <code>cut</code> into multiple categories:</p>
<pre><code>ggplot(d, aes(x, fill = cut(x, 100))) +
    geom_histogram()
</code></pre>
<p><a target="_blank" rel="nofollow" href="https://i0.wp.com/svbtleusercontent.com/9isqevvlkyog8q.jpg?ssl=1"><img alt="color1-1.jpg" src="https://i0.wp.com/svbtleusercontent.com/9isqevvlkyog8q_small.jpg?w=456&ssl=1" data-recalc-dims="1"></a></p>
<p>What the…</p>
<p>Oh, ggplot2 has added a legend for each of the 100 groups created by cut! Get rid of this with <code>show.legend = FALSE</code>:</p>
<pre><code>ggplot(d, aes(x, fill = cut(x, 100))) +
    geom_histogram(show.legend = FALSE)
</code></pre>
<p><a target="_blank" rel="nofollow" href="https://i0.wp.com/svbtleusercontent.com/ijw4ryjnm6ubw.jpg?ssl=1"><img alt="color2-1.jpg" src="https://i2.wp.com/svbtleusercontent.com/ijw4ryjnm6ubw_small.jpg?w=456&ssl=1" data-recalc-dims="1"></a></p>
<p>Not a bad starting point, but say we want to tweak the colours.</p>
<p>For a continuous colour gradient, a simple solution is to include <code>scale_fill_discrete</code> and play with the range of hues. To get your colours right, get familiar with the <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Hue">hue scale</a>.</p>
<p>For example, here we’ll tweak the colours to range from blue to red:</p>
<pre><code>ggplot(d, aes(x, fill = cut(x, 100))) +
  geom_histogram(show.legend = FALSE) +
  scale_fill_discrete(h = c(240, 10))
</code></pre>
<p><a target="_blank" rel="nofollow" href="https://i0.wp.com/svbtleusercontent.com/loip836kyyulaa.jpg?ssl=1"><img alt="color3-1.jpg" src="https://i2.wp.com/svbtleusercontent.com/loip836kyyulaa_small.jpg?w=456&ssl=1" data-recalc-dims="1"></a></p>
<p>Seems a little dark. Tweak chroma and luminance with <code>c</code> and <code>l</code>:</p>
<pre><code>ggplot(d, aes(x, fill = cut(x, 100))) +
  geom_histogram(show.legend = FALSE) +
  scale_fill_discrete(h = c(240, 10), c = 120, l = 70)
</code></pre>
<p><a target="_blank" rel="nofollow" href="https://i1.wp.com/svbtleusercontent.com/p7zyej4cwe8vdw.jpg?ssl=1"><img alt="color4-1.jpg" src="https://i0.wp.com/svbtleusercontent.com/p7zyej4cwe8vdw_small.jpg?w=456&ssl=1" data-recalc-dims="1"></a></p>
<h2><a target="_blank" rel="nofollow" href="http://drsimonj.svbtle.com/pretty-histograms-with-ggplot2#final-touches_2"> </a>Final touches</h2>
<p>The final touches are to set the theme, add labels, and a title:</p>
<pre><code>ggplot(d, aes(x, fill = cut(x, 100))) +
  geom_histogram(show.legend = FALSE) +
  scale_fill_discrete(h = c(240, 10), c = 120, l = 70) +
  theme_minimal() +
  labs(x = &quot;Variable X&quot;, y = &quot;n&quot;) +
  ggtitle(&quot;Histogram of X&quot;)
</code></pre>
<p><a target="_blank" rel="nofollow" href="https://i2.wp.com/svbtleusercontent.com/ydtdzeo1thmma.jpg?ssl=1"><img alt="touches-1.jpg" src="https://i2.wp.com/svbtleusercontent.com/ydtdzeo1thmma_small.jpg?w=456&ssl=1" data-recalc-dims="1"></a></p>
<p>Now have fun tweaking the colours!</p>
<pre><code>p &lt;- ggplot(d, aes(x, fill = cut(x, 100))) +
  geom_histogram(show.legend = FALSE) +
  theme_minimal() +
  labs(x = &quot;Variable X&quot;, y = &quot;n&quot;) +
  ggtitle(&quot;Histogram of X&quot;)

p + scale_fill_discrete(h = c(180, 360), c = 150, l = 80)
</code></pre>
<p><a target="_blank" rel="nofollow" href="https://i1.wp.com/svbtleusercontent.com/l2coulkxhcblvw.jpg?ssl=1"><img alt="tweak-1.jpg" src="https://i2.wp.com/svbtleusercontent.com/l2coulkxhcblvw_small.jpg?w=456&ssl=1" data-recalc-dims="1"></a></p>
<pre><code>
p + scale_fill_discrete(h = c(90, 210), c = 30, l = 50)
</code></pre>
<p><a target="_blank" rel="nofollow" href="https://i1.wp.com/svbtleusercontent.com/fsqq6ja1eb0leq.jpg?ssl=1"><img alt="tweak-2.jpg" src="https://i1.wp.com/svbtleusercontent.com/fsqq6ja1eb0leq_small.jpg?w=456&ssl=1" data-recalc-dims="1"></a></p>
<h2><a target="_blank" rel="nofollow" href="http://drsimonj.svbtle.com/pretty-histograms-with-ggplot2#sign-off_2"> </a>Sign off</h2>
<p>Thanks for reading and I hope this was useful for you.</p>
<p>For updates of recent blog posts, follow <a target="_blank" rel="nofollow" href="https://twitter.com/drsimonj">@drsimonj</a> on Twitter, or email me at <a target="_blank" rel="nofollow" href="mailto:drsimonjackson@gmail.com">drsimonjackson@gmail.com</a> to get in touch.</p>
<p>If you’d like the code that produced this blog, check out the <a target="_blank" rel="nofollow" href="https://github.com/drsimonj/blogR">blogR GitHub repository</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://drsimonj.svbtle.com/pretty-histograms-with-ggplot2"> blogR</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/0MGjFmPRaH8"></a></div>