<p><a 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 href="https://svbtleusercontent.com/woy7lw0s9fdn0q.jpg"></a></p>
<h2><a href="https://drsimonj.svbtle.com/#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 href="https://drsimonj.svbtle.com/#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 href="https://svbtleusercontent.com/aadby5nilzba.jpg"></a></p>
<h2><a href="https://drsimonj.svbtle.com/#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 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 href="https://svbtleusercontent.com/9isqevvlkyog8q.jpg"></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 href="https://svbtleusercontent.com/ijw4ryjnm6ubw.jpg"></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 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 href="https://svbtleusercontent.com/loip836kyyulaa.jpg"></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 href="https://svbtleusercontent.com/p7zyej4cwe8vdw.jpg"></a></p>
<h2><a href="https://drsimonj.svbtle.com/#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 href="https://svbtleusercontent.com/ydtdzeo1thmma.jpg"></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 href="https://svbtleusercontent.com/l2coulkxhcblvw.jpg"></a></p>
<pre><code>
p + scale_fill_discrete(h = c(90, 210), c = 30, l = 50)
</code></pre>
<p><a href="https://svbtleusercontent.com/fsqq6ja1eb0leq.jpg"></a></p>
<h2><a href="https://drsimonj.svbtle.com/#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 href="https://twitter.com/drsimonj">@drsimonj</a> on Twitter, or email me at <a 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 href="https://github.com/drsimonj/blogR">blogR GitHub repository</a>.</p>