-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlotting.Rmd
284 lines (198 loc) · 8.13 KB
/
Plotting.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
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
---
title: "Plotting Models"
author: "C.Stewart"
date: "2023-07-13"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
• do put all the model diagnostics for all the models that you present in your report in a supplementary material document.
• make plots visualizing the relationships (e.g. plots of followed individuals that show proportion that are adult female, adult male, subadult, etc...)
• You may also consider a plot that shows the proportion of individuals that were followed that were of the logical categories (adult male, adult female, subadult....etc...), and a similar plot for those that were following.
#LEADER plots
```{r}
rm(list=ls())
Miya_theme<-theme(axis.text = element_text(size=10, color="black"),
axis.title=element_text(size=10, color="black"),
axis.line = element_line(color="black"),
axis.ticks = element_line(color="black"),
legend.position = "top",
panel.background = element_rect(fill = "white", colour = "white",size = 1),
panel.grid = element_blank(),
panel.border = element_rect(colour = "black", fill=NA, size=1),
plot.background = element_rect(fill = "white", colour = "white",size = 1))
#set working directory
setwd("C:/Users/chars/OneDrive/Documents/CGS Project/Data")
#import csv file
df <- read.csv("summary.group.composition.plotting.csv")
#convert sex and age to characters
df$sex<- as.character(df$sex)
df$age<- as.character(df$age)
# Create an ordered factor variable for 'age'
df$age_category <- factor(df$age, levels = c(1, 2, 3),labels = c("Juvenile", "Subadult", "Adult"))
df$sex <- factor(df$sex, levels = c(1, 2), labels = c("Male", "Female"))
summary(df)
library(tidyverse)
```
# Exploratory plots (descriptive): LEADER
```{r}
# followship as a function of age (violin plot)
ggplot(data = df, aes(x = age_category, y = total_followed)) + geom_violin()
# followship as a function of sex (violin plot)
ggplot(data = df, aes(x = sex, y = total_followed)) + geom_violin()
# followship as a function of number of total adults
ggplot(data = df, aes(x = Total_Adults, y = total_followed)) + geom_point()
# there are some foraging groups where total adults = 0,
# should these be discounted?
ggplot(data = df, aes(x = Total_Adults, y = total_followed, colour = sex)) +
geom_count()
```
# Explore relationship between total followed and total adults
```{r}
# creating a df without the 0,0 values
# so we can examine the relationship without the zero adult points
df2 <- df[df$Total_Adults != 0, ]
LeaderPlot1<-ggplot(data = df2, aes(x = Total_Adults, y = total_followed)) +
geom_point() + stat_smooth(se = FALSE, colour = "black", method = "lm") + Miya_theme + labs(
x = "Number of adults per group",y="")
ggplot(data = df, aes(x = Total_Adults, y = total_followed)) +
geom_count() + stat_smooth(se = FALSE, colour = "black", method = "lm") + Miya_theme + labs(
x = "Number of adults per group",
y = "Number of leader occurences")
```
Create a plot with larger dots for higher density of points - hasn't worked
```{r}
df$coordinates <- paste(df$total_followed, df$Total_Adults, sep = ",")
df$frequency <- ave(df$coordinates, df$coordinates, FUN = length)
ggplot(data = df, aes(x = Total_Adults, y = total_followed)) +
geom_count() + stat_smooth(method = "lm")
#tried to create plot with LM line of best fit
```
# Facet plots
```{r}
ggplot(data = df, aes(x = Total_Adults, y = total_followed, colour = sex)) +
geom_point() +
facet_wrap(~ age_category)
#uses age as facet, but age should not be categorical?
#plot total_followed~ age and sex only
ggplot(data = df, aes(x = age, y = total_followed, colour = sex)) +
geom_violin()
LeaderPlot2<-ggplot(df, aes(x = age_category, y = total_followed, fill = sex)) +
geom_violin() +
labs(x = "Age", y="",fill = "Sex") +
scale_fill_manual(values = c("Male" = "blue", "Female" = "red")) +Miya_theme
LeaderPlot2
```
# More complex stuff
```{r}
plot_model(M2b)
plot_model(M2b, type = "pred", terms = c("age","sex","Total_Adults"),
show.values = TRUE) +
facet_grid(. ~ group)
#works but some funny output
plot_model(M2b, type = "pred", terms = c("age", "sex", "Total_Adults"),
show.values = TRUE) +
facet_wrap(sex ~ sex)
library(ggplot2)
# Obtain predicted values from the model
df$predicted <- predict(M2b, re.form = ~0)
# Create the facet plot
ggplot(df, aes(x = age, y = predicted)) +
geom_point() +
geom_line() +
facet_grid(sex ~ .) +
labs(x = "Age", y = "Predicted Values")
#kind of interesting plot ?
```
#FACET code from Miya
```{r}
library('ggplot2')
require(sjPlot)
library('ggpubr')
#create predicted plots
################################
# want four plots (facet grid) separated by sex (male, female) and age (adult, subadult)
#plot raw variables
#set variables for plots
df$sex <-as.factor(df$sex)
#plot raw values
FollowedPlot <- ggplot(data = df, aes(x = Total_Adults, y = total_followed, colour=sex)) +
geom_point() +
stat_smooth(method = "lm")+
facet_grid(sex ~ age)+
#labs(x="Age", y="Total followed")+
#scale_fill_grey()+
#scale_x_continuous(breaks = seq(0, 1, by = 1))+
Miya_theme +
theme(legend.position = c(0.2, 0.93),legend.direction = "horizontal", legend.key.size = unit(1, 'cm'),
legend.title = element_text(size=14), legend.text = element_text(size=14))
FollowedPlot
#can we combine with predicted values plots?
FollowedPlotPredicted <- plot_model(M2b, type = "pred", terms = c("age","sex"),
axis.title = c("age", "Total followed"), title = " ")
FollowedPlotPredicted
```
#FINAL leader plot
```{r}
library(egg)
library(cowplot)
# Combine plots using cowplot's plot_grid function
combined_plot <- plot_grid(LeaderPlot1,LeaderPlot2 ,ncol = 1,align = "v", axis = "lr",rel_heights = c(1,1))
y_axis_label <- ggdraw() +
draw_label("Number of leader occurences", angle = 90, size = 10) +
theme_void()
final_plot <- plot_grid(
y_axis_label,
combined_plot,
ncol = 2,
rel_widths = c(0.1, 0.9)
)
# Display the combined plot
print(final_plot)
```
#FOLLOWER plots
```{r}
# follower as a function of age (violin plot)
ggplot(data = df, aes(x = age, y = total_follower)) + geom_violin()
# follower as a function of sex (violin plot)
ggplot(data = df, aes(x = sex, y = total_follower)) + geom_violin()
# follower as a function of number of total adults
ggplot(data = df, aes(x = Total_Adults, y = total_follower)) + geom_point()
# there are some foraging groups where total adults = 0,
# should these be discounted?
ggplot(data = df, aes(x = age, y = total_follower, colour = sex)) +
geom_violin()
ggplot(df, aes(x = age_category, y = total_follower, fill = sex)) +
geom_violin() +
labs(x = "Age", y = "Total Follower", fill = "Sex") +
scale_fill_manual(values = c("Male" = "blue", "Female" = "pink")) + Miya_theme
ggplot(data = df, aes(x = total_followed, y = Total_Adults, colour = sex)) +
geom_point()
#
```
```{r}
ggplot(data = df, aes(x = Group_Size, y = total_follower)) +
geom_point()
```
```{r}
FollowerPlot <- ggplot(data = df, aes(x = Group_Size, y = total_followed, colour=sex)) +
geom_point() +
stat_smooth(method = "lm")+
facet_grid(sex ~ age)+
#labs(x="Age", y="Total followed")+
#scale_fill_grey()+
#scale_x_continuous(breaks = seq(0, 1, by = 1))+
Miya_theme +
theme(legend.position = c(0.2, 0.93),legend.direction = "horizontal", legend.key.size = unit(1, 'cm'),
legend.title = element_text(size=14), legend.text = element_text(size=14))
FollowerPlot
```
```{r}
plot(ggpredict(M2ab))
Plot <- ggarrange (M2ab$age, M2ab$sex, ncol=2, nrow=1, common.legend = TRUE, legend = "bottom")
view(Plot)
# make one plot through using a facet command as a layer to your ggplots
+ facet_wrap(~ grouping variable)
# facet_grid(groupvariable1 ~ groupvariable1)£ which would be sex¬satiation level in the Zuur data.
```