-
Notifications
You must be signed in to change notification settings - Fork 48
/
Data_Visualization.R
198 lines (150 loc) · 5.11 KB
/
Data_Visualization.R
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
## ----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 = T)
# install.packages('dplyr', dependencies = T)
### Install personal copy (no admin rights)
# install.packages('gpplot2',lib="/path/to/myfolder")
# install.packages('dplyr',lib="/path/to/myfolder")
### Load packages
# Load packages
library(ggplot2)
library(stats)
library(base)
library(dplyr)
# setwd("SET THE Working Director 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
auto.data <- tbl_df(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"-----------------
qplot(curb.weight,price,data=auto.data)
## ----aes_ex1_plot,echo=FALSE,out.width=".45\\linewidth",fig.show='hold'----
# map color to factor/categorical variable
qplot(curb.weight,
price,
data=auto.data,
color=num.of.cylinders)
# map color to continuous variable
qplot(curb.weight,
price,
data=auto.data,
color=bore)
## ----aes_tryout,eval=FALSE-----------------------------------------------
qplot(curb.weight,
price,
data=auto.data,
size=horsepower)
qplot(curb.weight,
price,
data=auto.data,
shape=drive.wheels)
## ----facet_ex,eval=FALSE-------------------------------------------------
qplot(curb.weight,
price,
data=auto.data) + facet_wrap(~aspiration)
## ----facet_plot,echo=FALSE,out.width=".7\\linewidth"---------------------
qplot(curb.weight,
price,
data=auto.data) + facet_wrap(~aspiration)
## ----grid_plot,echo=FALSE,out.width=".7\\linewidth"----------------------
qplot(curb.weight,
price,
data=auto.data) +
facet_grid(drive.wheels~num.of.doors)
## ----try_out_facets,eval=FALSE-------------------------------------------
qplot(curb.weight,
price,
data=auto.data) + facet_grid(.~drive.wheels)
qplot(curb.weight,
price,
data=auto.data) + facet_grid(drive.wheels~.)
qplot(curb.weight,
price,
data=auto.data,
color=num.of.doors) + facet_grid(drive.wheels~.)
## ----scatter_geom_ex,eval=FALSE------------------------------------------
qplot(curb.weight,price,data=auto.data,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
# geom_histogram operates with a single continuous variable.
# Let's look at price
qplot(price,
data=auto.data,
geom='histogram')
# or via qplot's defaults
qplot(price,data=auto.data)
## ----echo=FALSE,out.width=".6\\linewidth",message=FALSE,warning=FALSE----
qplot(price,
data=auto.data,
geom='histogram',
binwidth=20000)
## ----xlim_plot,echo=FALSE,out.width=".7\\linewidth",message=FALSE,warning=FALSE,warning=FALSE----
# Note our price distribution is a bit skewed
# Perhaps we are not interested in higher priced (≥ 20, 000 say) cars
# We can limit our plot cars with lower price by setting limits
qplot(price,
data=auto.data,
geom='histogram',
binwidth=450) +
xlim(4000,20000)
## ----hist_aes_plot,echo=FALSE,out.width=".45\\linewidth",message=FALSE,warning=FALSE,fig.show='hold'----
qplot(price,
data=auto.data,
color=drive.wheels)
qplot(price,
data=auto.data,
fill=drive.wheels)
## ----hist_facet_plot,echo=FALSE,out.width=".7\\linewidth",message=FALSE,warning=FALSE----
qplot(price,
data=auto.data) +
facet_wrap(~drive.wheels)
## ----hist_facet_scale_plot,echo=FALSE,out.width=".7\\linewidth",message=FALSE,warning=FALSE----
# This helps us separate out the categorical variables much easier.
# Note the counts vary quite a bit among the different classes, but yet the
# count axis is the same for all. We can change this by modifying the
# facet_wrap call:
qplot(price,
data=auto.data) +
facet_wrap(~drive.wheels,
scales = 'free_y')
## ----hist_facet_scale_plot3,echo=FALSE,out.width=".6\\linewidth",message=FALSE,warning=FALSE----
qplot(price,
data=auto.data) +
facet_wrap(~drive.wheels,
scales = 'free_y',
nrow=3)
## ----hist_density,eval=FALSE---------------------------------------------
qplot(price,data=auto.data,
geom='density')
qplot(price,
..density.., # don't use counts
data=auto.data,
geom='histogram') +
geom_density()
qplot(height,price,
data=auto.data,
geom='density2d')
qplot(height,price,
data=auto.data)+
geom_density2d()
## ----boxplot,eval=FALSE--------------------------------------------------
qplot(drive.wheels,
price,
data=auto.data,
geom='boxplot')