---
title: "R Notebook"
output: html_notebook
---


```{r}
library(tidyverse)
library(easystats)
library(ggstatsplot)

library(emmeans)
library(lme4)
library(lmerTest)

library(survival)
library(survminer)

library(interactions)
library(jtools)
library(sjPlot)
library(huxtable)
library(xlsx)

library(scales)

library(ggeffects)

library(glmulti)

library(RColorBrewer)

library(huxtable)

library(patchwork)
```



```{r}

dogaw<-read.csv("dog_aw_final_forpub_NEE.csv", sep = "\t", dec = ",", na.strings = c("", "NA", "#N/A"), encoding = "UTF-8")

dogaw <- dogaw %>% 
  filter(name != "Vince") %>% 
  mutate_if(is.character, as.factor) %>% 
  mutate(Context = fct_relevel(Context, "Distress")) %>% 
  mutate(species = fct_relevel(species, "Dog")) %>% 
  mutate(Group = factor(paste(species, type))) %>% 
  mutate(age_cut = cut(Age, quantile(Age, c(0, 0.33, 0.67, 1), na.rm = T), labels = c("0.7-3.0y", "3.0-6.0y", "6.0-14y"), include.lowest = T)) %>% 
  mutate(age_cut2 = cut(Age, c(0.7, 4.7, 9.4, 14), labels = c("0.7-4.7y", "4.7-9.4y", "9.4-14y"), include.lowest = T)) %>% 
  mutate(Initial_reaction_f = factor(Initial_reaction, labels = c("Withdrawal", "Approach"))) %>% 
  mutate(Group = fct_relevel(Group, "Dog Vocalization", "Chimpanzee Vocalization", "Human Vocalization"))

attr(dogaw$species, "label") <- "Caller species"
attr(dogaw$Group, "label") <- "Sound category"

plyr::count(dogaw, c("Group", "age_cut"))
plyr::count(dogaw, c("Group", "age_cut2"))

dogaw_d <- subset(dogaw, Group == "Dog Vocalization")
dogaw_nd <- subset(dogaw, Group != "Dog Vocalization")



```



```{r}

plyr::count(dogaw, "Group")
plyr::count(dogaw, "Sex")
plyr::count(dogaw, "Context")

plyr::count(dogaw, c("Sex", "Context"))

plyr::count(dogaw, c("Group", "Context"))
plyr::count(dogaw, c("Group", "Context", "Sex"))

```



```{r}
range(dogaw$Age, na.rm = T)
mean(dogaw$Age, na.rm = T)
sd(dogaw$Age, na.rm = T)
```

# Check sub-group balances

```{r fig.height=4, fig.width=8}


ggplot(dogaw, aes(Group, Age))+
  geom_boxplot(outlier.alpha = 0)+
  geom_jitter(width = 0.2, height = 0.2, alpha = 0.5)

ggplot(dogaw, aes(Group, Age, colour = Context))+
  geom_boxplot(outlier.alpha = 0)+
  geom_jitter(position = position_jitterdodge(jitter.width = 0.2, jitter.height = 0.2), alpha = 0.5)

ggplot(dogaw, aes(Context, Age, colour = Group))+
  geom_boxplot(outlier.alpha = 0)+
  geom_jitter(position = position_jitterdodge(jitter.width = 0.2, jitter.height = 0.2), alpha = 0.5)

ggplot(dogaw, aes(Age, fill = Group))+
  geom_density(alpha = 0.5, position = "stack")+
  facet_grid(.~Context)+
  labs(x = "Age (years)", y = "Density") + 
  labs(fill = "Sound category") +
  theme(panel.background = element_rect(fill = NA), panel.grid.major = element_line(colour = "lightgrey",linetype = "solid"), panel.grid.minor = element_line(colour = "grey",linetype = "dotted"))+
  theme(axis.line.x=element_line(linewidth=0.5, colour="black", linetype="solid"), axis.line.y=element_line(linewidth=0.5, colour="black", linetype="solid"))+
  theme(plot.margin=unit(c(10, 15, 10, 10), "points"))+
  theme(axis.title.y = element_text(size = rel(1.5), angle = 90)) + 
  theme(axis.title.x = element_text(size = rel(1.5))) + 
  theme(axis.text = element_text(size = "14"))

```


```{r}
agemod <- lm(Age ~ Group*Context, dogaw)
drop1(agemod, test = "F")

tab_model(agemod, dv.labels = c("Age differences across subgroups"), show.stat = T, digits = 3, collapse.ci = F, auto.label = T)

agemod <- update(agemod, .~. - Group:Context)
drop1(agemod, test = "F")

agemod <- update(agemod, .~. - Group)
drop1(agemod, test = "F")


agemod <- update(agemod, .~. - Context)
drop1(agemod, test = "F")

summ(agemod)

```


```{r fig.height=13, fig.width=9}
grouped_ggbarstats(dogaw, Sex, Context, grouping.var = Group)
```


# Study 1: Conspecific vocalizations

## Motivation vs valence


```{r}
appr_chk <- glm(Initial_reaction ~ Motivation + Valence,
            family = binomial(link = "logit"), data=dogaw_d, na.action = "na.omit")

check_model(appr_chk)

drop1(appr_chk, test = "Chisq")

summ(appr_chk, exp = T)
```

```{r}

best <- glmulti(Initial_reaction ~ Motivation + Valence, data=dogaw_d, crit = "aicc", level = 1, method = "h", family = binomial, fitfunction = glm, confsetsize = 100)

plot(best, type = "s")

weightable(best)

drop1(best@objects[[1]], test = "Chisq")
summary(best@objects[[1]])

model_parameters(best@objects[[1]], exponentiate = TRUE, digits = 3, ci_digits = 3)

```

```{r}

mot_mod <- best@objects[[1]]
mot_val_mod <- best@objects[[2]]
null_mod <- best@objects[[3]]
val_mod <- best@objects[[4]]
                      

compare_performance(mot_mod, mot_val_mod, null_mod, val_mod,
               rank = T)

compare_models(mot_mod, mot_val_mod, null_mod, val_mod,
               exponentiate = T, select = "ci_p")
```



```{r fig.height=5, fig.width=5}

ph <- tidy(confint(emmeans(best@objects[[1]], pairwise ~ Motivation, type = "response")$emmeans))

d_bin <- ggplot(subset(dogaw_d, !is.na(Initial_reaction_f))) + 
  geom_bar(aes(Motivation, fill = Initial_reaction_f), position = "fill")+
  geom_point(data = ph, aes(x=Motivation, y=prob))+
  geom_errorbar(data = ph, aes(x = Motivation, ymin=conf.low, ymax=conf.high, xmin = 0, xmax = 0),width=0)+
  geom_hline(yintercept=0.5,linewidth=0.3, linetype="dashed")+
  theme(legend.justification=c(1,1))+
   labs(x = "Motivation state", y = "Approach-withdrawal ratio") + 
   labs(fill = "Reaction") + 
  #scale_x_discrete(labels = c("distress", "agonistic", "playful/comfort"))+
  scale_fill_brewer(labels =  c("withdrawal","approach"), palette = "Set2", na.translate = T)+
  theme(panel.background = element_rect(fill = NA), panel.grid.major = element_line(colour = "lightgrey",linetype = "solid"), panel.grid.minor = element_line(colour = "grey",linetype = "dotted"))+
  theme(axis.line.x=element_line(linewidth=0.5, colour="black", linetype="solid"), axis.line.y=element_line(linewidth=0.5, colour="black", linetype="solid"))+
  theme(plot.margin=unit(c(10, 15, 10, 10), "points"))+
  theme(axis.title.y = element_text(size = rel(1.5), angle = 90)) + 
  theme(axis.title.x = element_text(size = rel(1.5))) + 
  theme(axis.text = element_text(size = 14))
  #facet_grid(.~group)+
  #theme(axis.text.x = element_text(angle = 30, hjust = 1))

```


```{r fig.height=5, fig.width=5}
ph <- tidy(confint(emmeans(best@objects[[4]], pairwise ~ Valence, type = "response")$emmeans))

d_bin_val <- ggplot(subset(dogaw_d, !is.na(Initial_reaction_f))) + 
  geom_bar(aes(Valence, fill = Initial_reaction_f), position = "fill")+
  geom_point(data = ph, aes(x=Valence, y=prob))+
  geom_errorbar(data = ph, aes(x = Valence, ymin=conf.low, ymax=conf.high, xmin = 0, xmax = 0),width=0)+
  geom_hline(yintercept=0.5,linewidth=0.3, linetype="dashed")+
  theme(legend.justification=c(1,1))+
   labs(x = "Valence state", y = "Approach-withdrawal ratio") + 
   labs(fill = "Reaction") + 
  #scale_x_discrete(labels = c("distress", "agonistic", "playful/comfort"))+
  scale_fill_brewer(labels =  c("withdrawal","approach"), palette = "Set2", na.translate = T)+
  theme(panel.background = element_rect(fill = NA), panel.grid.major = element_line(colour = "lightgrey",linetype = "solid"), panel.grid.minor = element_line(colour = "grey",linetype = "dotted"))+
  theme(axis.line.x=element_line(linewidth=0.5, colour="black", linetype="solid"), axis.line.y=element_line(linewidth=0.5, colour="black", linetype="solid"))+
  theme(plot.margin=unit(c(10, 15, 10, 10), "points"))+
  theme(axis.title.y = element_text(size = rel(1.5), angle = 90)) + 
  theme(axis.title.x = element_text(size = rel(1.5))) + 
  theme(axis.text = element_text(size = 14))
  #facet_grid(.~group)+
  #theme(axis.text.x = element_text(angle = 30, hjust = 1))
```



## Latencies

### Approach

```{r}
glmulti.coxph.out <-
    glmulti(Surv(Approach_latency, Approach_occurrence) ~ Context + Sex + Age, data = dogaw_d,
            level = 2,               # No interaction considered
            method = "h",            # Exhaustive approach
            crit = "aicc",            # AIC as criteria
            confsetsize = 100,         # Keep 5 best models
            plotty = T, report = T,  # No plot or interim reports
            fitfunction = "coxph")   # coxph function

weightable(glmulti.coxph.out)

write.xlsx2(weightable(glmulti.coxph.out), "Table S1.xlsx", "dog_approach")

plot(glmulti.coxph.out, type = "s")

drop1(glmulti.coxph.out@objects[[1]], test = "Chisq")
summary(glmulti.coxph.out@objects[[1]])

tab_model(glmulti.coxph.out@objects[[1]], show.stat = T, digits = 3, collapse.ci = F, auto.label = T, show.reflvl = T)

```

```{r}
emmeans(glmulti.coxph.out@objects[[1]], pairwise ~ Context, type = "response")
confint(emmeans(glmulti.coxph.out@objects[[1]], pairwise ~ Context, type = "response")$contrasts)
```


```{r}
lat_1 <- survfit(Surv(Approach_latency, Approach_occurrence)~Context, data=dogaw_d)

d_a <- ggsurvplot(lat_1, fun="event", conf.int = T, censor = T,
                 xlab = "Approach latency (s)", ylab = "Ratio of approach reactions", palette = "Set2", legend.title = "Context",
          conf.int.alpha = 0.2, legend.labs = c("Distress", "Agonistic", "Playful/comfort"))

d_a

```



### Withdrawal



```{r}
glmulti.coxph.out <-
    glmulti(Surv(Withdrawal_latency, Withdrawal_occurrence) ~ Context + Sex + Age, data = dogaw_d,
            level = 1,               # No interaction considered
            method = "h",            # Exhaustive approach
            crit = "aic",            # AIC as criteria
            confsetsize = 100,         # Keep 5 best models
            plotty = T, report = T,  # No plot or interim reports
            fitfunction = "coxph")   # coxph function

weightable(glmulti.coxph.out)

write.xlsx2(weightable(glmulti.coxph.out), "Table S1.xlsx", "dog_withdrawal", append=TRUE)


plot(glmulti.coxph.out, type = "s")

drop1(glmulti.coxph.out@objects[[1]], test = "Chisq")

summary(glmulti.coxph.out@objects[[1]])

tab_model(glmulti.coxph.out@objects[[1]], show.stat = T, digits = 3, collapse.ci = F, auto.label = T, show.reflvl = T)


```


```{r}
emmeans(glmulti.coxph.out@objects[[1]], pairwise ~ Context, type = "response")
confint(emmeans(glmulti.coxph.out@objects[[1]], pairwise ~ Context, type = "response")$contrasts)
```


```{r}
lat_1 <- survfit(Surv(Withdrawal_latency, Withdrawal_occurrence)~Context, data=dogaw_d)

d_w <- ggsurvplot(lat_1, fun="event", conf.int = T, censor = T,
                 xlab = "Withdrawal latency (s)", ylab = "Ratio of withdrawal reactions", legend.title = "Context", palette = "Set2",
          conf.int.alpha = 0.2, legend.labs = c("Distress", "Agonistic", "Playful/comfort"))

d_w
```


```{r fig.height=4, fig.width=7}

d_a$plot + d_w$plot + 
  ylim(c(0.0, 1.0)) +
  plot_annotation(tag_levels = 'A') +
  plot_layout(guides = "collect") & 
  theme(plot.tag.position = c(0,0), legend.position = "top")

ggsave("aw_lat_cont.png")
```




# Study 2: Cross-species effects

```{r}

appr_null <- glm(Initial_reaction ~ 1,
            family = binomial(link = "logit"), data=dogaw_nd, na.action = "na.omit")

appr_g <- glm(Initial_reaction ~ Group,
            family = binomial(link = "logit"), data=dogaw_nd, na.action = "na.omit")

appr_v <- glm(Initial_reaction ~ Valence,
            family = binomial(link = "logit"), data=dogaw_nd, na.action = "na.omit")

appr_s <- glm(Initial_reaction ~ Motivation,
            family = binomial(link = "logit"), data=dogaw_nd, na.action = "na.omit")

appr_s_v <- glm(Initial_reaction ~ Motivation + Valence,
            family = binomial(link = "logit"), data=dogaw_nd, na.action = "na.omit")

appr_gv <- glm(Initial_reaction ~ Group + Valence,
            family = binomial(link = "logit"), data=dogaw_nd, na.action = "na.omit")

appr_gs <- glm(Initial_reaction ~ Group + Motivation,
            family = binomial(link = "logit"), data=dogaw_nd, na.action = "na.omit")

appr_vi <- glm(Initial_reaction ~ Group*Valence,
            family = binomial(link = "logit"), data=dogaw_nd, na.action = "na.omit")

appr_si <- glm(Initial_reaction ~ Group*Motivation,
            family = binomial(link = "logit"), data=dogaw_nd, na.action = "na.omit")

appr_s_vi <- glm(Initial_reaction ~ Group*Valence + Motivation,
            family = binomial(link = "logit"), data=dogaw_nd, na.action = "na.omit")

appr_v_si <- glm(Initial_reaction ~ Group*Motivation + Valence,
            family = binomial(link = "logit"), data=dogaw_nd, na.action = "na.omit")

appr_full <- glm(Initial_reaction ~ Group*Motivation + Valence + Group:Valence,
            family = binomial(link = "logit"), data=dogaw_nd, na.action = "na.omit")

compare_performance(appr_vi, appr_si)

compare_performance(appr_null, appr_vi, appr_si, appr_s_vi, appr_v_si, appr_gv, appr_gs, appr_full, 
                    appr_g, appr_v, appr_s, appr_s_v, rank = F)

compare_performance(appr_vi, appr_si, appr_s_vi, appr_v_si, appr_gv, appr_gs, appr_full, 
                    appr_g, appr_v, appr_s, appr_s_v, rank = T)

drop1(appr_full, test = "Chisq")

drop1(appr_v, test = "Chisq")

```

```{r fig.width=15}
compare_models(appr_vi, appr_si, appr_s_vi, appr_v_si, appr_gv, appr_gs, appr_full, 
                    appr_g, appr_v, appr_s, appr_s_v,
               exponentiate = T, select = "ci_p")
```



```{r}
best <- glmulti(Initial_reaction ~ Group + Motivation + Valence, data=dogaw_nd, crit = "aicc", level = 2, method = "h", family = binomial, fitfunction = glm, confsetsize = 100)

plot(best, type = "s")

weightable(best)

drop1(best@objects[[1]], test = "Chisq")
summary(best@objects[[1]])

model_parameters(best@objects[[1]], exponentiate = TRUE, digits = 3, ci_digits = 3)

tab_model(glmulti.coxph.out@objects[[1]], show.stat = T, digits = 3, collapse.ci = F, auto.label = T, show.reflvl = T)
```




```{r fig.height=5, fig.width=5}

ph <- tidy(confint(emmeans(appr_s, pairwise ~ Motivation, type = "response")$emmeans))

nd_bin <- ggplot(subset(dogaw_nd, !is.na(Initial_reaction_f))) + 
  geom_bar(aes(Motivation, fill = Initial_reaction_f), position = "fill")+
  geom_point(data = ph, aes(x=Motivation, y=prob))+
  geom_errorbar(data = ph, aes(x = Motivation, ymin=conf.low, ymax=conf.high, xmin = 0, xmax = 0),width=0)+
  geom_hline(yintercept=0.5,linewidth=0.3, linetype="dashed")+
  theme(legend.justification=c(1,1))+
   labs(x = "Motivation state", y = "Approach-withdrawal ratio") + 
   labs(fill = "Reaction") + 
  #scale_x_discrete(labels = c("distress", "agonistic", "playful/comfort"))+
  scale_fill_brewer(labels =  c("withdrawal","approach"), palette = "Set2", na.translate = T)+
  theme(panel.background = element_rect(fill = NA), panel.grid.major = element_line(colour = "lightgrey",linetype = "solid"), panel.grid.minor = element_line(colour = "grey",linetype = "dotted"))+
  theme(axis.line.x=element_line(linewidth=0.5, colour="black", linetype="solid"), axis.line.y=element_line(linewidth=0.5, colour="black", linetype="solid"))+
  theme(plot.margin=unit(c(10, 15, 10, 10), "points"))+
  theme(axis.title.y = element_text(size = rel(1.5), angle = 90)) + 
  theme(axis.title.x = element_text(size = rel(1.5))) + 
  theme(axis.text = element_text(size = 14))
  #facet_grid(.~Group)+
  #theme(axis.text.x = element_text(angle = 30, hjust = 1))
```


```{r fig.height=5, fig.width=5}

ph <- tidy(confint(emmeans(appr_v, pairwise ~ Valence, type = "response")$emmeans))

nd_bin_val <- ggplot(subset(dogaw_nd, !is.na(Initial_reaction_f))) + 
  geom_bar(aes(Valence, fill = Initial_reaction_f), position = "fill")+
  geom_point(data = ph, aes(x=Valence, y=prob))+
  geom_errorbar(data = ph, aes(x = Valence, ymin=conf.low, ymax=conf.high, xmin = 0, xmax = 0),width=0)+
  geom_hline(yintercept=0.5,linewidth=0.3, linetype="dashed")+
  theme(legend.justification=c(1,1))+
   labs(x = "Valence state", y = "Approach-withdrawal ratio") + 
   labs(fill = "Reaction") + 
  #scale_x_discrete(labels = c("distress", "agonistic", "playful/comfort"))+
  scale_fill_brewer(labels =  c("withdrawal","approach"), palette = "Set2", na.translate = T)+
  theme(panel.background = element_rect(fill = NA), panel.grid.major = element_line(colour = "lightgrey",linetype = "solid"), panel.grid.minor = element_line(colour = "grey",linetype = "dotted"))+
  theme(axis.line.x=element_line(linewidth=0.5, colour="black", linetype="solid"), axis.line.y=element_line(linewidth=0.5, colour="black", linetype="solid"))+
  theme(plot.margin=unit(c(10, 15, 10, 10), "points"))+
  theme(axis.title.y = element_text(size = rel(1.5), angle = 90)) + 
  theme(axis.title.x = element_text(size = rel(1.5))) + 
  theme(axis.text = element_text(size = 14))
  #facet_grid(.~Group)+
  #theme(axis.text.x = element_text(angle = 30, hjust = 1))
```


```{r fig.height=7, fig.width=7}
(d_bin & theme(axis.title.x = element_text(hjust=5, vjust = -1), plot.title = element_text(hjust = 0.5, vjust = -1)) & ggtitle("Conspecific")) + (nd_bin & ylab(NULL) & xlab(NULL) & theme(plot.background = element_rect(fill='transparent'), plot.title = element_text(hjust = 0.5, vjust = -1)) & ggtitle("Heterospecific")) + 
(d_bin_val & theme(axis.title.x = element_text(hjust=3, vjust = -1), plot.title = element_text(hjust = 0.5, vjust = -1))) + (nd_bin_val & ylab(NULL) & xlab(NULL) & theme(plot.background = element_rect(fill='transparent'), plot.title = element_text(hjust = 0.5, vjust = -1))) + 
  #ylim(c(0.0, 1.0)) +
  plot_annotation(tag_levels = 'A') +
  plot_layout(guides = "collect") & 
  theme(plot.tag.position = c(-0.03,1.01), legend.position = "right")

ggsave("aw_bin_all.png")
```



## Latencies

### Approach


```{r}
glmulti.coxph.out <-
    glmulti(Surv(Approach_latency, Approach_occurrence) ~ Group + Context + Sex + Age + 
                 Group:Context + Group:Sex + Group:Age + Context:Sex + Context:Age + Sex:Age + Group:Context:Sex, data = dogaw_nd,
            level = 2,               # No interaction considered
            method = "h",            # Exhaustive approach
            crit = "aicc",            # AIC as criteria
            confsetsize = 100,         # Keep 5 best models
            plotty = T, report = T,  # No plot or interim reports
            fitfunction = "coxph")   # coxph function

weightable(glmulti.coxph.out)

write.xlsx2(weightable(glmulti.coxph.out), "Table S1.xlsx", "heterospecific_approach", append=TRUE)

plot(glmulti.coxph.out, type = "s")

drop1(glmulti.coxph.out@objects[[1]], test = "Chisq")
summary(glmulti.coxph.out@objects[[1]])

tab_model(glmulti.coxph.out@objects[[1]], show.stat = T, digits = 3, collapse.ci = F, auto.label = T, show.reflvl = T)
```


```{r}

emmeans(glmulti.coxph.out@objects[[1]], pairwise ~ Context|Sex, type = "response")
confint(emmeans(glmulti.coxph.out@objects[[1]], pairwise ~ Context|Sex, type = "response")$contrasts)

emmeans(glmulti.coxph.out@objects[[1]], pairwise ~ Sex|Context, type = "response")
confint(emmeans(glmulti.coxph.out@objects[[1]], pairwise ~ Sex|Context, type = "response")$contrasts)

```



```{r}

lat_1 <- survfit(Surv(Approach_latency, Approach_occurrence)~Sex, data=dogaw_nd)
ggsurvplot(lat_1, fun="event", conf.int = T, censor = T, facet.by = c("Context"),
                 xlab = "Approach latency (s)", ylab = "Ratio of approach reactions", legend.title = "Sex", palette = "Dark2",
                 conf.int.alpha = 0.2,
                 short.panel.labs = T)

lat_app <- survfit(Surv(Approach_latency, Approach_occurrence) ~ age_cut2, data = dogaw_nd)
nd_ap_ag <- ggsurvplot(lat_app, data = dogaw_nd, fun = "event", conf.int = T, censor = T,
                 xlab = "Approach latency (s)", ylab = "Ratio of approach reactions", legend.title = "Age category", palette = "Dark2",
          conf.int.alpha = 0.2, legend.labs = c("0.7-4.7y", "4.7-9.4y", "9.4-14y"), xlim = c(0, 60), ylim = c(0,1))

nd_ap_ag
```


### Withdrawal


```{r}
glmulti.coxph.out <-
    glmulti(Surv(Withdrawal_latency, Withdrawal_occurrence) ~ Group + Context + Sex + Age, data = dogaw_nd,
            level = 2,               # No interaction considered
            method = "h",            # Exhaustive approach
            crit = "aic",            # AIC as criteria
            confsetsize = 100,         # Keep 5 best models
            plotty = T, report = T,  # No plot or interim reports
            fitfunction = "coxph")   # coxph function

weightable(glmulti.coxph.out)

write.xlsx2(weightable(glmulti.coxph.out), "Table S1.xlsx", "heterospecific_withdrawal", append=TRUE)

plot(glmulti.coxph.out, type = "s")

drop1(glmulti.coxph.out@objects[[1]], test = "Chisq")

summary(glmulti.coxph.out@objects[[1]])

tab_model(glmulti.coxph.out@objects[[1]], show.stat = T, digits = 3, collapse.ci = F, auto.label = T, show.reflvl = T)

```



```{r}

lat_app <- survfit(Surv(Withdrawal_latency, Withdrawal_occurrence) ~ age_cut2, data = dogaw_nd)
nd_w_ag <- ggsurvplot(lat_app, data = dogaw_nd, fun = "event", conf.int = T, censor = T,
                 xlab = "Withdrawal latency (s)", ylab = "Ratio of withdrawal reactions", legend.title = "Age category", palette = "Dark2",
          conf.int.alpha = 0.2, legend.labs = c("0.7-4.7y", "4.7-9.4y", "9.4-14y"))

nd_w_ag
```

```{r fig.height=4, fig.width=7}
nd_ap_ag$plot + nd_w_ag$plot + 
  ylim(c(0.0, 1.0)) +
  plot_annotation(tag_levels = 'A') +
  plot_layout(guides = "collect") & 
  theme(plot.tag.position = c(0,0), legend.position = "top")

ggsave("nodog_aw_lat.png")
```


