Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Superimposing a log-normal density in ggplot and stat_function()

Tags:

r

ggplot2

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.

like image 202
Seb Avatar asked Sep 17 '12 17:09

Seb


1 Answers

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

like image 117
Sven Hohenstein Avatar answered Oct 24 '22 17:10

Sven Hohenstein