-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path02-Visualizing-data-set-Analyzing-iris-flower-data-set.Rmd
More file actions
735 lines (580 loc) · 31 KB
/
02-Visualizing-data-set-Analyzing-iris-flower-data-set.Rmd
File metadata and controls
735 lines (580 loc) · 31 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
# Visualizing the iris flower data set
Learning objectives:
- Basic concepts of R graphics
- Install and load R packages
- Basic plots with ggplot2
- Different ways to visualize the iris flower dataset
## Basic concepts of R graphics
We start with base R graphics. The first important distinction should be made about
high- and low-level graphics functions in base R.
High-level graphics functions initiate new plots, to which new elements could be
added using the low-level functions. See table below.
Function | Description
-----------|---------------------------------------------
plot | Generic plotting function
boxplot | boxplot. Can be applied to multiple columns of a matrix, or use equations boxplot( y ~ x)
hist | histogram
qqnorm | Quantile-quantile (Q-Q) plot to check for normality
curve | Graph an arithmetic function
barplot | Barplot
mosaicplot | Using mosaics to represent the frequencies of tabulated counts.
heatmap | Using colors to visualize a matrix of numeric values.
Table: High-level graphics functions initiate a new plot.
\newline \newline
In contrast, low-level graphics functions do not wipe out the existing plot;
they add elements to it. The functions are listed below:
Function | Description
-----------|---------------------------------------------
points | Add points to a plot
lines | Add lines to a plot
abline | Add a straight line
segments | Add line segments
text | Add text to a plot
legend | Add a legend to a plot
arrows | Add arrows to a plot
Table: Low-level graphics functions that add elements to an existing plot.
Another distinction about data visualization is between plain, exploratory plots and
refined, annotated ones. Both types are essential.
Sometimes we generate many graphics for exploratory data analysis (EDA)
to get some sense of what the data looks like. We can achieve this by using
plotting functions with **default settings** to quickly generate a lot of
"plain" plots. R is a very powerful EDA tool.
If we find something interesting about a dataset, we want to generate
really "cool"-looking graphics for papers and
presentations. Often we want to use a plot to convey a message to an audience.
Making such plots typically requires a bit more coding, as you
have to customize different parameters. For me, it usually involves
lots of Google searches, copy-and-paste of example codes, and then lots of trial-and-error.
One of the open secrets of R programming is that you can start from a plain
figure and **refine it step by step**. Here is
an example using the base R graphics. This produces a basic scatter plot with
the petal length on the x-axis and petal width on the y-axis. Since iris is a
data frame, we will use the ```iris$Petal.Length``` to refer to the Petal.Length
column.
```{r, fig.align='center', out.width='80%' }
PL <- iris$Petal.Length
PW <- iris$Petal.Width
plot(PL, PW)
```
To hange the type of symbols:
```{r, fig.align='center', out.width='80%'}
plot(PL, PW, pch = 2) # pch = 2 means the symbol is triangle
```
The **pch** parameter can take values from 0 to 25. Each value corresponds
to a different type of symbol. The commonly used values and point symbols
are shown in Figure \@ref(fig:pchsymbols).
```{r pchsymbols, echo=FALSE, out.width='100%', fig.cap='Points Symbols', fig.align='center'}
knitr::include_graphics("images/ch2_1_pch_symbols.png")
```
There are many other parameters to the **plot** function in R. You can get these
from the documentation:
```{r, eval=FALSE}
? plot
```
We can also change the color of the data points easily with the **col = ** parameter.
```{r, fig.align='center', out.width='80%'}
plot(PL, PW, pch = 2, col = "green") # change the symbol color to green
```
Next, we can use different symbols for different species. First, extract the species information.
```{r, fig.align='center', out.width='80%'}
iris$Species
```
This output shows that the 150 observations are classed into three
species *setosa*, *versicolor*, and *virginica*.
If you are read theiris data from a file, like what we did in Chapter 1,
the data type of the Species column is character. We need to convert this column into a **factor**.
```{r, fig.align='center', out.width='80%'}
iris$Species <- as.factor(iris$Species)
str(iris$Species)
```
Once convertetd into a factor, each observation is represented by one of the three levels of
the three species *setosa*, *versicolor*, and *virginica*. **factors** are used to
store categorical variables as levels.
To completely convert this factor to numbers for plotting, we use the *as.numeric* function.
Since we do not want to change the data frame, we will define a new variable called speciesID.
```{r, fig.align='center', out.width='80%'}
speciesID <- as.numeric(iris$Species)
speciesID
```
We can assign different markers to different species by letting *pch = speciesID*.
```{r, fig.align='center', out.width='80%'}
plot(PL, PW, pch = speciesID, col = "green")
```
What happens here is that the 150 integers stored in the speciesID factor are used
to alter marker types. The first 50 data points (*setosa*) are represented by open
circles (pch = 1). The next 50 (*versicolor*) are represented by triangles (pch = 2), while the last
50 (*virginica*) are in crosses (pch = 3).
Similarily, we can set three different colors for three species.
```{r, fig.align='center', out.width='80%'}
# assign 3 colors red, green, and blue to 3 species *setosa*, *versicolor*,
# and *virginica* respectively
plot(PL, PW, pch = speciesID, col = speciesID)
```
This figure starts to looks nice, as the three species are easily separated by
color and shape. It seems redundant, but it make it easier for the reader.
Some people are even color blind.
Let us change the x- and y-labels, and
add a main title.
```{r, fig.align='center', out.width='80%'}
plot(PL, PW, # x and y
pch = speciesID, # symbol type
col = speciesID, # color
xlab = "Petal length (cm)", # x label
ylab = "Petal width (cm)", # y label
main = "Petal width vs. length" # title
)
```
Note that this command spans many lines. Very long lines make it hard to read.
The benefit of multiple lines is that we can clearly see each line contain a parameter.
When you are typing in the Console window, R knows that you are not done and
will be waiting for the second parenthesis.
Note that the indention is by two space characters and this chunk of code ends with a right parenthesis.
It is essential to write your code so that it could be easily understood, or reused by others
(or your future self). See
[breif](http://adv-r.had.co.nz/Style.html) and
[detailed](https://style.tidyverse.org/) style guides. This is like checking the
dressing code before going to an event.
A true perfectionist never settles. We notice a strong linear correlation between
petal length and width. Let's add a trend line using **abline()**, a low level graphics function.
```{r, echo = c(2), fig.align='center', out.width='80%'}
plot(PL, PW,
pch = speciesID,
col = speciesID,
xlab = "Petal length (cm)",
ylab = "Petal width (cm)",
main = "Petal width vs. length"
)
abline(lm(PW ~ PL)) # the order is reversed as we need y ~ x.
```
The ```lm(PW ~ PL)``` generates a linear model (lm) of petal width as a function petal
length. y ~ x is **formula notation** that used in many different situations.
This linear regression model is used to plot the trend line.
We calculate the Pearson's correlation coefficient and mark it to the plot.
```{r, fig.show=FALSE}
PCC <- cor(PW, PL) # Pearson's correlation coefficient
PCC <- round(PCC, 2) # round to the 2nd place after decimal point.
paste("R =", PCC)
```
The *paste* function glues two strings together. Then we use the **text** function to
place strings at lower right by specifying the coordinate of (x=5, y=0.5).
(ref:23-5) A refined scatter plot using base R graphics.
```{r 23-5, echo = c(3,4), fig.cap='(ref:23-5)', fig.align='center', out.width='80%'}
plot(PL, PW,
pch = speciesID, # assign different symbols to different species
col = speciesID, # assign different colors to different species
xlab = "Petal length (cm)", # label x-axis
ylab = "Petal width (cm)", # label y-axis
main = "Petal width vs. length"
) # add a title to the plot
abline(lm(PW ~ PL)) # add a regression line to the plot
text(5, 0.5, paste("R=", PCC)) # add text annotation.
legend("topleft", # specify the location of the legend
levels(iris$Species), # specify the levels of species
pch = 1:3, # specify three symbols used for the three species
col = 1:3 # specify three colors for the three species
)
```
The last expression adds a legend at the top left using the *legend* function.
While **plot** is a high-level graphics function that starts a new plot,
**abline**, **text**, and **legend** are all low-level functions that can be
added to an existing plot.
You should be proud of yourself if you are able to generate this plot.
This is how we create complex plots step-by-step with trial-and-error.
Such a refinement process can be time-consuming.
But another open secret of coding is that we frequently steal others ideas and
code. The book [R Graphics Cookbook](https://r-graphics.org/) includes all kinds of R plots and
example code. Some websites list all sorts of R graphics and example codes that you can use.
For example, this [website:](http://www.r-graph-gallery.com/) <http://www.r-graph-gallery.com/> contains
more than 200 such examples. Another
one is available [here:](http://bxhorn.com/r-graphics-gallery/): <http://bxhorn.com/r-graphics-gallery/>.
If you know what types of graphs you want, it is very easy to start with the
template code and swap out the dataset.
## Scatter plot matrix
While data frames can have a mixture of numbers and characters in different
columns, a **matrix often only contains numbers**. Let's extract the first 4
columns from the data frame iris and convert to a matrix:
```{r}
ma <- as.matrix(iris[, 1:4]) # convert to matrix
colMeans(ma) # column means for matrix
colSums(ma)
```
The same thing can be done with rows via *rowMeans(x)* and *rowSums(x)*.
We can generate a matrix of scatter plot by *pairs()* function.
```{r fig.keep="none"}
pairs(ma)
```
(ref:spm2-2) Scatter plot matrix
```{r spm2-2, message=FALSE, out.width='100%', fig.cap='(ref:spm2-2)', fig.align='center'}
pairs(ma, col = rainbow(3)[speciesID]) # set colors by species
```
>
```{exercise}
>
Interpret the scatter plot matrix of Figure \@ref(fig:spm2-2) .
```
>
\ \
>
```{exercise}
>
Plot a scatter plot matrix for dataset **mtcars**. Set different point symbols and colors based on the types of *cyl*.
```
## Star and segment diagrams
**Star plot uses stars to visualize multidimensional data.** Radar chart is a useful way to display multivariate observations with an arbitrary number of variables. Each observation is represented as a star-shaped figure with one ray for each variable. For a given observation, the length of each ray is made proportional to the size of that variable. The star plot was firstly used by Georg von Mayr in 1877!
```{r fig.keep='none'}
df <- iris[, 1:4]
stars(df) # do I see any diamonds?
stars(df, key.loc = c(17, 0)) # What does this tell you?
```
>
```{exercise}
>
Based on the star plot, what is your overall impression regarding the differences among these 3 species of flowers?
```
The stars() function can also be used to generate segment diagrams, where each variable is used to generate colorful segments. The sizes of the segments are proportional to the measurements.
```{r fig.keep='none'}
stars(df, key.loc = c(20, 0.5), draw.segments = TRUE)
```
(ref:12-4) Star plots and segments diagrams.
```{r 12-4, echo=FALSE, fig.cap='(ref:12-4)', fig.align='center'}
knitr::include_graphics("images/img1204n_starsegment.png")
```
>
```{exercise}
>
Produce the segments diagram of the state data (state.x77) and offer some interpretation of South Dakota compared with other states. Hints: Convert the matrix to a data frame using *df.state.x77 <- as.data.frame(state.x77)*, then plot the segments diagram based on the dataset *df.state.x77*.
```
## The ggplot2 package is intuitive and powerful
In addition to the graphics functions in base R, there are many other packages
we can use to create plots. The most widely used are lattice and ggplot2.
Together with base R graphics,
sometimes these are referred to as the three independent paradigms of R
graphics. The lattice package extends base R graphics and enables the creating
of graphs in multiple facets. The ggplot2 is developed based on a Grammar of
Graphics (hence the "gg"), a modular approach that builds complex graphics by
adding layers. Intuitive yet powerful, ggplot2 is becoming increasingly popular.
ggplot2 is a modular, intuitive system for plotting, as we use different functions to refine different aspects of a chart step-by-step:
- **Aesthetic mapping** from variables to visual characteristics
- **Geometric shapes** (points, lines, boxes, bars, histograms, maps, etc)
- **Scales** and **statistical transformations** (log, reverse, count, etc)
- **coordinate systems**
- **facets** ( multiple plots)
- **Labels** and **annotations**
Detailed tutorials on ggplot2 can be find [here](https://uc-r.github.io/ggplot_intro) and
by its [author](https://r4ds.had.co.nz/data-visualisation.html).
The ggplot2 functions is not included in the base distribution of R.
These are available as an additional package, on the CRAN [website](https://cran.r-project.org/web/packages/ggplot2/index.html).
They need to be downloaded and installed. One of the main advantages of R is that it
is open, and users can contribute their code as packages. If you are using
**RStudio**, you can choose **Tools-\>Install packages** from the main menu, and
then enter the name of the package. If you are using R software, you can install
additional packages, by clicking **Packages** in the main menu, and select a
mirror site. All these mirror sites work the same, but some may be faster. After
choosing a mirror and clicking "OK", you can scroll down the long list to find
your package. Alternatively, you can type this command to install packages.
```{r eval=FALSE}
# Install the package. You can also do it through the Packages Tab
install.packages("ggplot2")
```
Packages only need to be installed once.
But every time you need to use the **functions or data** in a package,
you have to **load** it from your hard drive into memory.
```{r message=FALSE, warning=FALSE}
library(ggplot2) # load the ggplot2 package
```
(ref:2-6) Basic scatter plot using the ggplot2 package.
```{r 2-6, fig.cap='(ref:2-6)',fig.align='center', out.width='100%', message=FALSE}
# define a canvas
ggplot(data = iris)
# map data to x and y coordinates
ggplot(data = iris) +
aes(x = Petal.Length, y = Petal.Width)
# add data points
ggplot(data = iris) +
aes(x = Petal.Length, y = Petal.Width) +
geom_point()
# change color & symbol type
ggplot(data = iris) +
aes(x = Petal.Length, y = Petal.Width) +
geom_point(aes(color = Species, shape = Species))
# add trend line
ggplot(data = iris) +
aes(x = Petal.Length, y = Petal.Width) +
geom_point(aes(color = Species, shape = Species)) +
geom_smooth(method = lm)
# add annotation text to a specified location by setting coordinates x = , y =
ggplot(data = iris) +
aes(x = Petal.Length, y = Petal.Width) +
geom_point(aes(color = Species, shape = Species)) +
geom_smooth(method = lm) +
annotate("text", x = 5, y = 0.5, label = "R=0.96")
```
Some ggplot2 commands span multiple lines.
The first line defines the plotting space.
The ending "+" signifies that another layer ( data points) of plotting is added.
Also, the ggplot2 package handles a lot of the details for us.
We can easily generate many different types of plots.
It is also much easier to generate a plot like Figure \@ref(fig:23-5).
```{r fig.align='center', out.width='80%' }
ggplot(iris) +
aes(x = Petal.Length, y = Petal.Width) + # define space
geom_point(aes(color = Species, shape = Species)) + # add points
geom_smooth(method = lm) + # add trend line
annotate("text", x = 5, y = 0.5, label = "R=0.96") + # annotate with text
xlab("Petal length (cm)") + # x-axis labels
ylab("Petal width (cm)") + # y-axis labels
ggtitle("Correlation between petal length and width") # title
```
As you can see, data visualization using ggplot2 is similar to painting:
we first find a blank canvas, paint background, sketch outlines, and then add details.

```{r, echo=FALSE}
# URL https://giphy.com/gifs/l3vRnG8MctBsvAMtG
```
>
```{exercise}
>
Create a scatter plot of sepal length vs sepal width, change colors and shapes with species, and add trend line.
```
## Other types of plots with ggplot2
Box plots can be generated by ggplot.
```{r out.width='80%',fig.align='center'}
library(ggplot2)
ggplot(data = iris) +
aes(x = Species, y = Sepal.Length, color = Species) +
geom_boxplot()
```
Here we use Species, a categorical variable, as x-coordinate. Essentially, we
use it to define three groups of data. The y-axis is the sepal length,
whose distribution we are interested in. The benefit of using ggplot2 is evident as we can easily refine it.
(ref:22-1) Box plot with raw data points. This is getting increasingly popular.
```{r fig.cap='(ref:22-1)', out.width='80%',fig.align='center'}
ggplot(data = iris) +
aes(x = Species, y = Sepal.Length, color = Species) +
geom_boxplot() +
geom_jitter(position = position_jitter(0.2))
```
On top of the boxplot, we add another layer representing the raw data
points for each of the species. Since lining up data points on a
straight line is hard to see, we jittered the relative x-position within each subspecies randomly.
We also color-coded three species simply by adding "color = Species". Many of the low-level
graphics details are handled for us by ggplot2 as the legend is generated automatically.
(ref:2-7) Density plot of petal length, grouped by species.
```{r 2-7, fig.cap='(ref:2-7)',out.width='60%', fig.align='center' }
ggplot(data = iris) +
aes(x = Petal.Length, fill = Species) +
geom_density(alpha = 0.3)
```
(ref:2-51) Density plot by subgroups using facets.
```{r fig.cap='(ref:2-51)', out.width='80%',fig.align='center'}
ggplot(data = iris) +
aes(x = Petal.Length, fill = Species) +
geom_density(alpha = 0.3) +
facet_wrap(~Species, nrow = 3)
```
```{r dynamite, fig.cap='Dynamite Plot', out.width='80%', fig.align='center'}
ggplot(iris) +
aes(x = Species, y = Sepal.Width, fill = Species) +
stat_summary(geom = "bar", fun = "mean") +
stat_summary(geom = "errorbar", fun.data = "mean_se", width = .3)
```
The bar plot with error bar in \@ref(fig:dynamite) we generated above is called
dynamite plots for its similarity. **"The dynamite plots must die!"**, argued
renowned statistician Rafael Irizarry in his [blog](https://simplystatistics.org/2019/02/21/dynamite-plots-must-die/).
Dynamite plots give very little information; the mean and standard errors just could be
printed out. The outliers and overall distribution is hidden. Many scientists have chosen to use this boxplot with jittered points.
>
```{exercise}
>
Use boxplot, and density plots to investigate the similarity and differences of petal width of three species in the **iris** dataset.
```
## Hierarchical clustering and heat map
Hierarchical clustering summarizes observations into trees representing the overall similarities.
```{r}
ma <- as.matrix(iris[, 1:4]) # convert to matrix
disMatarix <- dist(ma)
plot(hclust(disMatarix))
```
We first calculate a distance matrix using the **dist()** function with the default Euclidean
distance method. The distance matrix is then used by the **hclust1()** function to generate a
hierarchical clustering tree with the default complete linkage method, which is then plotted in a nested command.
The 150 samples of flowers are organized in this cluster dendrogram based on their Euclidean
distance, which is labeled vertically by the bar to the left side. Highly similar flowers are
grouped together in smaller branches, and their distances can be found according to the vertical
position of the branching point. We are often more interested in looking at the overall structure
of the dendrogram. For example, we see two big clusters.
First, each of the flower samples is treated as a cluster. The algorithm joins
the two most similar clusters based on a distance function. This is performed
iteratively until there is just a single cluster containing all 150 flowers. At
each iteration, the distances between clusters are recalculated according to one
of the methods---*Single linkage, complete linkage, average linkage, and so on*.
In the single-linkage method, the distance between two clusters is defined by
the smallest distance among the all possible object pairs. This approach puts
'friends of friends' into a cluster. On the contrary, the complete linkage
method defines the distance as the largest distance between object pairs. It
finds similar clusters. Between these two extremes, there are many options in
between. The linkage method I found the most robust is the average linkage
method, which uses the average of all distances. However, the default seems to
be the complete linkage. Thus we need to change that in our final version.
**Heat maps** with hierarchical clustering are my favorite way of visualizing data matrices. The rows and columns are reorganized based on hierarchical clustering, and the values in the matrix are coded by colors. Heat maps can directly visualize millions of numbers in one plot. The hierarchical trees also show the similarity among rows and columns.
```{r}
heatmap(ma,
scale = "column",
RowSideColors = rainbow(3)[iris$Species]
)
```
Scaling is handled by the **scale()** function, which subtracts the mean from each
column and then divides by the standard division. Afterward, all the columns
have the same mean of approximately 0 and standard deviation of 1. This is also
called standardization. The default color scheme codes bigger numbers in yellow
and smaller numbers in red. The color bar on the left codes for different
species. But we still miss a legend and many other things can be polished.
Instead of going down the rabbit hole of adjusting dozens of parameters to
heatmap function (and it's improved version heatmap.2 in the ggplots package), We
will refine this plot using another R package called pheatmap.
```{r eval = FALSE}
install.packages("pheatmap")
```
(ref:12-3) Heatmap for iris flower dataset.
```{r 12-3, warning=FALSE, message=FALSE, fig.cap='(ref:12-3)', fig.align='center'}
library(pheatmap)
ma <- as.matrix(iris[, 1:4]) # convert to matrix
row.names(ma) <- row.names(iris) # assign row names in the matrix
pheatmap(ma,
scale = "column",
clustering_method = "average", # average linkage
annotation_row = iris[, 5, drop = FALSE], # the 5th column as color bar
show_rownames = FALSE
)
```
First, we convert the first 4 columns of the iris data frame into a matrix. Then
the row names are assigned to be the same, namely, "1" to "150". This is
required because row names are used to match with the column annotation
information, specified by the annotation\_row parameter. Even though we only
need the 5th column, i.e., Species, this has to be a data frame. To prevent R
from automatically converting a one-column data frame into a vector, we used
*drop = FALSE* option. Multiple columns can be contained in the column
annotation data frame to display multiple color bars. The rows could be
annotated the same way.
More information about the pheatmap function can be obtained by reading the help
document. But most of the times, I rely on the online tutorials. Beyond the
official documents prepared by the author, there are many documents created by R
users across the world. The R user community is uniquely open and supportive. I
was researching heatmap.2, a more refined version of heatmap part of the gplots
package and landed on Dave Tang's
[blog](https://davetang.org/muse/2010/12/06/making-a-heatmap-with-r/), which
mentioned that there is a more user-friendly package called pheatmap described
in his other
[blog](https://davetang.org/muse/2018/05/15/making-a-heatmap-in-r-with-the-pheatmap-package/).
To figure out the code chuck above, I tried several times and also used Kamil
Slowikowski's [blog](https://slowkow.com/notes/pheatmap-tutorial/).
We can gain many insights from Figure \@ref(fig:12-3). The 150 flowers in the rows are organized into different clusters. *I. Setosa* samples obviously formed a unique cluster, characterized by smaller (blue) petal length, petal width, and sepal length. The other two subspecies are not clearly separated but we can notice that some *I. Virginica* samples form a small subcluster showing bigger petals. The columns are also organized into dendrograms, which clearly suggest that petal length and petal width are highly correlated.
## Principal component analysis (PCA)
This section can be skipped, as it contains more statistics than R programming.
To visualize high-dimensional data, we use PCA to map data to lower dimensions.
PCA is a linear dimension-reduction method. As illustrated in Figure \@ref(fig:13-4),
it tries to define a new set of orthogonal coordinates to represent the data such that
the new coordinates can be ranked by the amount of variation or information it captures
in the dataset. After running PCA, you get many pieces of information:
- How the new coordinates are defined,
- The percentage of variances captured by each of the new coordinates,
- A representation of all the data points onto the new coordinates.
(ref:13-4) Concept of PCA. Here the first component x’ gives a relatively accurate representation of the data.
```{r 13-4, echo=FALSE, out.width='75%', fig.cap='(ref:13-4)', fig.align='center'}
knitr::include_graphics("images/img1304_PCA.png")
```
Here is an example of running PCA on the first 4 columns of the iris data.
Note that "scale = TRUE" in the following
command means that the data is normalized before conduction PCA so that each
variable has unit variance.
```{r message=FALSE}
pca <- prcomp(iris[, 1:4], scale = TRUE)
pca # Have a look at the results.
```
The first principal component is positively correlated with Sepal length, petal length, and petal width. Recall that these three variables are highly correlated. Sepal width is the variable that is almost the same across three species with small standard deviation. PC2 is mostly determined by sepal width, less so by sepal length.
```{r out.width='50%' }
plot(pca) # plot the amount of variance each principal components captures.
str(pca) # this shows the structure of the object, listing all parts.
```
```{r }
head(pca$x) # the new coordinate values for each of the 150 samples
```
To plot the PCA results, we first construct a data frame with all information, as required by ggplot2.
```{r fig.keep='none'}
pcaData <- as.data.frame(pca$x[, 1:2]) # extract first two columns and convert to data frame
pcaData <- cbind(pcaData, iris$Species) # bind by columns
colnames(pcaData) <- c("PC1", "PC2", "Species") # change column names
library(ggplot2)
ggplot(pcaData) +
aes(PC1, PC2, color = Species, shape = Species) + # define plot area
geom_point(size = 2) # adding data points
```
Now we have a basic plot. We can add elements one by one using the “+”
sign at the end of the first line. We will add details to this plot.
(ref:13-5) PCA plot of the iris flower dataset using R base graphics (left) and ggplot2 (right).
```{r 13-5, message=FALSE, fig.show='hold', out.width='80%', fig.cap='(ref:13-5)', fig.align='center'}
percentVar <- round(100 * summary(pca)$importance[2, 1:2], 0) # compute % variances
ggplot(pcaData, aes(PC1, PC2, color = Species, shape = Species)) + # starting ggplot2
geom_point(size = 2) + # add data points
xlab(paste0("PC1: ", percentVar[1], "% variance")) + # x label
ylab(paste0("PC2: ", percentVar[2], "% variance")) + # y label
ggtitle("Principal component analysis (PCA)") + # title
theme(aspect.ratio = 1) # width and height ratio
```
The result (Figure \@ref(fig:13-5)) is a projection of the 4-dimensional
iris flowering data on 2-dimensional space using the first two principal components.
We can see that the first principal component alone is useful in distinguishing the three species.
We could use simple rules like this: If PC1 < -1, then Iris setosa.
If PC1 > 1.5 then Iris virginica. If -1 < PC1 < 1, then Iris versicolor.
>
```{exercise}
>
Create PCA plot of the state.x77 data set (convert matrix to data frame).
Use the state.region information to color code the states. Interpret your results.
Hint: do not forget normalization using the scale option.
```
## Classification by predicting the odds of binary outcomes
This section can be skipped, as it contains more statistics than R programming.
It is easy to distinguish *I. setosa* from the other two species, just based on
petal length alone. Here we focus on building a predictive model that can
predict between *I. versicolor* and *I. virginica*. For this purpose, we use the logistic
regression to model the odds ratio of being *I. virginica* as a function of all
of the 4 measurements:
$$ln(odds)=ln(\frac{p}{1-p})
=a×Sepal.Length + b×Sepal.Width + c×Petal.Length + d×Petal.Width+c+e.$$
```{r}
df <- iris[51:150, ] # removes the first 50 samples, which represent I. setosa
df <- droplevels(df) # removes setosa, an empty levels of species.
model <- glm(Species ~ ., # Model: Species as a function of other variables
family = binomial(link = "logit"),
data = df
)
summary(model)
```
Sepal length and width are not useful in distinguishing versicolor from
*virginica*. The most significant (P=0.0465) factor is Petal.Length. One unit
increase in petal length will increase the log-odds of being *virginica* by
9.429. A marginally significant effect is found for Petal.Width.
If you do not fully understand the mathematics behind linear regression or
logistic regression, do not worry about it too much. In this class, I
just want to show you how to do these analyses in R and interpret the results. I
do not understand how computers work. Yet I **use** it every day.
The best way to learn R is to use it. After the first two chapters, it is entirely
possible to start working on a your own dataset.
If you do not have a dataset, you can find one from sources
such as [TidyTuesday](https://github.com/rfordatascience/tidytuesday).
You do not need to finish the rest of this book.
Set a goal or a research question. Feel free to search for
and steal some example code.
>
```{exercise}
>
So far, we used a variety of techniques to investigate the iris flower dataset. Recall that in the very beginning, I asked you to eyeball the data and answer two questions:
>
a. What distinguishes these three species?
b. If we have a flower with sepals of 6.5cm long and 3.0cm wide, petals of 6.2cm long, and 2.2cm wide, which species does it most likely belong to?
>
Review all the analysis we did, examine the raw data, and answer the above questions. Write a paragraph and provide evidence of your thinking. Do more analysis if needed.
```
References:
1 Beckerman, A. (2017). Getting started with r second edition. New York, NY, Oxford University Press.