barplot_or_boxplot: An Example for Choosing 'barplot' or 'boxplot'

Description Usage See Also Examples

Description

An exmaple.

Usage

1

See Also

bartlett.test, leveneTest, LeveneTest, dunn.test

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
library(R11)
# barplot_or_boxplot()
a<-1:5
b<-2:14
c<-c(1,5,6,15,25,14,20)
a<-as.numeric(a)
b<-as.numeric(b)
x<-c(a,b,c)
g <- c('group1', 'group2', 'group3')
group<-rep(g, c(5,13,7))
dat<-data.frame(x,group)
View(dat)

tapply(dat$x, dat$group, shapiro.test) # Normal distribution test

##-----------------------------------------
#     Test of Homogeneity of Variances
##-----------------------------------------

bartlett.test(dat$x~dat$group) 

# Levene's Test (center = median)
library(car)
leveneTest(dat$x, dat$group) # P-value>0, then boxplot

boxplot(dat$x~dat$group, ylim=c(0,30))

##-------------------------------------------------
#     Significance test and its post-hoc test
##-------------------------------------------------

kruskal.test(dat$x~dat$group)

library(dunn.test)
# Dunn's test of multiple comparisons using rank sums
dunn.test(x=dat$x, g = dat$group,list = T)

tapply(dat$x, dat$group, median)

# label significance followed dunn.test results 

text(1, max(dat$x[dat$group=="group1"])+1,"b")
text(2, max(dat$x[dat$group=="group2"])+1,"a")
text(3, max(dat$x[dat$group=="group3"])+1,"a")

##-------------------------------------------------
#        Alternatively, use ggplot2
##------------------------------------------------- 

library(ggplot2)
cols<-c("grey50", "blue", "red")

p<- ggplot(data=dat,mapping = aes(x=dat$group, y=dat$x))+
       geom_boxplot(fill=cols, alpha=0.7)+
       ylim(0,30)+
       xlab('Groups')+ylab('x value')+ theme_bw()
p

p + geom_jitter()

p + annotate(geom="text", x=1, y=max(dat$x[dat$group=="group1"])+1, label="b", color="black") + 
    annotate(geom="text", x=2, y=max(dat$x[dat$group=="group2"])+1, label="a", color="black") + 
    annotate(geom="text", x=3, y=max(dat$x[dat$group=="group3"])+1, label="a", color="black")
    
# More links
# http://www.sthda.com/english/wiki/ggplot2-texts-add-text-annotations-to-a-graph-in-r-software
# http://t-redactyl.io/blog/2016/04/creating-plots-in-r-using-ggplot2-part-10-boxplots.html

PhDMeiwp/R11 documentation built on May 20, 2019, 4:25 p.m.