-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathch5.Rmd
276 lines (193 loc) · 4.71 KB
/
ch5.Rmd
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
---
title: "簡單的繪圖(ggplot2)"
author: "郭耀仁"
date: "`r Sys.Date()`"
output: slidy_presentation
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, results = 'hide', warning = FALSE, message = FALSE)
```
## 安裝與載入 `ggplot2`
- 套件的安裝與載入有什麼不同?
```
install.packages("ggplot2")
```
```{r}
library(ggplot2)
```
## 玩具資料(Toy datasets)
- 載入 `ggplot2` 之後會有多的玩具資料
```{r}
data()
```
## 散佈圖
- `ggplot() + geom_point()` 繪製散佈圖
```{r}
ggplot(cars, aes(x = speed, y = dist)) +
geom_point()
```
## 散佈圖(2)
- `+ ggtitle() + xlab() + ylab()` 編輯標題,X 軸名稱與 Y 軸名稱
```{r}
ggplot(cars, aes(x = speed, y = dist)) +
geom_point() +
ggtitle("Car speed vs. braking distance") +
xlab("Speed") +
ylab("Dist")
```
## 散佈圖(3)
- `+ geom_smooth(method = )` 加上 fit line
```{r}
ggplot(cars, aes(x = speed, y = dist)) +
geom_point() +
ggtitle("Car speed vs. braking distance") +
xlab("Speed") +
ylab("Dist") +
geom_smooth(method = "lm")
```
## 散佈圖(4)
- `aes()` 中加入 `color = `
```{r}
str(iris)
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point() +
ggtitle("Sepal.Length vs. Sepal.Width") +
xlab("Sepal length") +
ylab("Sepal width")
```
## 散佈圖(5)
- 練習使用 `ggplot2` 套件附的 `diamonds` 資料
- 畫出 `carat` 與 `price` 的散佈圖
```
library(ggplot2)
ggplot(___, aes(x = ___, y = ___)) +
geom_point()
```
## 線圖
- `ggplot() + geom_line()` 繪製線圖
```{r}
ggplot(cars, aes(x = speed, y = dist)) +
geom_point() +
geom_line()
```
## 線圖(2)
- 線圖通常用來視覺化時間序列
```{r}
head(economics)
ggplot(economics, aes(x = date, y = unemploy)) +
geom_line()
```
## 直方圖
- `ggplot() + geom_histogram()` 繪製直方圖
```{r}
ggplot(cars, aes(x = speed)) +
geom_histogram()
```
## 直方圖(2)
- 透過 `facet_wrap()` 納入類別變數的展開
```{r}
ggplot(iris, aes(x = Sepal.Length)) +
geom_histogram() +
facet_wrap(~ Species) # 加入 ncol = 1 垂直展開
```
## 直方圖(3)
- 練習使用 `ggplot2` 套件附的 `diamonds` 資料
- 畫出 `carat` 的直方圖
- 選擇一個適當的 `binwidth`
```
library(ggplot2)
ggplot(___, aes(x = ___)) +
geom_histogram(binwidth = ___)
```
## 盒鬚圖
- `ggplot() + geom_boxplot()` 繪製盒鬚圖
```{r}
ggplot(iris, aes(x = 1, y = Sepal.Length)) +
geom_boxplot()
```
## 盒鬚圖(2)
- `aes()` 指定 `x = ` 納入類別變數的展開
```{r}
ggplot(iris, aes(x = Species, y = Sepal.Length)) +
geom_boxplot()
```
## 盒鬚圖(3)
- 練習使用 `ggplot2` 套件附的 `diamonds` 資料
- 畫出不同 `cut` 的 `price` 的盒鬚圖
```
library(ggplot2)
ggplot(___, aes(x = ___, y = ___)) +
geom_boxplot()
```
## 函數圖
- `ggplot() + stat_function(fun = , geom = "line")`
```{r}
sin_df <- data.frame(x = c(-pi, pi))
sin_df
ggplot(sin_df, aes(x = x)) +
stat_function(fun = sin, geom = "line")
```
## 函數圖(2)
- 繪製自訂函數
```{r}
sigmoid_func <- function(x){
return(1 / (1 + exp(-x)))
}
sigmoid_df <- data.frame(x = c(-10, 10))
sigmoid_df
ggplot(sigmoid_df, aes(x = x)) +
stat_function(fun = sigmoid_func, geom = "line") +
geom_hline(yintercept = 0.5, lty = 2) +
geom_vline(xintercept = 0, lty = 2)
```
## 長條圖
- `ggplot() + geom_bar()` 繪製長條圖
```{r}
ggplot(mtcars, aes(gear)) +
geom_bar()
```
## 長條圖(2)
- `geom_bar(stat = "identity")` 可以呈現數值
```{r}
ggplot(mtcars, aes(x = row.names(mtcars), y = hp)) +
geom_bar(stat = "identity") + coord_flip()
```
## 長條圖(3)
- 練習使用 `ggplot2` 套件附的 `diamonds` 資料
- 看不同 `cut` 類型的數量
```
ggplot(___, aes(x = ___)) +
geom_bar()
```
## 在一個畫布上畫多個圖形
- 使用 `gridExtra` 套件來幫忙
- `grid.arrange()` 函數
```
install.packages("gridExtra")
```
```{r}
library(gridExtra)
gg1 <- ggplot(cars, aes(x = speed, y = dist)) + geom_point()
gg2 <- ggplot(iris, aes(x = Petal.Length, y = Petal.Width, colour = Species)) + geom_point()
grid.arrange(gg1, gg2, nrow = 2)
```
## `ggplotly()` 加入互動性
- 使用 `plotly` 套件的 `ggplotly()` 函數
```
install.packages("plotly")
```
```{r}
library(plotly)
static_gg <- ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, col = Species)) +
geom_point()
ggplotly(static_gg)
```
## 圖形或資料的互動性
- [D3.js](https://d3js.org/)
- [Plotly](https://plot.ly/)
- [Shiny](https://shiny.rstudio.com/)
## 文件
- 沒事多看文件,多看文件沒事
- <http://docs.ggplot2.org/current/index.html>
## 期中作業
- 用一個 2x2 的畫布練習使用 ggplot2 繪製任意四個圖形