>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)
'R' 카테고리의 다른 글
stat_summary (0) | 2020.09.09 |
---|---|
(R) The way to interpret boxplot in R (0) | 2020.09.07 |
(R) Histogram to Scale Histogram / Scale Histogram and Density curve/binwidth/adjust of geom_density() (0) | 2020.09.04 |
(R) Kernal density curve /geom_density()/geom_line(stat="density") (0) | 2020.09.03 |
(R) Ways to change the name of element in a variable / revalue()/ ifelse() (0) | 2020.09.01 |