Building Normal Distributions

I am trying to build 3 examples of a normal distribution, however ggplot seems to recognize the path as one continuous and not one stratified by factor levels. I am relatively new to ggplot and any help would be greatly appreciated.

Here is my code:

set.seed(5872) x<-seq(-7.5,7.5,0.1) l<-length(x)*3 df<-data.frame(P=factor(rep(c("Mean: -1, SD: 0.5","Mean: 0, SD: 1","Mean: 1, SD: 1.5"), each=l) ), X=(c(x,x,x)), Y=(c(dnorm(x,-1,0.5),dnorm(x,0,1),dnorm(x,1,1.5)))) Normal<-ggplot(data=df,aes(X,Y,group=P,color=P))+ geom_path()+ scale_x_continuous("")+ scale_y_continuous("f(x)")+ scale_color_discrete("Parameters")+ ggtitle("Normal") + theme(plot.title = element_text(size=25,lineheight=.8, face="bold")) 

How can I get ggplot to recognize factors and build a plot with three different colors? Instead of displaying one continuous path?

+8
r normal-distribution ggplot2 distribution
source share
1 answer

A reproducible example using the hint from bdemarest:

  library(ggplot2) set.seed(5872) x<-seq(-7.5,7.5,0.1) l<-length(x) df<-data.frame(P=factor(rep(c("Mean: -1, SD: 0.5","Mean: 0, SD: 1","Mean: 1, SD: 1.5"), each=l) ), X=(c(x,x,x)), Y=(c(dnorm(x,-1,0.5),dnorm(x,0,1),dnorm(x,1,1.5)))) Normal<-ggplot(data=df,aes(X,Y,group=P,color=P))+ geom_path()+ scale_x_continuous("")+ scale_y_continuous("f(x)")+ scale_color_discrete("Parameters")+ ggtitle("Normal") + theme(plot.title = element_text(size=25,lineheight=.8, face="bold")) print(Normal) 
+2
source share

All Articles