-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathintro_2_ggplot2.Rmd
191 lines (141 loc) · 3.44 KB
/
intro_2_ggplot2.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
---
title: "tidyverse: ggplot2"
author: 郭耀仁
output:
revealjs::revealjs_presentation:
theme: black
highlight: zenburn
center: true
---
# ggplot2 套件
## 超級受歡迎
- 包含於 tidyverse 之中
- 視覺化的文法
- 將變數指定在 `aes()` 之中
```{r eval=FALSE}
ggplot(df, aes()) +
geom_xxx()
```
## 基本的統計圖形
- 散佈圖:`geom_point()`
- 線圖:`geom_line()`
- 直方圖:`geom_histogram()`
- 盒鬚圖:`geom_boxplot()`
- 長條圖:`geom_bar()`
## 繪圖前先(安裝)載入這些套件
```{r eval=FALSE}
install.packages(c("tidyverse", "gapminder"))
```
```{r message=FALSE}
library(tidyverse)
library(gapminder)
```
## 散佈圖
```{r}
gapminder_2007 <- gapminder %>%
filter(year == 2007)
scatter <- ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp)) +
geom_point() +
theme_minimal()
```
---
```{r}
scatter
```
## 線圖
```{r}
north_asia <- gapminder %>%
filter(country %in% c("China", "Japan", "Taiwan", "Korea, Rep."))
line_plot <- ggplot(north_asia, aes(x = year, y = gdpPercap, colour = country)) +
geom_line() +
theme_minimal()
```
---
```{r}
line_plot
```
## 直方圖
```{r}
histogram <- ggplot(gapminder_2007, aes(x = gdpPercap)) +
geom_histogram(bins = 20) +
theme_minimal()
```
---
```{r}
histogram
```
## 盒鬚圖
```{r}
box_plot <- ggplot(gapminder_2007, aes(x = continent, y = gdpPercap)) +
geom_boxplot() +
theme_minimal()
```
---
```{r}
box_plot
```
## 長條圖
```{r}
gdpPercap_2007_na <- gapminder %>%
filter(year == 2007 & country %in% c("China", "Japan", "Taiwan", "Korea, Rep."))
bar_plot <- ggplot(gdpPercap_2007_na, aes(x = country, y = gdpPercap)) +
geom_bar(stat = "identity") +
theme_minimal()
```
---
```{r}
bar_plot
```
# 隨堂練習
## 練習資料集
- [點此下載](https://storage.googleapis.com/learn-r-the-easy-way.appspot.com/hh.xlsx)
```{r eval=FALSE}
install.packages("readxl")
library(readxl)
hh <- read_excel("YOUR_EXCEL_PATH")
```
```{r echo=FALSE}
library(readxl)
hh <- read_excel("~/Downloads/hh.xlsx")
```
## 欄位定義
- district: 行政區
- hh: 戶數
- disposable_income_in_thousands: 可支配所得(新台幣千元)
- year: 年份
## 大安區、中正區與信義區的平均每戶可支配所得線圖
```{r echo=FALSE}
line_df <- hh %>%
filter(district %in% c("大安區", "中正區", "信義區")) %>%
mutate(avg_income = disposable_income_in_thousands * 1000 / hh)
ggplot(line_df, aes(x = year, y = avg_income, colour = district)) +
geom_line() +
theme_minimal() +
theme(text = element_text(family = 'Heiti TC Light'))
```
## 所有行政區的每戶可支配所得盒鬚圖
```{r echo=FALSE}
box_df <- hh %>%
mutate(avg_income = disposable_income_in_thousands * 1000 / hh)
ggplot(box_df, aes(x = district, y = avg_income)) +
geom_boxplot() +
theme_minimal() +
theme(text = element_text(family = "Heiti TC Light"))
```
## 平均每戶可支配所得的最新排名
```{r echo=FALSE}
bar_df <- hh %>%
filter(year == 2016) %>%
mutate(avg_income = disposable_income_in_thousands * 1000 / hh) %>%
arrange(avg_income)
district_levels <- as.character(bar_df$district)
bar_df$district <- factor(bar_df$district, levels = district_levels)
ggplot(bar_df, aes(x = district, y = avg_income)) +
geom_bar(stat = "identity") +
theme_minimal() +
theme(text = element_text(family = "Heiti TC Light")) +
coord_flip()
```
# 延伸閱讀
## 官方文件
http://ggplot2.tidyverse.org/reference/index.html