Data
ToothGrowth
Shapiro-Wilk test
H0 : the variable follow a normal distribution
H1 : the variable NOT follow a normal distribution
shapiro.test(ToothGrowth$len)
##
## Shapiro-Wilk normality test
##
## data: ToothGrowth$len
## W = 0.96743, p-value = 0.1091
p-value > 0.05,Accept H0,the p values are not statistically significant (the variable follow a normal distribution)
Q-Q plot using ggplot2
library(ggplot2)
library(qqplotr)
##
## Attaching package: 'qqplotr'
## The following objects are masked from 'package:ggplot2':
##
## stat_qq_line, StatQqLine
ggplot(data = ToothGrowth, mapping = aes(sample = len)) +
stat_qq_band(fill="Purple",alpha = 0.15) +
stat_qq_line(col="Purple") +
stat_qq_point(col="black",size=1) +
labs(title = "Normal Q-Q plot",
x = "Theoretical Quantiles",
y = "Sample Quantiles")+theme_bw()
Density plot using ggplot2
ggplot(data=ToothGrowth, aes(x = len)) +
geom_density(alpha = 0.15,fill="Purple")+
labs(title = "Density plot of tooth length",
x = "Tooth length")+theme_bw()
Histogram plot using ggplot2
ggplot(data=ToothGrowth, aes(x=len)) +
geom_histogram(aes(x=len, y=..density..), bins=50,fill="Purple",alpha = 0.15) +
stat_function(fun=dnorm, args = list(mean=mean(ToothGrowth$len),
sd=sd(ToothGrowth$len)), color="Purple",size=1)+
labs(title = "Histogram plot of tooth length")+theme_bw()