DAY 18
0
Big Data

## Day18 R語言箱型圖(Box)

R語言中，我們可以用ggpolt2套件中的函數geom_boxplot()

http://docs.ggplot2.org/0.9.3.1/geom_boxplot.html

## 箱形圖初次見面

#使用長度先畫箱形圖
ggplot(ToothGrowth, aes(x = 0, y = len)) + geom_boxplot()

• 箱形的上方是Q3，下方是Q1
• 箱形高度就是ICQ= Q3-Q1
• 中間的線是中位數

## 分類

#X軸改用兩種給藥方式(柳橙汁(OG)或維生素C(VC))
ggplot(ToothGrowth, aes(x = supp, y = len)) + geom_boxplot(width = .5)

## 缺口效果

#加上缺口效果和平均數的點
ggplot(ToothGrowth, aes(x = supp, y = len)) + geom_boxplot(width = .5, notch = TRUE) +
stat_summary(fun.y = "mean", geom = "point", shape = 23, size = 3, fill = "blue")

## 結合點看分佈

#X加上點圖(DotPlot)
ggplot(ToothGrowth, aes(x = supp, y = len)) + geom_boxplot(width = .5) +
geom_dotplot(binaxis = "y", binwidth = .5, stackdir = "center")

## 加上註釋

#加上註釋(Text Annotations)
ggplot(ToothGrowth, aes(x = supp, y = len)) + geom_boxplot(width = .5) +
geom_dotplot(binaxis = "y", binwidth = .5, stackdir = "center") +
annotate("text", x = 1, y = 36, label = "喝柳橙汁") +
annotate("text", x = 2, y = 36, label = "吃維他命C")

## 上色

#boxplot再加上顏色(default color)
ggplot(ToothGrowth, aes(x = supp, y = len, fill = supp)) + geom_boxplot(width = .5) +
geom_dotplot(binaxis = "y", binwidth = .5, stackdir = "center") +
annotate("text", x = 1, y = 36, label = "喝柳橙汁") +
annotate("text", x = 2, y = 36, label = "吃維他命C")

2016.12.17攝於象山,台北

### 參考

http://docs.ggplot2.org/0.9.3.1/geom_boxplot.html
R Graphics cookbook

R語言與機器學習見面會30