# Descriptive Statistics and Graph
###### tags: `R` `descriptive statistics` `graph`
## Descriptive Statistics
```r=
# set up data
x <- c(3, 17, 19, 4, 5, 1, 28, 6, 13, 8, 34, 8, 23, 12)
x
mean(x)
median(x)
sd(x)
quantile(x)
range(x)
# Testing normality
shapiro.test(x)
```
## Graph (ggplot2)
```r=
library(ggplot2)
df <- data.frame(
sex=factor(rep(c("F", "M"), each=200)),
weight=round(c(rnorm(200, mean=55, sd=15))),
height=round(c(rnorm(200, mean=165, sd=10)))
)
head(df)
# Basic histogram
ggplot(data=df, aes(x=weight)) + geom_histogram()
# Change the width of bins
ggplot(data=df, aes(x=weight)) +
geom_histogram(binwidth=1)
# Basic Bar chart
ggplot(data= df, aes(x= sex)) + geom_bar()
# Basic dotplot
ggplot(data=df, aes(x = sex, y = height)) +
geom_dotplot(binaxis='y', stackdir='center', stackratio=2.5, dotsize=0.2)
# Basic boxplot
ggplot(data=df, aes(x = sex, y = weight)) + geom_boxplot()
```