0

I'm trying to visualize how close my data is to Normal, but I'm having trouble using both stat_function and facet_grid together.

My first approach is here:

example<-mat.or.vec(50,2)
example[1:25,1]<-"Group 1"
example[26:50,1]<-"Group 2"
example[1:25,2]<-rnorm(25,0,1)
example[26:50,2]<-rnorm(25,3,2)
example<-as.data.frame(example)
colnames(example)<-c("group","score")
example$score<-as.numeric(as.character(example$score))

ggplot(example,aes(x=as.numeric(score),color=group,fill=group))+
  geom_histogram(binwidth = 0.5,alpha=0.5,aes(y=..density..))+
  stat_function(fun = dnorm, args = list(mean = 0, sd = 1))+
  stat_function(fun = dnorm, args = list(mean =3, sd =2))+
  guides(fill=guide_legend(title="Group"),color=FALSE)+
  facet_grid(group~.)+scale_fill_manual(labels=c("Treatment","Control"),values=c("blue","pink"))+
  scale_color_manual(values=c("blue","pink"))+
  xlab("Score")+
  ylab("Density")+
  scale_x_continuous(limits=c(-3,7))+
  theme(legend.background = element_blank(),
        legend.box.background = element_rect(size=0.5,color="black"),
        legend.key = element_blank(),
        legend.text=element_text(face="bold",color="black",size=15),
        legend.title = element_blank(),
        legend.key.width = unit(3,"line"),
        text = element_text(size = 15),
        axis.text.x = element_text(face = "bold", color = "black", size = 15),
        axis.text.y = element_text(face = "bold", color = "black", size = 15),
        axis.title = element_text(face="bold",color="black",size="15"),
        panel.background=element_blank(),panel.border = element_rect(colour = "black", fill=NA, size=1))

That returns graph which isn't quite what I'm looking for. I want the only the curve pertaining to each group to display in each facet. (I'd also love to get rid of those grey labels while we're at it.)

Any ideas? I also tried a single stat_function with mean=c(0,3) and sd=c(1,2), but that returns a multivariate Normal, which is definitely not what I want.

Thanks!

vrofeberg
  • 1
  • 1

0 Answers0