# print link to github repo if anyif (file.exists("./.git/config")) { config <-readLines("./.git/config") url <-grep("url", config, value =TRUE) url <-gsub("\\turl = |.git$", "", url)cat("\nSource code and data found at [", url, "](", url, ")", sep ="")}
# ggplot font sizebs <-12# custom functionspart_var <-function(model) { var_components_pc1 <-as.data.frame(VarCorr(model)) total_variance <-sum(var_components_pc1$vcov) var_components_pc1$proportion <- var_components_pc1$vcov / total_variancereturn(var_components_pc1)}# plot variance partitioning resultsgraph_part_var <-function(levels, data,by =NULL, response,full =TRUE,tick.labels = levels) {if (!is.null(by)) {if (full) { by_levels <-c(unique(data[, by]), "full") } else { by_levels <-unique(data[, by]) } } else by_levels <-"full"# scale responseif (is.numeric(data[, response])) { data[, response] <-scale(data[, response]) } else {stop("Response variable must be numeric") } var_by_list <-lapply(by_levels, function(x) {if (x !="full") { formula <-paste0(response, " ~ (1 | ", paste(levels, collapse =" / "), ")")cat(paste0(x, ":", formula), "\n") dat <-droplevels(data[data[, by] == x, ]) } else { dat <-droplevels(data)if (!is.null(by)) { formula <-paste0(response," ~ (1 | ",paste(levels, collapse =" / "),") + (1 | ", by,")") } else { formula <-paste0(response," ~ (1 | ",paste(levels, collapse =" / "),")") }cat("full:", formula, "\n") } model <-lmer( formula,data = dat,control =lmerControl(optimizer ="bobyqa",optCtrl =list(maxfun =2e5) ) ) var_components <-part_var(model)print(var_components) var_components <- var_components[var_components$grp !="Residual", ]if (!is.null(by)) { var_components <- var_components[var_components$grp != by, ] } var_components$level <-factor(rev(levels), levels =rev(levels))# sample sizes var_components$n <-sapply(var_components$level, function(y)length(unique(dat[, names(dat) == y]))) var_components$prop <-as.character(round(var_components$proportion, 2)) var_components$prop[var_components$prop =="0"] <-"< 0.01" var_components$prop <-paste(var_components$prop," (n = ", var_components$n,")",sep ="") var_components$by_levels <- x var_components$perc <-paste(round(var_components$proportion *100, 0),"% (n = ", var_components$n,")",sep ="" )return(var_components) }) var_by_df <-do.call(rbind, var_by_list) var_by_df$by_levels <-factor(var_by_df$by_levels, levels = by_levels)# set X tick labels xlabs <- tick.labelsnames(xlabs) <- levels gg_out <-ggplot(var_by_df, aes(x = level, y = proportion, fill = level)) +geom_bar(stat ="identity") +theme_classic(base_size =20) +labs(x ="Level", y ="Explained variance (%)", fill ="Factor") +facet_wrap(~ by_levels, ncol =1) +theme(axis.text.x =element_text(angle =45,vjust =1,hjust =1 )) +geom_text(aes(label = perc), vjust =-0.5, size =5) +scale_fill_viridis_d(option ="G",begin =0.1,end =0.9,guide ="none",direction =-1 ) +scale_y_continuous(labels =label_percent(), limits =c(0, max(var_by_df$proportion) +0.16)) +scale_x_discrete(labels = xlabs)# Data for the nested ovals oval_data <-data.frame(# label = c("A", "B", "C", "D"),x0 =c(0, 0, 0, 0, 0),# Center x position for all ovalsy0 =c(-1.7, -0.4, 0.7, 2.2, 3.3),# Center y position for all ovalsb =c(1.5, 3, 4.5, 6, 7.5),# Semi-major axis (horizontal radius)a =c(2, 4, 6, 8, 10) +1,# Semi-minor axis (vertical radius)text.pos =c(-4.7, -2, 1, 3.2, 6) +3# Radius for each circle ) oval_data <- oval_data[seq_along(levels), ] oval_data$label <-rev(xlabs) oval_data$size <- oval_data$a * oval_data$b# oval_data <- oval_data[oval_data$label %in% c("Site", "Bird"), ] # Order by y0 for correct layering# oval_data$label <- factor(oval_data$label, levels = rev(oval_data$label)) oval_data$label <-factor(oval_data$label, levels = oval_data$label[order(oval_data$size, decreasing =FALSE)]) oval_data$label.color <- viridis::mako(nrow(oval_data),begin =0.1,end =0.9,direction =-1,alpha =1 ) oval_data$label.color <-factor(oval_data$label.color, levels = oval_data$label.color[order(oval_data$size, decreasing =TRUE)])# oval_data <- oval_data[1:4,]# Plot using ggplot2 and ggforce gg_oval <-ggplot(oval_data, group = label) +geom_ellipse(aes(x0 = x0,y0 = y0,a = a,b = b,angle =0,fill = label.color ),color ="white") +geom_text(aes(x = x0, y = text.pos, label = label),size =7,color =c(rep("black", nrow(oval_data) -1), "white") ) +# scale_fill_viridis_d(option = "G", begin = 0.1, end = 0.9, guide = "none", direction = 1) + # Add labels in the centerscale_fill_identity() +# Use the colors as specified in the datatheme_void() +# Remove background, axes, and gridtheme(legend.position ="none") +coord_fixed() #+ # Keep aspect ratio fixed for true ovals#ylim(c(-12, 6))print(gg_oval)return(gg_out)}
Purpose
Quantify contribution of different social levels of organization in the geographic variation of parrots calls.
Analysis flowchart
flowchart LR
A(Read data) --> B(Plot acoustic space)
B --> C(Regression models<br>to partition variance)
C --> D(Plot variance partition)
style A fill:#44015466
style B fill:#3E4A894D
style C fill:#26828E4D
style D fill:#6DCD594D
Warning: Using the `size` aesthetic in this geom was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` in the `default_aes` field and elsewhere instead.
Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, : Model is nearly unidentifiable: large eigenvalue ratio
- Rescale variables?