I am trying to plot 3 examples of the normal distribution, however ggplot appears to be recognising the path as one continuous one rather than one stratified by the 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 recognise the factors and plot with the 3 different colors? Rather than displaying one continuous path?
A reproducible example, using 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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.