-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4-actual.R
322 lines (247 loc) · 10.9 KB
/
day4-actual.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#day 4 actual
#check your folders
# data_output
# figure_output
library(tidyverse)
surveys <- read_csv('data/portal_data_joined.csv')
#Warm-up
# Q(a) Create a new dataframe from surveys ,
# remove observations with missing(weight,hindfoot_length and sex)
surveys_complete <- surveys %>%
filter(!is.na(weight) & !is.na(hindfoot_length) & !is.na(sex))
# verify the count (a)
surveys %>%
filter(!is.na(weight) & !is.na(hindfoot_length) & !is.na(sex)) %>% dim()
# Q(b) Create a new dataframe - select only those species
# for which the count >=50
survey_species_50 <- surveys %>%
count(species_id) %>%
filter( n >= 50)
# Q(c) Filter only those species from (b) with criteria from (a),
# and write it to folder "data_output/surveys_complete.csv"
surveys_complete <- surveys_complete %>%
filter(species_id %in% survey_species_50$species_id)
# verify that the row count is 30463 and column count is 13
write_csv(surveys_complete,"data_output/surveys_complete.csv")
ggplot(data=surveys_complete , mapping = aes(x=weight,y=hindfoot_length)) +
geom_line()
# point plot
ggplot(data=surveys_complete , mapping = aes(x=weight,y=hindfoot_length)) +
geom_point()
# using pipe%>%
surveys_complete %>% ggplot(aes(x=weight,y=hindfoot_length)) + geom_point()
#changing the transparency
surveys_complete %>% ggplot(aes(x=weight,y=hindfoot_length)) +
geom_point(alpha=0.1)
# color my plots
surveys_complete %>% ggplot(aes(x=weight,y=hindfoot_length)) +
geom_point(alpha=0.1,color="blue")
# coloring by species
surveys_complete %>% ggplot(aes(x=weight,y=hindfoot_length)) +
geom_point(alpha=0.1,aes(color=species_id))
#glo
surveys_complete %>% ggplot(aes(x=weight,y=hindfoot_length,color=species_id)) +
geom_point(alpha=0.1)
# challenge
#2
#Challenge
#Q Scatter plot of weight(y-axis) over species _id(x-axis),
# with plot types(from the dataframe) showing in diff color
#
# plot_type of the dataframe surveys_complete$plot_type
surveys_complete %>% ggplot(aes(x=species_id,y=weight,color=plot_type)) +
geom_point(alpha=0.1)
#Please fill the post-session survey
#https://www.surveymonkey.com/r/swc_post_workshop_v1?workshop_id=2018-08-28-UW
#Visualizing distributions
# Box plot
surveys_complete %>% ggplot(aes(x=species_id,y=weight,color=plot_type)) +
geom_boxplot()
# Box plot with no color
surveys_complete %>% ggplot(aes(x=species_id,y=weight)) +
geom_boxplot()
# jitter
surveys_complete %>% ggplot(aes(x=species_id,y=weight)) +
geom_boxplot(alpha=0) + geom_jitter(alpha=0.2, color="tomato")
#change the order
surveys_complete %>% ggplot(aes(x=species_id,y=weight)) +
geom_jitter(alpha=0.2, color="tomato") +
geom_boxplot(alpha=0)
# violin plots(multimodal distributions)
surveys_complete %>% ggplot(aes(x=species_id,y=weight)) +
geom_jitter(alpha=0.2, color="tomato") +
geom_violin()
# scaling x or y axis
surveys_complete %>% ggplot(aes(x=species_id,y=weight)) +
geom_jitter(alpha=0.2, color="tomato") +
geom_violin() + scale_y_log10()
# color the point plot by plot_id
surveys_complete %>% ggplot(aes(x=species_id,y=weight)) +
geom_jitter(alpha=0.2, aes(color=plot_id)) +
geom_violin() + scale_y_log10()
class(surveys_complete$plot_id)
# coloring by plot_id
surveys_complete %>% ggplot(aes(x=species_id,y=weight)) +
geom_jitter(alpha=0.2, aes(color=as.character(plot_id))) +
geom_violin() + scale_y_log10()
#time series - how number of species has changed over time
surveys_complete %>% count(year,species_id) %>%
ggplot(aes(x=year,y=n)) + geom_line()
# issue with previous - overlayed all on one
# want to see by species as that is the intent
surveys_complete %>% count(year,species_id) %>%
ggplot(aes(x=year,y=n,color=species_id)) + geom_line()
#time series - how weights of my species has changed over time
surveys_complete %>% group_by(year,species_id) %>%
summarise(mean_weight = mean(weight)) %>%
ggplot(aes(x=year,y=mean_weight,color=species_id)) + geom_line()
# Presentation of plots
# faceting - split one plot into multiple plots based on a factor(categorical variable)
surveys_complete %>% count(year,species_id) %>%
ggplot(aes(x=year,y=n)) + geom_line() +
facet_wrap(~species_id)
# specify the column name for facets
surveys_complete %>% count(year,species_id) %>%
ggplot(aes(x=year,y=n)) + geom_line() +
facet_wrap(c('species_id'))
# lines for each sex within a species
surveys_complete %>% count(year,sex, species_id) %>%
ggplot(aes(x=year,y=n,color=sex)) + geom_line() +
facet_wrap(~species_id)
# changing the presentation style
surveys_complete %>% count(year,sex, species_id) %>%
ggplot(aes(x=year,y=n,color=sex)) + geom_line() +
facet_wrap(~species_id) + theme_bw()
# remove the grid
surveys_complete %>% count(year,sex, species_id) %>%
ggplot(aes(x=year,y=n,color=sex)) + geom_line() +
facet_wrap(~species_id) + theme_bw() + theme(panel.grid = element_blank())
# Themes
surveys_complete %>% count(year,sex, species_id) %>%
ggplot(aes(x=year,y=n,color=sex)) + geom_line() +
facet_wrap(~species_id) + theme_classic()
# Theme minimal
surveys_complete %>% count(year,sex, species_id) %>%
ggplot(aes(x=year,y=n,color=sex)) + geom_line() +
facet_wrap(~species_id) + theme_minimal()
# no visualization
surveys_complete %>% count(year,sex, species_id) %>%
ggplot(aes(x=year,y=n,color=sex)) + geom_line() +
facet_wrap(~species_id) + theme_void()
# labels
surveys_complete %>% count(year,sex, species_id) %>%
ggplot(aes(x=year,y=n,color=sex)) + geom_line() +
facet_wrap(~species_id) + theme_minimal() +
labs(x="Year of observation", y="Number of Species",title="Yearly Species distribution")
# facet.grid() gives the option to specify the rows and columns
#only one row, multiple columns
#. is one row
surveys_complete %>% count(year,sex, species_id) %>%
ggplot(aes(x=year,y=n,color=sex)) + geom_line() +
facet_grid(.~species_id) + theme_minimal() +
labs(x="Year of observation", y="Number of Species",title="Yearly Species distribution")
# multiple rows
surveys_complete %>% count(year,sex, species_id) %>%
ggplot(aes(x=year,y=n,color=sex)) + geom_line() +
facet_grid(species_id~.) + theme_minimal() +
labs(x="Year of observation", y="Number of Species",title="Yearly Species distribution")
# sex and species ( rows - sex, and columns as species)
# multiple rows
surveys_complete %>% count(year,sex, species_id) %>%
ggplot(aes(x=year,y=n,color=sex)) + geom_line() +
facet_grid(sex~species_id) + theme_minimal() +
labs(x="Year of observation", y="Number of Species",title="Yearly Species distribution")
# fix the x labels
surveys_complete %>% count(year,sex, species_id) %>%
ggplot(aes(x=year,y=n,color=sex)) + geom_line() +
facet_grid(sex~species_id) + theme(axis.text.x = element_text(size = 10,color = "red",angle = 90)) +
labs(x="Year of observation", y="Number of Species",title="Yearly Species distribution") +
# Define your own themes
# justifying the plot title
theme_ideal <- theme(axis.text.x = element_text(size = 10,color = "red",angle = 90),
axis.text.y = element_text(size=10,color="blue"),
text= element_text(size=20),
axis.title = element_text(size=25),
plot.title = element_text(size=25,hjust = 0.5)
)
#writing functions which gives multiple themes
my_t <- function ()
{
theme_ideal <- theme(axis.text.x = element_text(size = 10,color = "red",angle = 90),
axis.text.y = element_text(size=10,color="blue"),
text= element_text(size=20),
axis.title = element_text(size=25),
plot.title = element_text(size=25,hjust = 0.5)
)
return(theme_ideal)
}
# talk about color scales (using scale color manual)
surveys_complete %>% group_by(sex,year,species_id) %>%
summarise(mean_weight = mean(weight)) %>%
ggplot(aes(x=year,y=mean_weight,color=sex)) + geom_line() +
scale_color_manual(values = c('red','blue'))
# explore rgb
# color brewer palletes (Cynthia Brewer)
surveys_complete %>% group_by(sex,year,species_id) %>%
summarise(mean_weight = mean(weight)) %>%
ggplot(aes(x=year,y=mean_weight,color=sex)) + geom_line() +
scale_color_brewer(palette = 'Set1')
#lts plot surveys complete
#continous color palette
surveys_complete %>%
ggplot(aes(x=weight,y=hindfoot_length,color=year)) + geom_point(alpha=0.1) +
scale_color_continuous(low='white', high = 'dark red')
#modify the color scale
surveys_complete %>%
ggplot(aes(x=weight,y=hindfoot_length,color=year)) + geom_point(alpha=0.1) +
scale_color_distiller(palette = 'Set1')
##modifying legends
surveys_complete %>%
ggplot(aes(x=weight,y=hindfoot_length,color=year)) + geom_point(alpha=0.1) +
scale_color_distiller(palette = 'Set1',name="year of observation")
# filling the difference between two lines
#add paneling
library(gridExtra)
# or you could use 'cowplot'
# plot between two years
species_box <- surveys_complete %>%
ggplot(aes(x=species_id,y=weight)) + geom_boxplot() + scale_y_log10() + theme_classic()
species_count <- surveys_complete %>% count(year,species_id) %>%
ggplot(aes(x=year,y=n,color=species_id)) + geom_line() + theme_classic()
grid.arrange(species_box,species_count, ncol=2,widths= c(1,1.5))
# call your func with theme
# apply your own theme as a funtion
surveys_complete %>% count(year,sex, species_id) %>%
ggplot(aes(x=year,y=n,color=sex)) + geom_line() +
facet_grid(species_id~.) + my_t() +
labs(x="Year of observation", y="Number of Species",title="Yearly Species distribution")
# apply your own theme
surveys_complete %>% count(year,sex, species_id) %>%
ggplot(aes(x=year,y=n,color=sex)) + geom_line() +
facet_grid(species_id~.) + theme_ideal +
labs(x="Year of observation", y="Number of Species",title="Yearly Species distribution")
# ggsave() - save your plots
#
dist_by_species <- surveys_complete %>% count(year,sex, species_id) %>%
ggplot(aes(x=year,y=n,color=sex)) + geom_line() +
facet_grid(species_id~.) + theme_ideal +
labs(x="Year of observation", y="Number of Species",title="Yearly Species distribution")
#modifying legends
dist_by_species <- surveys_complete %>% count(year,sex, species_id) %>%
ggplot(aes(x=year,y=n,color=sex)) + geom_line() +
facet_grid(species_id~.) + theme_ideal +
labs(x="Year of observation", y="Number of Species",title="Yearly Species distribution")
ggsave("fig_output/dist_by_species.png",dist_by_species)
png(filename = "fig_output/dis_by_species_2.png")
surveys_complete %>% count(year,sex, species_id) %>%
ggplot(aes(x=year,y=n,color=sex)) + geom_line() +
facet_grid(species_id~.) + theme_ideal +
labs(x="Year of observation", y="Number of Species",title="Yearly Species distribution")
dev.off()
#pdf save
pdf(file ="fig_output/dis_by_species_2.pdf")
surveys_complete %>% count(year,sex, species_id) %>%
ggplot(aes(x=year,y=n,color=sex)) + geom_line() +
facet_grid(species_id~.) + theme_ideal +
labs(x="Year of observation", y="Number of Species",title="Yearly Species distribution")
dev.off()