avg_sd_over <- over %>%
group_by(tip_label, cannibal, occur, world) %>%
mutate(mean=mean(ate_prob), se=mean((sd(ate_prob)/sqrt(sum_n)))) %>%
select(spcs, cannibal, occur, mean, se) %>%
distinct()
## Adding missing grouping variables: `tip_label`, `world`
Taxonomic Notes:
Species initially not found in tree:
Species found in the tree that are different from my original data:
dup_spcs <- over %>%
select(ID, genus, spcs, ate_prob, cannibal, mate_status, exprt, habitat, occur, tip_label, world) %>%
distinct(spcs, .keep_all=TRUE) %>%
filter(spcs != "Homalonychus selenopoides")
taxa <- tnrs_match_names(names=dup_spcs$spcs, context_name="Animals")
#matches made by rotl is fine
matches <- taxa %>%
filter(number_matches!=1)
#aphonopelma; homalonychus selenopoides, oligoxystre argentinense, tetragnatha elongata
# --------------------# fixed, see notes # -------------------#
not_in_tree <- is_in_tree(taxa$ott_id)
tree <- tol_induced_subtree(ott_ids = taxa$ott_id, label_format="name")
is.binary(tree)
## [1] FALSE
#resolving polytomies
tree2 <- multi2di(tree)
###Make tree binary
is.binary(tree2)
## [1] TRUE
tree2$node.label <- NULL
branches <- compute.brlen(tree2, method="Grafen", power=1)
is.ultrametric(branches)
## [1] TRUE
phylo_vcv <- vcv.phylo(branches, corr=T)
rownames(dup_spcs) <- dup_spcs$tip_label
tree_tib <- as_tibble(branches)
joining_traits <- full_join(tree_tib, dup_spcs, by=c("label" = "tip_label"))
traits <- joining_traits %>%
mutate_if(is.character,as.factor)
traits2 <- tree_tib %>%
full_join(avg_sd_over, by=c("label" = "tip_label")) %>%
mutate_if(is.character,as.factor) %>%
slice(1:105)
final_tree <- as.treedata(traits)
str(final_tree)
## Formal class 'treedata' [package "tidytree"] with 11 slots
## ..@ file : chr(0)
## ..@ treetext : chr(0)
## ..@ phylo :List of 4
## .. ..$ edge : int [1:146, 1:2] 76 76 86 86 85 87 87 83 98 98 ...
## .. .. ..- attr(*, "dimnames")=List of 2
## .. .. .. ..$ : NULL
## .. .. .. ..$ : chr [1:2] "parent" "node"
## .. ..$ edge.length: num [1:146] 0.0137 0.0137 0.0137 0.0137 0.0274 ...
## .. ..$ tip.label : chr [1:74] "Gluvia_dorsalis" "Galeodes_caspius" "Habronattus_pyrrithrix" "Evarcha_culicivora" ...
## .. ..$ Nnode : int 73
## .. ..- attr(*, "class")= chr "phylo"
## ..@ data : tibble [147 × 11] (S3: tbl_df/tbl/data.frame)
## .. ..$ node : int [1:147] 1 2 3 4 5 6 7 8 9 10 ...
## .. ..$ ID : Factor w/ 68 levels "ft001","ft002",..: 32 32 54 16 33 64 40 52 25 58 ...
## .. ..$ genus : Factor w/ 47 levels "Agelenopsis",..: 17 16 19 15 39 29 33 31 21 21 ...
## .. ..$ spcs : Factor w/ 74 levels "Agelenopsis pennsylvanica",..: 24 23 28 22 64 46 50 48 31 30 ...
## .. ..$ ate_prob : num [1:147] 37.7 38.5 33 0.72 3.17 ...
## .. ..$ cannibal : Factor w/ 2 levels "female","male": 1 1 1 2 1 1 1 2 1 1 ...
## .. ..$ mate_status: Factor w/ 3 levels "mated","unknown",..: 2 2 3 2 1 3 2 2 3 3 ...
## .. ..$ exprt : Factor w/ 2 levels "field","lab": 2 2 2 1 2 2 1 2 2 2 ...
## .. ..$ habitat : Factor w/ 2 levels "field","lab": 1 1 2 1 1 1 1 1 2 2 ...
## .. ..$ occur : Factor w/ 3 levels "post","pre","unknown": 2 2 2 1 1 2 2 2 1 1 ...
## .. ..$ world : Factor w/ 2 levels "new","old": 2 2 1 2 1 1 2 2 2 1 ...
## ..@ extraInfo : tibble [0 × 0] (S3: tbl_df/tbl/data.frame)
## Named list()
## ..@ tip_seq : NULL
## ..@ anc_seq : NULL
## ..@ seq_type : chr(0)
## ..@ tipseq_file: chr(0)
## ..@ ancseq_file: chr(0)
## ..@ info : list()
p_tree <- ggtree(final_tree, aes(colour=world)) + geom_tiplab(aes(label=spcs), align=TRUE) + xlim(NA, 6) + scale_color_manual(values=c("bisque4","deeppink4")) + theme(legend.position="bottom")
p_traits <- ggplot(traits2, aes(x=spcs, y=mean, fill=occur))+ geom_bar(stat="identity", colour="black", width=0.5, position=position_dodge(width=.75)) + geom_errorbar(aes(ymax=mean+se, ymin=mean-se), size=0.15, width=0.5)+ facet_wrap(~cannibal)+ coord_flip() + xlab(" ") + ylab(" ") + geom_hline(yintercept=c(0,50, 100), alpha=0.08) + ylim(-5, 100)+ scale_fill_manual(values=c("navajowhite","darkslategray4", "darkslateblue")) + theme(strip.background = element_blank(), panel.background=element_rect(fill="white"), axis.ticks=element_blank(), legend.title=element_blank(), axis.text.x=element_text(size=3), legend.position = "bottom") + scale_x_discrete(limits=rev(c(
"Latrodectus hasselti","Latrodectus geometricus","Latrodectus tredecimguttatus", "Latrodectus pallidus","Latrodectus hesperus",
"Leucauge mariana", "Leucauge argyra",
"Metellina segmentata",
"Tetragnatha elongata",
"Trichonephila plumipes","Trichonephila inaurata madagascariensis","Trichonephila edulis", "Trichonephila senegalensis","Trichonephila pilipes", "Trichonephila fenestrata",
"Nephilengys livida", "Nephilengys malabarensis",
"Herennia multipuncta",
"Larinia jeskovi", "Leviellus thorelli",
"Cyrtophora citricola",
"Austracantha minax",
"Caerostris darwini",
"Araneus diadematus",
"Phonognatha graeffei",
"Argiope bruennichi","Argiope lobata", "Argiope aurantia", "Argiope keyserlingi", "Argiope aemula",
"Micrathena gracilis",
"Diplocephalus permixtus",
"Argyroneta aquatica",
"Schizocosa ocreata", "Schizocosa uetzi",
"Hogna helluo", "Hogna radiata",
"Pardosa prativaga","Pardosa pseudoannulata", "Pardosa milvina",
"Pavocosa gallopavo",
"Allocosa brasiliensis", "Allocosa alticeps",
"Aglaoctenus lagotis",
"Lycosa inornata", "Lycosa tarantula", "Lycosa fasciiventris",
"Rabidosa punctulata", "Rabidosa rabida",
"Dolomedes fimbriatus", "Dolomedes triton", "Dolomedes tenebrosus",
"Pisaurina mira", "Pisaura mirabilis",
"Evarcha culicivora",
"Habronattus pyrrithrix",
"Phidippus johnsoni",
"Misumena vatia",
"Mecaphesa celer", "Micaria sociabilis",
"Agelenopsis pennsylvanica",
"Hololena curta",
"Stegodyphus lineatus",
"Paratrechalea ornata",
"Grammostola iheringi", "Grammostola schulzei", "Grammostola anthracina",
"Homoeomma uruguayense",
"Catumiri argentinense",
"Eupalaestrus weijenberghi",
"Plesiopelma longisternale",
"Aphonopelma marxi",
"Galeodes caspius",
"Gluvia dorsalis")))
p_tree + p_traits