title: Plot normal distribution onto data
body: <p>I have some lognormal data that I want to transform, then fit a normal distribution to. Here is a reproducible example with data that's not exactly lognormal, but it's close enough:</p>
  
  <pre><code># create some lognormal data
  dat &lt;- data.frame(x = c(runif(1000, 0, 1), runif(300, 1, 100), runif(100,100,1000)))
  </code></pre>
  
  <p>I can transform these data by <code>log10</code> and fit a normal distribution to them with <code>MASS::fitdistr</code>.</p>
  
  <pre><code># fit normal distribution to log-transformed values
  library(MASS)
  fit &lt;- fitdistr(log10(dat$x), densfun = "normal")
  fit$estimate # mean and SD of the normal distribution
  </code></pre>
  
  <p>Now I want to plot the data, and the distribution on top of it. I transform my data by log10, and plot the normal distribution with <code>stat_function</code>, but it's not fitting the data.</p>
  
  <pre><code># plot data and distribution
  ggplot(data = dat) +
    geom_histogram(mapping = aes(x = log10(x))) +
    stat_function(fun = dnorm, args = list(mean = fit$estimate[1], sd = fit$estimate[2], log = TRUE))
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/ujHyV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ujHyV.png" alt="enter image description here"></a></p>
  
  <p>Any pointers, as well as verification if I'm going about this correctly, would be very helpful. </p>
  
  <p>Lastly to get my x axis in displaying units of 10, 100, 1000, etc... should I use <code>scale_x_log10()</code>? What's a simple way to format the x axis?</p>