forked from OHI-Science/data-science-training
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ggplot2_training.Rmd
176 lines (109 loc) · 2.46 KB
/
ggplot2_training.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
---
title: "ggplot2 training"
output: html_document
---
```{r setup, include=FALSE}
install.packages("tidyverse")
library(tidyverse)
```
#mpg dataset
```{r}
mpg
```
```{r}
ggplot(data = mpg)
```
```{r}
ggplot(data = mpg, aes(x = displ, y = hwy)) +
geom_point()
```
```{r}
#assign this plot to a variable
car_plot <- ggplot(data = mpg, aes(x = displ, y = hwy))
# build off of car_plot
car_plot +
geom_point()
```
```{r}
ggplot(data = mpg, aes(x = displ, y = hwy)) +
geom_point(alpha = 0.4)
```
Coloring points in the plot based on class of car (e.g. sub or compact)
```{r}
ggplot(data = mpg) +
geom_point(aes(x = displ, y = hwy, color = class))
```
```{r}
ggplot(data = mpg) +
geom_point(aes(x = cty, y = hwy, size = class, color = fl))
```
```{r}
ggplot(data = mpg) +
geom_point(aes(x = displ, y = hwy), alpha = 0.4, color = "blue")
```
```{r}
ggplot(data = mpg) +
geom_point(aes(x = displ, y = hwy, shape = cty))
```
```{r}
ggplot(data = mpg) +
geom_point(aes(x = displ, y = hwy, colour = displ < 5))
```
# Faceting
```{r}
ggplot(data = mpg) +
geom_point(aes(x = displ, y = hwy, color = class)) +
facet_wrap(~ manufacturer)
```
#remove the gray background using theme_bw()
```{r}
ggplot(data = mpg) +
geom_point(aes(x = displ, y = hwy, color = class)) +
facet_wrap(~ manufacturer) +
theme_dark()
```
```{r}
install.packages("ggthemes")
library(ggthemes)
ggplot(data = mpg) +
geom_point(aes(x = displ, y = hwy, color = class)) +
facet_wrap(~ manufacturer) +
theme_excel()
```
# Geoms
```{r}
ggplot(mpg, aes(x = drv, y = hwy)) +
geom_violin()
```
```{r}
ggplot(data = mpg, aes(x = displ, y = hwy)) +
geom_point() +
geom_smooth()
```
```{r}
ggplot(data = mpg, aes(x = displ, y = hwy)) +
geom_point(aes(color = class)) +
geom_smooth(color = "red")
```
# Bar Charts
```{r}
ggplot(data = mpg) +
geom_bar(aes(x = fl, fill = class), position = "dodge") +
theme_bw() +
scale_x_discrete(labels = c("CNG", "Diesel", "Ethanol", "Premium", "Regular")) +
xlab("Fuel Type") +
ylab("Number of cars") +
scale_fill_brewer(palette = "Set3")
RColorBrewer::display.brewer.all()
```
#Saving image
```{r}
ggplot(data = mpg) +
geom_bar(aes(x = fl, fill = class), position = "dodge") +
theme_bw() +
scale_x_discrete(labels = c("CNG", "Diesel", "Ethanol", "Premium", "Regular")) +
xlab("Fuel Type") +
ylab("Number of cars") +
scale_fill_brewer(palette = "Blues")
ggsave("my_plot2.pdf", width = 6, height = 6)
```