forked from jtr13/cc21fall2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_r_tutorial.Rmd
173 lines (112 loc) · 4.76 KB
/
base_r_tutorial.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
# Base R Tutorial
Siqin Shen
```{r, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Overview
This is a Base R Cheatsheet/Tutorial, and it includes the most common Base R graphic functions and parameters.
I will use data `SleepStudy` from **Lock5withR** package.
```{r}
SleepStudy <- Lock5withR::SleepStudy
```
## Section 1 - Functions
### Histogram
```{r}
hist(SleepStudy$GPA, breaks = 10)
```
### Barplot
**Make sure the data is a matrix or vector before plot barplot.**
**The most easy way is to use the table() function to transform the data.**
```{r}
LarkOwl <- table(SleepStudy$LarkOwl)
barplot(LarkOwl, names.arg = "Lark-Owl", border = "blue" , col = "grey", horiz = FALSE)
```
### Scatter plot
```{r}
plot(SleepStudy$StressScore, SleepStudy$GPA)
```
### Dot Plot
**A dot plot is just a bar chart that uses dots to represent individual quanta. **
source: https://www.mvorganizing.org/is-a-dot-plot-the-same-as-a-scatter-plot/#:~:text=A%20dot%20plot%20is%20just,height%20and%20one%20represented%20weight.
```{r}
table_data <- table(SleepStudy$DepressionStatus, SleepStudy$LarkOwl)
dotchart(table_data)
```
### Box Plot
```{r}
boxplot(PoorSleepQuality ~ DepressionStatus, data = SleepStudy, horizontal = FALSE, names = c("moderate", "normal", "severe"), col = "gold")
```
### Mosaic Plot
**Make sure the data is a matrix or vector before plot mosaic plot.**
**The most easy way is to use the table() function to transform the data.**
```{r}
table_data <- table(SleepStudy$ClassYear, SleepStudy$NumEarlyClass)
mosaicplot(table_data, color = TRUE)
```
### Density Plot
**Make sure to transform the data into density before plot density plot.**
**The most easy way is to use the density() function to transform the data.**
```{r}
density_gpa <- density(SleepStudy$GPA)
plot(density_gpa)
```
## Section 2 - Parameters
### Main
**It adds the title to a graph.**
```{r}
hist(SleepStudy$GPA, breaks = 10, main = "GPA distribution")
```
### Labels
**It adds the x-axis and y-axis labels to the graph.**
```{r}
hist(SleepStudy$GPA, breaks = 10, main = "GPA distribution", xlab = "GPA", ylab = "Number of Students")
```
### Add Color
```{r}
hist(SleepStudy$GPA, breaks = 10, main = "GPA distribution", xlab = "GPA", ylab = "Number of Students",col.lab = "light blue", col = "gold")
```
### Point Shape
```{r}
plot(SleepStudy$StressScore, SleepStudy$GPA, main = "Stress Score vs GPA", pch = 6, xlab = "StressScore", ylab = "GPA",col.lab = "light blue")
```
### Legend
```{r}
plot(SleepStudy$StressScore, SleepStudy$GPA, main = "Stress Score vs GPA", col = SleepStudy$LarkOwl, pch = 6, xlab = "StressScore", ylab = "GPA",col.lab = "light blue")
legend("topleft", legend = levels(SleepStudy$LarkOwl), col = 1:nlevels(SleepStudy$LarkOwl), pch = 6, title = "LarkOwl")
```
### Point/label Size
```{r}
plot(SleepStudy$StressScore, SleepStudy$GPA, main = "Stress Score vs GPA", col = SleepStudy$LarkOwl, pch = 6, xlab = "StressScore", ylab = "GPA",col.lab = "light blue", cex.lab = 1.5, cex = 0.75)
legend("topleft", legend = levels(SleepStudy$LarkOwl), col = 1:nlevels(SleepStudy$LarkOwl), pch = 6, title = "LarkOwl")
```
### Line Type
```{r}
plot(density_gpa, main = "GPA Density Distribution", lty = 4, col = "red")
```
### Line Width
```{r}
plot(density_gpa, main = "GPA Density Distribution", lty = 4, col = "red", lwd = 2)
```
## Section 3 - Others
- Add best fit Lines to a scatter plot
**We need to get the function of line first.**
**We can use Loess smoother loess() to simulate the line.**
source: https://dcgerard.github.io/stat234/base_r_cheatsheet.html
```{r}
loess_fit <- loess(GPA ~ StressScore, data = SleepStudy)
xnew <- seq(min(SleepStudy$StressScore), max(SleepStudy$StressScore), length = 100)
ynew <- predict(object = loess_fit, newdata = data.frame(StressScore = xnew))
plot(SleepStudy$StressScore, SleepStudy$GPA, main = "Stress Score vs GPA", col = SleepStudy$LarkOwl, pch = 6, xlab = "StressScore", ylab = "GPA",col.lab = "light blue", cex.lab = 1.5, cex = 0.75)
legend("topleft", legend = levels(SleepStudy$LarkOwl), col = 1:nlevels(SleepStudy$LarkOwl), pch = 6, title = "LarkOwl")
lines(xnew, ynew, col = "blue", lty = 1, lwd = 2)
```
### Facet Wrap
```{r, fig.width=10}
wrap <- par(mfrow = c(2, 3))
hist(SleepStudy$GPA, breaks = 10, main = "GPA distribution")
barplot(LarkOwl, names.arg = "Lark-Owl", border = "blue" , col = "grey", horiz = FALSE, main = "Lark Owl Count")
plot(SleepStudy$StressScore, SleepStudy$GPA, main = "Stress Score-GPA Relation")
dotchart(table_data, main = "DepressionStatus-LarkOwl Relation")
boxplot(PoorSleepQuality ~ DepressionStatus, data = SleepStudy, horizontal = FALSE, names = c("moderate", "normal", "severe"), col = "gold", main = "PoorSleepQuality vs DepressionStatus")
mosaicplot(table_data, color = TRUE, main = "ClassYear vs Num Early Class")
```