-
Notifications
You must be signed in to change notification settings - Fork 0
/
02_workbook.Rmd
181 lines (123 loc) · 3.54 KB
/
02_workbook.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
174
175
176
177
178
179
180
181
---
title: "Grammar of Graphics"
author: "Ashrita Dara"
output:
html_document:
code_folding: hide
toc: TRUE
toc_float: TRUE
editor_options:
chunk_output_type: console
---
```{r}
library(tidyverse)
```
## Exercise 1
Created a scatter plot that shows the relationship between pressure and wind.
```{r}
# YOUR WORK GOES HERE
# dots going negative slope diagonal scattered on outside
# aes is aesthetic mappings which gives labels,
# colors, transparency, etc..
# geom_point is geometric points
ggplot(data = storms) +
geom_point(mapping = aes(x = pressure, y = wind, color = category))
```
## Exercise 2
```{r}
# YOUR WORK GOES HERE
# salmon color is default color
ggplot(data = storms) +
geom_point(mapping = aes(x = pressure, y = wind, color = "green"))
# put the color outside of aes
ggplot(data = storms) +
geom_point(mapping = aes(x = pressure, y = wind), color = "purple", alpha = 0.05)
# 1 / 0.05 is 20 so it takes 20 translucent dots layered on
# top of each other to make it solid color
ggplot(data = storms) +
geom_point(mapping = aes(x = pressure, y = wind), color = "purple", alpha = 0.05)
```
## Exercise 3
```{r}
# YOUR WORK GOES HERE
ggplot(data = msleep) +
geom_point(mapping = aes(x = bodywt, y = sleep_total),
color = "navy", alpha = 0.4) +
scale_x_log10() +
scale_y_continuous(limits = c(0, NA))
```
## Exercise 4
```{r}
adequate_sleep <- msleep |>
mutate(sleep14 = if_else(
condition = sleep_total > 16,
true = "Adequate sleep",
false = "Inadequate sleep")
) |>
count(sleep14) |>
mutate(prop = n / sum(n))
ggplot(data = adequate_sleep, aes(x = "", y = prop, fill = sleep14)) +
geom_col() +
# YOUR WORK GOES HERE\
coord_polar(theta = "y") +
scale_fill_manual(values = c("Adequate sleep" = "lightgreen", "Inadequate sleep" = "salmon")) +
theme_void() # gets rid of unneccesary labels
```
## Exercise 5
```{r}
# YOUR WORK GOES HERE
ggplot(data = storms) +
geom_point(mapping = aes(x = pressure, y = wind),
color = "navy", alpha = 0.05) +
facet_wrap(~ month)
```
## Exercise 6
```{r}
# YOUR WORK GOES HERE
# STATISTICAL TRANSFORMATIONS : transform data
ggplot(data = storms) +
geom_bar(mapping = aes(x = category))
count(storms, category) %>%
ggplot() +
geom_col(mapping = aes(x = category, y = n))
```
## Exercise 7
```{r}
# YOUR WORK GOES HERE
ggplot(data = storms) +
geom_bar(mapping = aes(x = category)) +
theme_minimal() # makes white background instead of default grey
```
## Exercise 8
```{r}
# YOUR WORK GOES HERE
# example of layers
storms %>%
filter(category > 0) %>% # gets storms greater than 0 because 0 is not a storm
distinct(name, year) %>%
count(year) %>%
ggplot() +
geom_line(mapping = aes(x = year, y = n), alpha = 0.5) +
geom_point(mapping = aes(x = year, y = n))
```
## Exercise 9
```{r}
# YOUR WORK GOES HERE
ggplot(data = storms, mapping = aes(x = pressure, y = wind)) +
geom_point(mapping = aes(color = category, alpha = 0.05)) +
geom_smooth() # put line after the graph or else it will be covered
```
## Exercise 10
```{r}
# YOUR WORK GOES HERE
# from exercise 5
ggplot(data = storms) +
geom_point(mapping = aes(x = pressure, y = wind),
color = "navy", alpha = 0.05) +
facet_wrap(~ month)
ggsave(filename = "favorite-plot.png", width = 6.5, height = 4)
```
## Session Info
```{r}
sessionInfo()
```