In this case, this is not a density calculation, but the other one uses the log10 transformation.
First check the density with similar ones without conversion
library(ggplot2) library(fueleconomy) d <- density(vehicles$cty, from=min(vehicles$cty), to=max(vehicles$cty)) ggplot(data.frame(x=d$x, y=d$y), aes(x=x, y=y)) + geom_line() ggplot(vehicles, aes(x=cty)) + stat_density(geom="line")
So the problem seems to be a conversion. In stat_density below, it seems if the log10 transformation is applied to the variable x before calculating the density. Thus, in order to reproduce the results manually, you must convert the variable before calculating the density. For instance,
d2 <- density(log10(vehicles$cty), from=min(log10(vehicles$cty)), to=max(log10(vehicles$cty))) ggplot(data.frame(x=d2$x, y=d2$y), aes(x=x, y=y)) + geom_line() ggplot(vehicles, aes(x=cty)) + stat_density(geom="line") + scale_x_log10()
PS: To see how ggplot prepares data for density, you can look at the code as.list(StatDensity) , which leads to StatDensity$compute_group - ggplot2:::compute_density
source share