How can I plot the density instead of a line in ggplot2?

132 Views Asked by At

How can I make ggplot2::stat_function generate a density plot instead of geom = "line"?

My_table$Distances is numeric

mixmdl <- mixtools::normalmixEM(My_table2$Distances, k = 4)

plot_GMM(mixmdl,4)
mixmdl$posterior
x <- mixmdl$x
x <- data.frame(x)

GMMplot <- ggplot2::ggplot(data.frame(x)) +
  ggplot2::geom_density(ggplot2::aes(x), color="black",alpha = .2) +
  ggplot2::stat_function(geom = "line", fun = plot_mix_comps,args = list(mixmdl$mu[1], 
  mixmdl$sigma[1], lam = mixmdl$lambda[1]),colour = "blue1",alpha = 1.5) +
  xlab("distance (nm)")+theme_bw()+
  theme(axis.title.y=element_blank(),
  axis.text.y=element_blank(),
  axis.ticks.y=element_blank(),
  legend.position = "none")+
  ggplot2::stat_function(geom = "line",fun = plot_mix_comps,args = list(mixmdl$mu[2], mixmdl$sigma[2], lam = mixmdl$lambda[2]),colour = "blue2")+
  ggplot2::stat_function(geom = "line",fun = plot_mix_comps,args = list(mixmdl$mu[3], mixmdl$sigma[3], lam = mixmdl$lambda[3]),colour = "blue3")+
  ggplot2::stat_function(geom = "line",fun = plot_mix_comps,args = list(mixmdl$mu[4], mixmdl$sigma[4], lam = mixmdl$lambda[4]),colour = "blue4")+ylim(0.00,0.043)
GMMplot
0

There are 0 best solutions below