-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathData Analysis-Intro to wdcplot-mtcars Example.Rmd
More file actions
executable file
·189 lines (157 loc) · 3.04 KB
/
Copy pathData Analysis-Intro to wdcplot-mtcars Example.Rmd
File metadata and controls
executable file
·189 lines (157 loc) · 3.04 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
# Intro to wdcplot; mtcars Example
===
***
When in edit mode, run the analysis using the "play" (arrow) button in the navigation bar.
## Let's load the popular mtcars dataset. It comes pre-bundled with R.
```{r}
data(mtcars)
```
## What does the data look like?
```{r}
head(mtcars)
```
# Looks like mtcars is a data frame:
```{r}
class(mtcars)
```
## So the structure is ...
```{r}
str(mtcars)
```
## What are the total number of rows?
```{r}
nrow(mtcars)
```
## How do we access a column?
```{r}
head(mtcars$mpg)
```
## What's the type/class?
```{r}
class(mtcars$mpg)
## OR
head(mtcars[,1])
## OR
head(mtcars[, "mpg"])
```
## We don't want a numeric vector. We want a data.frame slice.
```{r}
head(mtcars["mpg"])
```
## Did we do it right? What's the type/class?
```{r}
class(mtcars["mpg"])
```
## How do I get more than one column?
```{r}
head(mtcars[c("mpg","hp")])
```
## How do we know the row names?
```{r}
row.names(mtcars)
```
## How do we access a specific value?
```{r}
mtcars["Mazda RX4", "cyl"]
# OR
mtcars[1,2]
```
## How do I access a row?
```{r}
mtcars["Mazda RX4", ]
```
## Also the same as ...
```{r}
mtcars[1, ]
```
## Vehicles with automatic transmission?
```{r}
head(mtcars[mtcars$am==0,])
```
## Mileage for automatic transmission vehicles?
```{r}
mtcars[mtcars$am==0,]$mpg
```
## Let's know our data better.
```{r}
summary(mtcars)
```
## We don't want to keep typing mtcars$... so we use attach:
```{r}
attach(mtcars)
```
## You can now directly refer mtcars$mpg as mpg.
```{r}
identical(mtcars$mpg, mpg)
```
## Let's summarize our data:
```{r}
table(cyl)
```
## Plotting is easy...
## bar plot
```{r}
barplot(mpg)
```
## OR a histogram
```{r}
hist(mpg)
```
## OR a boxplot
```{r}
boxplot(mpg)
```
## Looks like mpg are pretty low:
```{r}
mean(mpg)
```
## Mean of mileage for 4 cylinder cars:
```{r}
mean(mpg[cyl==4])
```
## vs 8 cylinder cars:
```{r}
mean(mpg[cyl==8])
```
## Is that because of a higher number of cylinders?
```{r}
plot(cyl, mpg)
```
## Let's fit a regression line:
```{r}
lm(mpg~cyl+hp)
```
## Pearson correlation, is that a good fit?
```{r}
cor(mpg,cyl)^2
cor(mpg, hp)^2
```
## Let's plot based on number of cylinders
```{r}
plot(hp,mpg,pch=19, col=cyl)
# add a legend
legend(250, 30, pch=19, col=c(4,6,8), legend=c("4 cylinders","6 cylinders","8 cylinders"))
```
## Now we do the cool stuff:
```{r fig.width=11, fig.height=11, tidy=FALSE}
require(rcloud.dcplot)
wdcplot(mtcars,
dimensions(..index.., mpg, wt, cyl),
groups(mpgGroup = group(mpg, bin(2)),
wtGroup = group(wt, group = bin(0.5)),
cylinders = group(cyl)),
charts(SWvL = bubble('Miles Per Gallon vs. Weight',
dimension = ..index..,
color = cyl,
x = wt,
y = mpg,
r = ..selected.. * 3,
label = NULL),
cyl = pie("Cylinders",group = cylinders),
PW = bar('Miles Per Gallon', group = mpgGroup, x.domain = c(10.4,33.9)),
PL = bar('Weight', group = wtGroup, x.domain = c(1.5,5.5))
))
```
```{r}
detach(mtcars)
```