Prolong the tails on the density plot in ggplot2

Viewed 278

I am plotting the same density plot using base plotting system and ggplot2. Density plot in base plotting system has smooth tails:

d = density(iris$Sepal.Length) 
plot(d)

enter image description here

Density plot in ggplot2 has chopped tails:

library(ggplot2)
ggplot( iris, aes(x=Sepal.Length)) + geom_density()

enter image description here

Is there any way to force ggplot2 to plot density plot similar to base plotting system (with smooth tails)?

1 Answers

Store the density and use xlim to set range from it:

library(ggplot2)

d <- density(iris$Sepal.Length)

ggplot(iris, aes(x=Sepal.Length)) + 
  geom_density() + 
  xlim(range(d$x))

Plot

plot density with full tails

Related