-
Notifications
You must be signed in to change notification settings - Fork 0
/
ggplot_day1:2.Rmd
211 lines (161 loc) · 4.66 KB
/
ggplot_day1:2.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
---
title: "ggplot2_tutorial_2017"
author: "Chad Fibke"
date: '2017-10-13'
output: github_document
---
```{r}
library(tidyverse)
library(gapminder)
library(RColorBrewer)
```
# Day.1 Oct13
```{r scatter.plot, echo=TRUE}
summary(mpg)
glimpse(mpg)
??mpg
mpg %>%
ggplot(aes( x = displ, y = cty))+
geom_point()+
theme_classic()+
geom_rect(aes(xmin = 3, xmax = 4, ymin = 15, ymax = 20), alpha = 0.0001, color = "red")
# This can help to highlight any data!
#xmin - (required) left edge of rectangle
#xmax - (required) right edge of rectangle
#ymin - (required) bottom edge of rectangle
#ymax - (required) top edge of rectangle
#size - (default: 0.5) line width of the rectangle's outline
#linetype - (default: 1=solid) line type of the rectangle's outline
#color - (default: NA=no outline) color of the rectangle's outline
#fill - (default: "grey20") fill color of the rectangle
#alpha - (default: 1=opaque) transparency of the rectangle's fil
```
```{r}
mpg %>%
ggplot(aes(x = displ, y = class, color = trans))+
geom_point()+
facet_wrap(~class)
```
```{r}
#dimonds and cuts
glimpse(diamonds)
# so we will use a bar chart if we are looking at cut
diamonds %>%
ggplot(aes(x = cut, y = price))+
geom_boxplot(aes(fill = cut))+
theme_classic()+
facet_wrap(~cut, scales = "free")
diamonds %>%
ggplot(aes(x = cut))+
geom_bar(aes(fill = cut))+
theme_classic()+
theme(legend.box.background = element_rect())
# remeber we have to tell r the element line, rect or text\
```
some scatter plot
```{r}
mpg %>%
ggplot(aes(x = displ, y = hwy))+
geom_point()+
geom_smooth(color = "red", span = 0.2) # the smaller the span then the sliding widow will be very small!
```
```{r}
mpg %>%
ggplot(aes( x = displ))+
geom_smooth( aes(y = hwy , color = "hwy"), size = 1.3, span = 100)+
geom_smooth( aes(y = cty , color = "cty"), size = 1.3, span = 0.2)+
geom_point(aes( y = cty), color = "red")+
geom_point(aes(y=hwy), color = "blue")+
theme_classic()+
geom_hline(yintercept = mean(mpg$hwy))+
geom_hline(yintercept = mean(mpg$cty))
#If you speficy your asthetics at the begining, it is carried out for the rest of the layers. Now once we add a layer we can overright these aes **per** layer
```
# now we can extract the the graphs
```{r}
#ggsave()
```
### Here we work with some dplyr and gapminder
```{r}
gapminder %>%
ggplot(aes( x = year, y = lifeExp, group = country))+
geom_line(aes(color = continent), alpha = 0.5)+
geom_point(alpha = 0.1)+
theme_classic()+
facet_wrap(~continent, ncol = 5)+
theme(legend.box.background = element_rect(),
strip.background = element_rect(fill = "orange"),
axis.text.x = element_text(angle = 90))
#remeber the theme only going to tweak the alredy established axis and all of that1
# for facet grid (rows~columns)
#(if you see row ~ . ) the . is nothing or we can even add multiple matrix combos (.~x1 + x2)
```
# faceting
```{r}
mpg %>%
ggplot(aes( x = cyl, group = class, fill = class))+
geom_histogram(binwidth = 1, color = "black")+
facet_wrap(~class)+
theme_bw()
```
# ggplot day 2
talks about scaling:
#c("fixed", "free_x", "free_y", "free")) : 'arg' should be one of “fixed”, “free_x”, “free_y”, “free”
```{r}
mpg %>%
ggplot(aes( x = cyl, group = class, fill = class))+
geom_histogram(binwidth = 1, color = "black")+
facet_wrap(~class, scales = "free_x")+
theme_bw()
```
```{r}
msleep %>%
ggplot(aes(x = sleep_cycle, y = sleep_total, color = vore)) +
geom_point()+
ggtitle("how things sleep")+
xlab("Sleep Cycle")+
ylab("Total amount of sleep")+
scale_colour_manual( name = "Diet", #this will allow us to pick what we want in the legend
values = c("red", "blue", "green", "black"),
breaks = c("carni", "herbi", "insecti","omni"))
```
# lets add some color:
```{r}
display.brewer.all()
```
```{r}
msleep %>%
ggplot(aes(x= brainwt, fill = vore))+
geom_histogram(binwidth = 1 )+
scale_x_log10()+
scale_fill_brewer("RDGry")
```
# how to work with positioning:
```{r}
diamonds %>%
ggplot(aes(color, fill = cut))+
geom_bar(position = "dodge")
```
```{r}
mpg %>%
ggplot(aes(x= cty, y = hwy))+
geom_point()+
geom_text(aes(label = class ))+
annotate("label", 15, 40, label = "this is not a drill") #the "lable" will give us a outlined box!
```
exercise:
```{r}
msleep %>%
ggplot(aes(x = sleep_total, y =bodywt))+
geom_text(aes(label = genus, color = genus))+
scale_y_log10()+
theme(legend.position = "")
```
#exerice with overplotting:
```{r}
diamonds %>%
ggplot(aes(x = carat, y = price))+
geom_point(alpha = 0.2)
ggsave("plot.pdf", device = "pdf")
```
![](plot.pdf)