5

I try to superimpose a function via stat_function() in ggplot but can't figure out my mistake. this example produces a nice looking plot:

data <- data.frame(x=rt(10000, df=7))

ggplot(data=data, aes(x=x)) + geom_histogram(aes(y = ..density..)) +
  stat_function(fun =dnorm, size=1, color='gray', args=list()) +
  opts(title="Histogram of interest rate changes") + theme_bw()

enter image description here

but when i try to superimpose a log-normal density this doesn't work as expected (or should I say as expected this doesn't work ;):

data <- data.frame(x=rf(10000, df1=7, df2=120))

ggplot(data=data, aes(x=x)) + geom_histogram(aes(y = ..density..)) +
 stat_function(fun =dnorm, size=1, color='gray', args=list(log=TRUE)) +
 opts(title="Histogram of interest rate changes") + theme_bw()

enter image description here

so here's my hopefully simple question: what am I doing wrong here? I guess this is a really simple problem I just don't see the answer - sorry.

Seb
  • 5,417
  • 7
  • 31
  • 50

1 Answers1

7

Use dlnorm, the density function of the log-normal distribution:

ggplot(data=data, aes(x=x)) + geom_histogram(aes(y = ..density..)) +
  stat_function(fun = dlnorm, size=1, color='gray') +
  opts(title="Histogram of interest rate changes") + theme_bw()

enter image description here

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
  • Just ran across your answer. I would appreciate, if you could take a look at my related question (in case you haven't seen it): http://stackoverflow.com/q/25598485/2872891. Thank you! – Aleksandr Blekh Sep 02 '14 at 21:17