0

here is my code to plot density on the histogram using geom_density(bw = "SJ") but the line does not appear on the plot. How can I fix it?

df %>%
  ggplot(aes(x=age, fill = as.factor(general_status)))+
  geom_histogram(
      bins = nclass.Sturges(age))+
  geom_density(bw = "SJ")+
  facet_grid(~general_status)+
  geom_vline(data = status_means, aes(xintercept = avg),  color="black", linetype="dashed")+
  geom_text(data = status_means , aes(x = avg, label = avg), y= Inf, vjust = 2, hjust = -0.1 )+
  ggtitle("Company age distribution by \nstatus") +
  coord_cartesian(ylim=c(0, 350000)) +
  scale_y_continuous(breaks=seq(0, 350000, 50000))+
  scale_fill_manual(values = c("Active" = "springgreen4", "Failed" = "firebrick3"))+
  theme(legend.position="None", strip.background = element_rect(colour="black",
                                        fill="white"))

enter image description here

  • Please make your question reproducible: add `dput(df)` to the question. – Peter Jul 03 '21 at 09:17
  • 3
    Your problem is almost certainly because your histoghram plots counts and is at a different scale to the density: the density isn't zero, but is "indistinguishable from zero" on the scale of the y-axis. – Limey Jul 03 '21 at 09:18
  • Does this answer your question? [Add density lines to histogram and cumulative histogram](https://stackoverflow.com/questions/13960896/add-density-lines-to-histogram-and-cumulative-histogram) – Limey Jul 03 '21 at 09:18

0 Answers0