-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathData_Visualization_updated.R
More file actions
309 lines (219 loc) · 6.24 KB
/
Copy pathData_Visualization_updated.R
File metadata and controls
309 lines (219 loc) · 6.24 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
## ----intro_read,message=FALSE,warning=FALSE,results='hide',echo=FALSE----
### Install packages if you don't have them yet
### Typical install:
# install.packages("ggplot2", dependencies = TRUE)
# install.packages("dplyr", dependencies = TRUE)
### Install personal copy (no admin rights)
# install.packages("ggplot2", lib="/path/to/myfolder")
# install.packages("dplyr", lib="/path/to/myfolder")
### Load packages
library(ggplot2)
library(stats)
library(base)
library(dplyr)
# setwd("SET THE WORKING DIRECTORY TO THE PATH TO THIS DIRECTORY")
### Load personal copy
# library(ggplot2, lib.loc="/path/to/myfolder")
# library(dplyr, lib.loc="/path/to/myfolder")
### Read in data
auto.data <- read.csv(
"./Datasets/auto/AutoData.csv",
header = TRUE
)
# tbl_df() isn't necessary here.
# It helps to display the data more clearly.
#
# In modern dplyr, use as_tibble() if you want
# the data to be explicitly converted to a tibble.
auto.data <- as_tibble(auto.data)
## ----intro_dataExamine,eval=FALSE----------------------------------------
# Find the dimensions
dim(auto.data)
# Look at the structure
str(auto.data)
# Examine the top
head(auto.data)
# Find out about a function
?str
## ----scatter_explot,echo=FALSE,out.width=".7\\linewidth"-----------------
ggplot(auto.data, aes(
x = curb.weight,
y = price
)) +
geom_point()
## ----aes_ex1_plot,echo=FALSE,out.width=".45\\linewidth",fig.show='hold'----
# Map color to a factor/categorical variable
ggplot(auto.data, aes(
x = curb.weight,
y = price,
color = factor(num.of.cylinders)
)) +
geom_point()
# Map color to a continuous variable
ggplot(auto.data, aes(
x = curb.weight,
y = price,
color = bore
)) +
geom_point()
## ----aes_tryout,eval=FALSE-----------------------------------------------
# Map size to horsepower
ggplot(auto.data, aes(
x = curb.weight,
y = price,
size = horsepower
)) +
geom_point()
# Map shape to drive wheels
ggplot(auto.data, aes(
x = curb.weight,
y = price,
shape = drive.wheels
)) +
geom_point()
## ----facet_ex,eval=FALSE-------------------------------------------------
ggplot(auto.data, aes(
x = curb.weight,
y = price
)) +
geom_point() +
facet_wrap(~aspiration)
## ----facet_plot,echo=FALSE,out.width=".7\\linewidth"---------------------
ggplot(auto.data, aes(
x = curb.weight,
y = price
)) +
geom_point() +
facet_wrap(~aspiration)
## ----grid_plot,echo=FALSE,out.width=".7\\linewidth"----------------------
ggplot(auto.data, aes(
x = curb.weight,
y = price
)) +
geom_point() +
facet_grid(drive.wheels ~ num.of.doors)
## ----try_out_facets,eval=FALSE-------------------------------------------
ggplot(auto.data, aes(
x = curb.weight,
y = price
)) +
geom_point() +
facet_grid(. ~ drive.wheels)
ggplot(auto.data, aes(
x = curb.weight,
y = price
)) +
geom_point() +
facet_grid(drive.wheels ~ .)
ggplot(auto.data, aes(
x = curb.weight,
y = price,
color = factor(num.of.doors)
)) +
geom_point() +
facet_grid(drive.wheels ~ .)
## ----scatter_geom_ex,eval=FALSE------------------------------------------
# Explicitly specify the point geometry
ggplot(auto.data, aes(
x = curb.weight,
y = price
)) +
geom_point()
## ----geom_hist_plot,out.width=".7\\linewidth",echo=FALSE,
## message=FALSE,warning=FALSE--------------------------------------------
# geom_histogram() operates with a single continuous variable.
# Let's look at price.
ggplot(auto.data, aes(x = price)) +
geom_histogram()
# Equivalent histogram using the default binning
ggplot(auto.data, aes(x = price)) +
geom_histogram()
## ----echo=FALSE,out.width=".6\\linewidth",
## message=FALSE,warning=FALSE--------------------------------------------
# Specify the histogram bin width
ggplot(auto.data, aes(x = price)) +
geom_histogram(binwidth = 20000)
## ----xlim_plot,echo=FALSE,out.width=".7\\linewidth",
## message=FALSE,warning=FALSE--------------------------------------------
# Note our price distribution is a bit skewed.
# Perhaps we are not interested in higher-priced (>= 20,000) cars.
# We can limit the plot to cars with lower prices.
ggplot(auto.data, aes(x = price)) +
geom_histogram(binwidth = 450) +
coord_cartesian(xlim = c(4000, 20000))
## ----hist_aes_plot,echo=FALSE,out.width=".45\\linewidth",
## message=FALSE,warning=FALSE,fig.show='hold'-----------------------------
# Map color to drive wheels
ggplot(auto.data, aes(
x = price,
color = drive.wheels
)) +
geom_histogram()
# Map fill to drive wheels
ggplot(auto.data, aes(
x = price,
fill = drive.wheels
)) +
geom_histogram()
## ----hist_facet_plot,echo=FALSE,out.width=".7\\linewidth",
## message=FALSE,warning=FALSE--------------------------------------------
ggplot(auto.data, aes(x = price)) +
geom_histogram() +
facet_wrap(~drive.wheels)
## ----hist_facet_scale_plot,echo=FALSE,out.width=".7\\linewidth",
## message=FALSE,warning=FALSE--------------------------------------------
# This helps us separate the categorical variables much more easily.
# Note that the counts vary quite a bit among the different classes,
# but the count axis is the same for all.
#
# We can change this by modifying the facet_wrap() call.
ggplot(auto.data, aes(x = price)) +
geom_histogram() +
facet_wrap(
~drive.wheels,
scales = "free_y"
)
## ----hist_facet_scale_plot3,echo=FALSE,out.width=".6\\linewidth",
## message=FALSE,warning=FALSE--------------------------------------------
ggplot(auto.data, aes(x = price)) +
geom_histogram() +
facet_wrap(
~drive.wheels,
scales = "free_y",
nrow = 3
)
## ----hist_density,eval=FALSE---------------------------------------------
# Density plot
ggplot(auto.data, aes(x = price)) +
geom_density()
# Histogram with density on top
#
# The modern ggplot2 syntax:
# after_stat(density)
#
# replaces the older:
# ..density..
ggplot(auto.data, aes(x = price)) +
geom_histogram(
aes(y = after_stat(density))
) +
geom_density()
# 2D density plot
ggplot(auto.data, aes(
x = height,
y = price
)) +
geom_density_2d()
# Scatter plot with 2D density contours
ggplot(auto.data, aes(
x = height,
y = price
)) +
geom_point() +
geom_density_2d()
## ----boxplot,eval=FALSE--------------------------------------------------
ggplot(auto.data, aes(
x = drive.wheels,
y = price
)) +
geom_boxplot()