본문 바로가기
R

(R) comparing several kernal density curves

by jangpiano 2020. 9. 4.
반응형

>library(plyr)


> birthwt$smoke<-factor(birthwt$smoke)

> birthwt$smoke<-revalue(birthwt$smoke, c("0"="NO smoke","1"="smoke"))

> table(birthwt$smoke)


NO smoke    smoke 

     115       74 

> ggplot(data=birthwt,aes(x=bwt))+geom_histogram(fill="white",colour="black")+facet_grid(vars(smoke))


> birthwt$race<-factor(birthwt$race)

> birthwt$race<-revalue(birthwt$race,c("1"="white","2"="black","3"="others"))

> table(birthwt$race)


 black others  white 

    26     67     96 


> ggplot(data=birthwt,aes(x=bwt))+geom_histogram(fill="white",colour="black")+facet_grid(vars(race))



This form is not appropriate for this case because there are different numbers of observations under title "No smoke" and "smoke".

So it seems inappropriate for this case to compare two categories by counting the number of observations. 

So I will present a method for comparing densities by making Kernal density curve. 


<Comparing several Kernal density curves by facet_grid() and ggplot(aes(fill=))>


> ggplot(data=birthwt,aes(x=bwt))+geom_density(fill="white",colour="black")+facet_grid(vars(smoke))

> ggplot(data=birthwt,aes(x=bwt,y=..density..))+geom_histogram(binwidth=200)+geom_density()+facet_grid(vars(smoke))

> ggplot(data=birthwt,aes(x=bwt,y=..density..))+geom_histogram(binwidth=200,fill="yellow",colour="grey")+geom_density()+facet_grid(vars(smoke))


> ggplot(data=birthwt,aes(x=bwt,colour=smoke))+geom_density()


> ggplot(data=birthwt,aes(x=bwt,fill=smoke))+geom_density(alpha=0.5)


> ggplot(data=birthwt,aes(x=bwt,fill=smoke))+geom_density(alpha=0.3)


> ggplot(data=birthwt,aes(x=bwt))+geom_density(fill="White",colour="black")+facet_grid(vars(race))


> ggplot(data=birthwt,aes(x=bwt,y=..density..))+geom_density()+geom_histogram()+facet_grid(vars(race))

> ggplot(data=birthwt,aes(x=bwt,y=..density..))+geom_density()+geom_histogram(binwidth=200,fill="yellow",colour="grey")+facet_grid(vars(race))


> ggplot(data=birthwt,aes(x=bwt,colour=race))+geom_density(alpha=0.3)

> ggplot(data=birthwt,aes(x=bwt,fill=race))+geom_density(alpha=0.3)





반응형