如何使用ggplot在直方图上绘制密度?

时间:2021-07-03 09:02:03

标签: r ggplot2

这是我使用 geom_density(bw = "SJ") 在直方图上绘制密度的代码 但这条线没有出现在情节上。 我该如何解决?

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

0 个答案:

没有答案