-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworksheet-5.R
144 lines (115 loc) · 2.91 KB
/
worksheet-5.R
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
## Getting started
library(dplyr)
library(...)
animals <- read.csv(..., na.strings = '') %>%
filter(!is.na(species_id), !is.na(sex), !is.na(weight))
## Constructing layered graphics in ggplot
ggplot(...,
...) +
...
ggplot(data = animals,
aes(x = species_id, y = weight)) +
...
ggplot(data = animals,
aes(x = species_id, y = weight)) +
geom_boxplot() ...
geom_point(...,
...,
...)
ggplot(data = animals,
aes(x = species_id, y = weight, ...)) +
geom_boxplot() +
geom_point(stat = 'summary',
fun.y = 'mean')
## Exercise 1
...
## Adding a regression line
levels(animals$sex) <- c('Female', 'Male')
animals_dm <- filter(animals, ...)
ggplot(...,
aes(x = year, y = weight)) +
geom_point(...,
size = 3,
stat = 'summary',
fun.y = 'mean') +
...
ggplot(data = animals_dm,
aes(x = year, y = weight)) +
geom_point(aes(shape = sex),
size = 3,
stat = 'summary',
fun.y = 'mean') +
geom_smooth(...)
ggplot(data = animals_dm,
aes(...,
...,
...) +
geom_point(aes(shape = sex),
size = 3,
stat = 'summary',
fun.y = 'mean') +
geom_smooth(method = 'lm')
# Storing and re-plotting
year_wgt <- ggplot(data = animals_dm,
aes(x = year,
y = weight,
color = sex)) +
geom_point(aes(shape = sex),
size = 3,
stat = 'summary',
fun.y = 'mean') +
geom_smooth(method = 'lm')
year_wgt +
...
year_wgt <- year_wgt +
scale_color_manual(...)
year_wgt
## Exercise 2
...
## Axes, labels and themes
histo <- ggplot(data = animals_dm,
aes(x = weight, fill = sex)) +
geom_...
histo
histo <- histo +
...(title = 'Dipodomys merriami weight distribution',
x = 'Weight (g)',
y = 'Count') +
scale_x_continuous(limits = c(20, 60),
breaks = c(20, 30, 40, 50, 60))
histo
histo <- histo +
theme_bw() +
theme(legend.position = c(0.2, 0.5),
plot.title = ...,
... = element_text(...),
... = element_text(size = 13, vjust = 0))
histo
## Facets
animals_common <- filter(animals, ...)
ggplot(data = ...,
...) +
geom_histogram() +
...
labs(title = "Weight of most common species",
x = "Count",
y = "Weight (g)")
ggplot(data = animals_common,
aes(x = weight)) +
geom_histogram(...,
...) +
geom_histogram() +
facet_wrap( ~ species_id) +
labs(title = "Weight of most common species",
x = "Count",
y = "Weight (g)")
ggplot(data = animals_common,
aes(x = weight, ...)) +
geom_histogram(...) +
facet_wrap( ~ species_id) +
labs(title = "Weight of most common species",
x = "Count",
y = "Weight (g)") +
guides(fill = FALSE)
## Exercise 3
...