generated from jtr13/cctemplate
-
Notifications
You must be signed in to change notification settings - Fork 57
/
ggplot2_cheatsheet_group16.Rmd
219 lines (154 loc) · 4.62 KB
/
ggplot2_cheatsheet_group16.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
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
# ggplot2 cheatsheet
Haoyuan Sun, Zhongtian Qiao
```{r, include=FALSE}
knitr::opts_chunk$set(warning = FALSE, message = FALSE)
```
```{r}
library(tidyverse)
library(ggplot2)
```
## Overview
ggplot2 is a system for declaratively creating graphics. You provide the data, tell ggplot2 how to map variables to aesthetics, what graphical primitives to use, and it takes care of the details. This cheatsheet shows code options for commonly used graphs by using ggplot2.
## scatter plot
```{r}
ggplot(data = mpg) +
geom_point(aes(x = displ, y = hwy))
```
We can get a scatter plot by using `geom_point()`.
### Setting color
```{r}
ggplot(data = mpg) +
geom_point(aes(x = displ, y = hwy), color = "blue")
```
We can change the color of the poionts by using `color =`.
### Color by groups
```{r}
ggplot(data = mpg) +
geom_point(aes(x = displ, y = hwy, color = class))
```
If X-variable is a categorical variable, such as variable "class", we can set points of different classes to have different colors.
### Identify overlapping points
```{r}
ggplot(data = mpg) +
geom_count(aes(x = displ, y = hwy))
```
We can get a scatter plot by using `geom_count`. The size of the points shows if the point is overlap.
## Line plot
```{r}
ggplot(data = mpg) +
geom_line(aes(x = displ, y = hwy))
```
We can get a line plot by using `geom_line()`.
~ Use `lty =` to change the type of line.
~ Use `size =` to change the size of line.
~ Use `col =` to change the color of line.
### Adding an arbitrary line
```{r}
ggplot(data = mpg) +
geom_point(aes(x = displ, y = hwy)) +
geom_abline(slope = 1, intercept = 20)
```
We can add arbitrary lines by using `geom_abline()`.
## Box plot
```{r}
ggplot(data = mpg) +
geom_boxplot(aes(x = class, y = hwy))
```
We can get a box plot by using `geom_boxplot()`.
### Horizontal box plot
```{r}
ggplot(data = mpg) +
geom_boxplot(aes(x = class, y = hwy)) +
coord_flip()
```
By using `coord_flip()`, we will get a horizontal box plot.
## Histogram
```{r}
ggplot(data = mpg) +
geom_histogram(aes(x = hwy))
```
We can get a histogram by using `geom_histogram()`.
### Bins
```{r}
ggplot(data = mpg) +
geom_histogram(aes(x = hwy), bins = 10)
```
The default value of bin is 30. By changing the value of `bins =`, we can get different width of bin.
## Bar plot
```{r}
ggplot(data = mpg, aes(x = hwy, y = displ)) +
geom_bar(position = "dodge", stat = "identity")
```
We can get a barplot which shows the relationship between of hwy and displ by using `geom_bar` with arguments `position="dodge"` and `stat = "identity"`.
## Heatmap
```{r}
x <- c(1, 1, 1, 2, 2, 2, 3, 3, 3)
y <- c(1, 2, 3, 1, 2, 3, 1, 2, 3)
df <- data.frame(x, y)
set.seed(2017)
df$z <- sample(9)
ggplot(df, aes(x, y)) +
geom_raster(aes(fill = z))
```
### Another way to plot heatmap
```{r}
ggplot(df, aes(x, y, fill = z)) +
geom_tile()
```
## Countour plot
```{r}
ggplot(data = mpg, aes(x = displ, y = hwy)) +
geom_density2d() +
geom_point(size = 1, alpha = 0.3)
```
We can get a contour plot by using `geom_density2d()`.
## Area plot
```{r}
ggplot(data = economics) +
geom_area(aes(x = date, y = unemploy))
```
If we want to analyse 2 continuous variables, we can plot an area plot by using `geom_area`.
## Adding Text
### title and xy-corrdinate
```{r}
ggplot(data = mpg) +
geom_point(aes(x = displ, y = hwy, color = class)) +
labs(title = "displ v.s. hwy",
subtitle = "group by different class",
x = "Displ",
y = "Hwy",
color = "Class")
```
By using `labs()`, we can add more information for the plot, such as the title, subtitle, x-coordinate, y-coordinate, the class of color, the class of fill, etc.
### Label
```{r}
data <- data.frame(name = c("a", "b", "c"), count = c(20, 10, 30))
ggplot(data, aes(name, count)) +
geom_bar(stat = "identity") +
geom_text(aes(label = count))
```
We can get the label for the plot of data by `geom_text()`,
~ The content of label is controlled by `aes(label = )`.
~ Use `hjust` and `vjust` to adjust the vertical and horizontal position of the label.
~ Use `col = ` to adjust the color of the label.
~ Use `size = ` to adjust the size of the label.
```{r}
ggplot(data, aes(name, count)) +
geom_bar(stat = "identity") +
geom_text(aes(label = count), col = "blue", vjust = -0.3, size = 5)
```
## Facet
### facet_wrap
```{r}
ggplot(data = mpg) +
geom_point(aes(x = displ, y = hwy)) +
facet_wrap(~class,nrow=2)
```
We can get multiple plots group by class by using `facet_wrap`.
### facet_grid
```{r}
ggplot(data = mpg) +
geom_point(aes(x = displ, y = hwy)) +
facet_grid(drv~cyl)
```
We can get multiple plots which are group by drv and cyl by using `facet_grid`.