title: How can I superimpose an arbitrary parametric distribution over a histogram using ggplot?
body: <p>How can I superimpose an arbitrary parametric distribution over a histogram using ggplot?</p>
  
  <p>I have made an attempt based on a <a href="http://www.statmethods.net/graphs/density.html" rel="nofollow noreferrer">Quick-R example</a>, but I don't understand where the scaling factor comes from. Is this method reasonable? How can I modify it to use ggplot?</p>
  
  <p>An example overplot the normal and lognormal distributions using this method follows:</p>
  
  <pre><code>## Get a log-normalish data set: the number of characters per word in "Alice in Wonderland"
  alice.raw &lt;- readLines(con = "http://www.gutenberg.org/cache/epub/11/pg11.txt", 
                         n = -1L, ok = TRUE, warn = TRUE,
                         encoding = "UTF-8")
  
  alice.long &lt;- paste(alice.raw, collapse=" ")
  alice.long.noboilerplate &lt;- strsplit(alice.long, split="\\*\\*\\*")[[1]][3]
  alice.words &lt;- strsplit(alice.long.noboilerplate, "[[:space:]]+")[[1]]
  alice.nchar &lt;- nchar(alice.words)
  alice.nchar &lt;- alice.nchar[alice.nchar &gt; 0]
  
  # Now we want to plot both the histogram and then log-normal probability dist
  require(MASS)
  h &lt;- hist(alice.nchar, breaks=1:50, xlab="Characters in word", main="Count")
  xfit &lt;- seq(1, 50, 0.1)
  
  # Plot a normal curve
  yfit&lt;-dnorm(xfit,mean=mean(alice.nchar),sd=sd(alice.nchar))
  yfit &lt;- yfit * diff(h$mids[1:2]) * length(alice.nchar) 
  lines(xfit, yfit, col="blue", lwd=2) 
  
  # Now plot a log-normal curve
  params &lt;- fitdistr(alice.nchar, densfun="lognormal")
  yfit &lt;- dlnorm(xfit, meanlog=params$estimate[1], sdlog=params$estimate[1])
  yfit &lt;- yfit * diff(h$mids[1:2]) * length(alice.nchar)
  lines(xfit, yfit, col="red", lwd=2)
  </code></pre>
  
  <p>This produces the following plot:
  <img src="https://i.stack.imgur.com/YgvCg.png" alt="Plot produced by the code above, showing a histogram of word length superimposed with a normal distribution curve and a log-normal distribution curve"></p>
  
  <p>To clarify, I would like to have counts on the y-axis, rather than a density estimate.</p>