I want to add multiple vertical lines in my density plot that start at the x-axis and end at the curve using ggplot2. I'm using the starwars dataset from dplyr. I want to plot the height variable as a normal distribution. The dashed lines inside the curve represent the standard deviations. So far I got this (just the plot without the lines):
sd.values = seq(66, 264, 34.77043)
zeros.vector = rep(0, 6)
ggplot(starwars, aes(x=height, y=dnorm(height, m=mean(height, na.rm=T), s=sd(height, na.rm=T)))) +
geom_line() + labs(x='height', y='f(height)') +
scale_x_continuous(breaks=sd.values,labels=sd.values)
density plot without lines
Now, I want to add the dashed lines using geom_segment
:
ggplot(starwars, aes(x=height, y=dnorm(height, m=mean(height, na.rm=T), s=sd(height, na.rm=T))))+
geom_line() + labs(x='height', y='f(height)') +
scale_x_continuous(breaks=sd.values, labels=sd.values) +
geom_segment((aes(x=sd.values, y=zeros.vector, xend=sd.values,
yend=dnorm(sd.values, m=mean(height, na.rm=T), s=sd(height, na.rm=T)))),
linetyp ='dashed')
But in the end, I only get the following error message:
Error: Aesthetics must be either length 1 or the same as the data (87): x, y, xend and yend
Any idea what I have to change in order to add the dashed lines?