简体   繁体   中英

R - ggplot2 plotting lognormal curve on histogram

So i have a set of data which follows the lognormal distribution, i have found the values for mu and sigma and am now trying to plot a histogram of the data alongside the PDF of the lognormal. However plotting the line seems to put it below 0 which doesnt seem to make sense, what am i doing wrong here, it seems like the line should be 5 y values higher.

Thanks M

h <- read.table("data.csv", header=TRUE, sep=",", row.names="id")
library("ggplot2")
d <- ggplot(data=h, aes(h$time)) + 
geom_histogram(binwidth = 0.8) + 
stat_function(fun = dlnorm, args = list(meanlog = 2.2, sdlog = 0.44, log = TRUE), colour = "red")
d

Graph Produced 在此处输入图片说明

To be able to make the comparision:

h <- data.frame(time = rlnorm(100, 2.2, .44))
library("ggplot2")
d <- ggplot(data=h) + 
  geom_histogram(binwidth = 0.8, aes(x = time, ..density..)) + 
  stat_function(fun = dlnorm, args = list(meanlog = 2.2, sdlog = 0.44), 
                colour = "red")
d

As suggested, turn log = TRUE off and then use ..density.. to get a frequency instead of a count.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM