Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prolong the tails on the density plot in ggplot2

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)?

like image 361
Helios Avatar asked Oct 20 '25 15:10

Helios


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

like image 173
Ben Avatar answered Oct 22 '25 03:10

Ben