-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathggplot.Rmd
More file actions
143 lines (112 loc) · 3.18 KB
/
Copy pathggplot.Rmd
File metadata and controls
143 lines (112 loc) · 3.18 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
---
title: "r4ds chapter 3 ggplot"
author: "Yaqiong Li"
date: "Dec 10, 2018"
---
```{r}
library(tidyverse)
```
```{r}
library(ggplot2)
head(mpg)
```
ggplot() creates a cooridnate systhem that you can add layers on.
```{r}
mpg %>% ggplot(aes(x=displ, y=hwy)) + geom_point()
```
```{r}
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy))
```
# Ex 3.2.4
```{r}
ggplot(data = mpg)
```
```{r}
dim(mpg)
```
```{r}
mpg %>% ggplot(aes(x = cyl, y = hwy)) + geom_point()
```
```{r}
mpg %>% ggplot(aes(x = class, y = drv)) + geom_point()
```
# 3.3
```{r}
mpg %>% ggplot(aes(x=displ, y=hwy)) + geom_point(aes(col = class))
```
codes from book
```{r}
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, col = class))
```
Notes: to map an aesthetic to a variable, associate the name of the aesthetic to the name of the variable inside aes(). ggplot2 will automatically assign a unique level of aesthetic (color here) to each unique value of the variable, a process known as *scaling*.
Mapping an unordered varialbe (class) to an ordered aesthetic (size) is not a good idea, thus we got a warning for doing this.
```{r}
mpg %>% ggplot(aes(x = displ, y = hwy)) + geom_point(aes (size = class))
```
aes _alpha_ controls the transparency
aes _shape_ controls the shape
```{r}
mpg %>% ggplot() + geom_point(aes(x = displ, y = hwy, alpha = class))
```
```{r}
mpg %>% ggplot() + geom_point(aes(x = displ, y = hwy, shape = class))
```
If you want to manually set the aesthetic properties that do not convey information from the data set, it goes _outside_ of aes()
```{r}
mpg %>% ggplot() + geom_point(aes(x = displ, y = hwy), col = "red")
```
other manual aesthetic settings include
* color
* size (mm)
* the [shape](https://r4ds.had.co.nz/data-visualisation.html#fig:shapes) of points
# Ex 3.3.1
```{r}
glimpse(mpg)
```
A reference about [stroke](https://ggplot2.tidyverse.org/reference/geom_point.html)
```{r}
# For shapes that have a border, you can color inside and outside separately. Use the stroke aesthetic to modify the width of the border
mpg %>% ggplot(aes(x = displ, y = hwy)) + geom_point(shape = 21, color = "red", fill = "blue", size = 5, stroke = 2)
```
```{r}
mpg %>% ggplot(aes(x = displ, y = hwy, col = displ < 5)) + geom_point()
```
# 3.4 Common problems
* If the left hand of the console is a "+", it means that R doesn't think that you have completed an expression. Press *ESCAPE* to start over.
# 3.5 Facets
Facet can be used to split the plot by categorical variable.
Facet by single variable.
```{r}
mpg %>% ggplot(aes(x = displ, y = hwy)) + geom_point() + facet_wrap(~ class, nrow = 2)
```
Facet by combination of variables.
```{r}
mpg %>% ggplot(aes(x = displ, y = hwy)) +
geom_point() +
facet_grid(drv ~ cyl)
```
```{r}
mpg %>% ggplot(aes(x = displ, y = hwy)) +
geom_point() +
facet_grid( .~ cyl) # . means not into r*c
```
# Ex 3.5.1
```{r}
mpg %>% ggplot(aes(x = displ, y = hwy)) +
geom_point() +
facet_grid(drv ~.)
```
# 3.6 Geometric objects
```{r}
g <- mpg %>% ggplot(aes(x = displ, y = hwy))
g + geom_point()
```
```{r}
g + geom_smooth()
```
```{r}
g + geom_smooth(aes(linetype = drv))
```
```{r}
g + geom_point(aes(col = drv)) + geom_smooth(aes(linetype = drv))
```