본문 바로가기
R

(R) geom_violin() - a density graph

by jangpiano 2020. 9. 9.
반응형

<geom_violin>


geom_violin is an efficient density graph. 

The violin graph is created by mirroring the kernel density curve symmetrically. 

It is usefully used for comparing several density graphs without overlapping. 


geom_violin()  

 geom_density()

 > ggplot(data=birthwt,aes(x=smoke,y=bwt))+geom_violin()

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

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



 

library(MASS)

>install.packages(plyr)

> table(birthwt$race)


 1  2  3 

96 26 67 

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

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

> table(birthwt$race)


 white  black others 

    96     26     67 


library(MASS)

>install.packages(plyr)

> table(birthwt$smoke)


  0   1 

115  74 

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

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

> table(birthwt$smoke)


No smoke    smoke 

     115       74 


bwt: birth weight in grams. 


> ggplot(data=birthwt,aes(x=smoke,y=bwt))+geom_violin()

 > ggplot(data=birthwt,aes(x=smoke,y=bwt))+geom_violin(trim=FALSE)

 



> ggplot(data=birthwt,aes(x=smoke,y=lwt))+geom_violin()+geom_boxplot(fill="black")


You should take account that geom_violin() must be followed by geom_boxplot() if you want to express both graphs. 

Otherwise, the violin graph will cover the boxplot. 

> ggplot(data=birthwt,aes(x=smoke,y=lwt))+geom_violin()+geom_boxplot(fill="black",width=0.1)

> ggplot(data=birthwt,aes(x=smoke,y=lwt))+geom_violin()+geom_boxplot(fill="black",width=0.1)+stat_summary(fun=median,geom="point",fill="white",shape=21,size=3)

<from density to count - geom_violin(scale="count")>


Violin graph is a great substitute for other density functions. 

However, it can also express count instead of density if you set 'scale="count"' in geom_violin()


> ggplot(data=birthwt,aes(x=smoke,y=bwt))+geom_violin(scale="count")

<The way to adjust smoothness>


As you can see below, whether having adjust=1 or not does not affect the smoothness of the graph. 

It means that adjust for the density graph is originally set as 1. 


> ggplot(data=birthwt,aes(x=smoke,y=bwt))+geom_violin(adjust=1) 

 > ggplot(data=birthwt,aes(x=smoke,y=bwt))+geom_violin()

 

 


 > ggplot(data=birthwt,aes(x=smoke,y=bwt))

+geom_violin(adjust=0.5)

 > ggplot(data=birthwt,aes(x=smoke,y=bwt))

+geom_violin()

 > ggplot(data=birthwt,aes(x=smoke,y=bwt))

+geom_violin(adjust=2)

 

 

 






반응형

'R' 카테고리의 다른 글

(R) Central limit theorem  (0) 2020.09.27
(R) Normal approximation to Binomial  (0) 2020.09.27
stat_summary  (0) 2020.09.09
(R) The way to interpret boxplot in R  (0) 2020.09.07
(R) comparing several kernal density curves  (0) 2020.09.04