Leonardo,
Please review the syntax and examples in the help file, and in the vignette about gm.prcomp. Usually, such issues are a data input or matching issue (e.g., make certain that the names of your specimens match the names on the phylogeny).
Dean
Dr. Dean C. Adams (he/him)
Distinguished Professor of Evolutionary Biology
Director of Graduate Education, EEB Program
Department of Ecology, Evolution, and Organismal Biology
Iowa State University
https://faculty.sites.iastate.edu/dcadams/
phone: 515-294-3834
--
You received this message because you are subscribed to the Google Groups "geomorph R package" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
geomorph-r-pack...@googlegroups.com.
To view this discussion on the web, visit
https://groups.google.com/d/msgid/geomorph-r-package/1cff8a21-7444-4964-b434-1d79ce5cd8c4n%40googlegroups.com.
I am new using R, and I’ve been struggling for some time now.
I am working on a data set containing 3D landmarks on vertebrae for 24 dolphin species, that are grouped in 3 subfamilies. In total there are 116 individuals.
We are exploring vertebral morphology within dolphins from the biomechanical point of view and trying to evidence morphological similarities in dolphin species with similar habitat requirements. For that we have stablished the categorial factor “Habitat”.
After performing GPA on our raw data. We first created a data frame containing the proc. coord and our categorical factors (that include species and subfamily apart from Habitat) and then calculated meanshapes for each species and a dataframe with the mean shape for each species and the categorial values for the means input from a cvs file.
Everything semmed to be fine this far, but when we try to plot a phylomorphospace and color the tips according to the habitat of each species, it is impossible for us to match species with the color representing their habitat, and species that are coastal are colored as if they were oceanic for eample (it messes up most of the colors)
We are assuming there is something wrong in the matching of the data, but we can’t seem to find the problem….
Hopefully someone can help us with this.
I am copying the code we are using below , but I do not know if that is enough to try to help us.
Any comments will be welcome!
Thanks in advance
Delphinidae_Data <- read.table("01Th_delphinidae_SoloAd.txt", header=T, dec =".")
Delphinidae_Clasif <-read.csv(file = "01Th_IDdelphinidae_SoloAd.csv", header = T,sep = ";",dec = ".")
Delphinidae_Clasif$Sp<-as.factor(Delphinidae_Clasif$Sp)
Delphinidae_Clasif$Subfamily<-as.factor(Delphinidae_Clasif$Subfamily)
Delphinidae_Clasif$Habitat<-as.factor(Delphinidae_Clasif$Habitat)
Delphinidae_3Darray <- arrayspecs(Delphinidae_Data[,2:ncol(Delphinidae_Data)], 35, 3)
Delphinidae_GPA <- gpagen(Delphinidae_3Darray)
gdf_delph<-geomorph.data.frame(Delphinidae_GPA,SP=Delphinidae_Clasif$Sp,Subfamily=Delphinidae_Clasif$Subfamily,Habitat=Delphinidae_Clasif$Habitat)
#Filogenia de la Familia
TreeDelphinidae<- '(((((((((((Slong,Sclym),Lhose),Ddelp),Scoeu),((Satte,Sfron),(TtrunO,TtrunC))),Schin),Sfluv),((((Pelec,Fatte),Gmacro),Ggris),Sbred)),((((Ccomm,Chect),((L. aust, Lcruc),Cheav)),Lobsc),Liper)),Lalbi),Lacut);'
Delphinidae_phy<-read.tree(text=TreeDelphinidae)
#Mean shapes
Y1 <- mshape(gdf_delph$coords[,,which(gdf_delph$SP == "Lacut")])
Y2 <- mshape(gdf_delph$coords[,,which(gdf_delph$SP == "Lalbi")])
Y3 <- mshape(gdf_delph$coords[,,which(gdf_delph$SP == "Ccomm")])
Y4 <- mshape(gdf_delph$coords[,,which(gdf_delph$SP == "Cheav")])
Y5 <- mshape(gdf_delph$coords[,,which(gdf_delph$SP == "Chect")])
Y6<- mshape(gdf_delph$coords[,,which(gdf_delph$SP == "Laust")])
Y7 <- mshape(gdf_delph$coords[,,which(gdf_delph$SP == "Lcruc")])
Y8 <- mshape(gdf_delph$coords[,,which(gdf_delph$SP == "Lipero")])
Y9 <- mshape(gdf_delph$coords[,,which(gdf_delph$SP == "Lobsc")])
Y10 <- mshape(gdf_delph$coords[,,which(gdf_delph$SP == "Fatte")])
Y11 <- mshape(gdf_delph$coords[,,which(gdf_delph$SP == "Ggris")])
Y12 <- mshape(gdf_delph$coords[,,which(gdf_delph$SP == "Gmacro")])
Y13 <- mshape(gdf_delph$coords[,,which(gdf_delph$SP == "Pelec")])
Y14 <- mshape(gdf_delph$coords[,,which(gdf_delph$SP == "Sbred")])
Y15 <- mshape(gdf_delph$coords[,,which(gdf_delph$SP == "Lhose")])
Y16 <- mshape(gdf_delph$coords[,,which(gdf_delph$SP == "Satte")])
Y17 <- mshape(gdf_delph$coords[,,which(gdf_delph$SP == "Schin")])
Y18 <- mshape(gdf_delph$coords[,,which(gdf_delph$SP == "Ddelp")])
Y19 <- mshape(gdf_delph$coords[,,which(gdf_delph$SP == "Sclym")])
Y20 <- mshape(gdf_delph$coords[,,which(gdf_delph$SP == "Scoeu")])
Y21 <- mshape(gdf_delph$coords[,,which(gdf_delph$SP == "Sfluv")])
Y22 <- mshape(gdf_delph$coords[,,which(gdf_delph$SP == "Sfron")])
Y23 <- mshape(gdf_delph$coords[,,which(gdf_delph$SP == "Slong")])
Y24 <- mshape(gdf_delph$coords[,,which(gdf_delph$SP == "TtrunC")])
Y25 <- mshape(gdf_delph$coords[,,which(gdf_delph$SP == "TtrunO")])
YT <- array(c(Y25, Y24, Y23, Y22, Y21, Y20, Y19, Y18, Y17, Y16, Y15, Y14, Y13, Y12, Y11, Y10, Y9, Y8, Y7, Y6, Y5, Y4, Y3, Y2,Y1 ), dim = c(35,3,25), dimnames = list(c(),c(),Delphinidae_phy$tip.label))
mean_Clasif <-read.csv(file = "01Th_ID_SoloAd.csv", header = T,sep = ";",dec = ".")
mean_Clasif$Sp<-as.factor(mean_Clasif$Sp)
mean_Clasif$Subfamily<-as.factor(mean_Clasif$Subfamily)
mean_Clasif$Habitat<-as.factor(mean_Clasif$Habitat)
summary(mean_Clasif$Habitat)
# branch lenghts
Delphinidae_phy <- compute.brlen(Delphinidae_phy)
meanshape_gdf<-geomorph.data.frame(coords=YT,CS=CS, SP=mean_Clasif$Sp,Subfamily=mean_Clasif$Subfamily,Habitat=mean_Clasif$Habitat,Phy= Delphinidae_phy)
#filoPCA
Delphinidae_PCA_meanshapes<-gm.prcomp(A=meanshape_gdf$coords,phy =meanshape_gdf$Phy)
gp <- as.factor (meanshape_gdf$Habitat)
col.gp<-c("purple", "lightblue","red","orange","green","black","grey","blue")
names(col.gp) <- levels(gp)
col.gp1<- col.gp[match(gp, names(col.gp))]
summary(gp)
levels(gp)
#plot
plot(Delphinidae_PCA_meanshapes, axis1=1, axis2= 2, phylo =TRUE,col= col.gp1, phylo.par= list(tip.labels = TRUE, node.labels=FALSE, edge.color= "black", edge.width= 1, tip.txt.cex =0.5, tip.txt.adj = c(-0.1, -0.1) ))
legend('topleft',legend= levels(gp), fill= c("purple", "lightblue","red","orange","green","black","grey","blue"),cex=0.3)
title("Vertebra PCA",cex.main=1.5)
--
You received this message because you are subscribed to the Google Groups "geomorph R package" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geomorph-r-pack...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/geomorph-r-package/1D50DF52-26E9-407E-89F5-03492BCE7BBC%40gmail.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/geomorph-r-package/ff26246a-1d2a-4c4e-8fd7-0a6523898064n%40googlegroups.com.
So, this is what I get when calling the habitat, and it looks ok, it is in the order it should be according to the order of the species in the .cvs file
> meanshape_gdf$Habitat
[1] Shelf/Oceanic Shelf/Oceanic Coastal Coastal
[5] Coastal Coastal Oceanic Oceanic
[9] Shelf Oceanic OceanicDD OceanicDD
[13] Oceanic Shelf Oceanic Oceanic
[17] Coastal/Riverine Shelf/Oceanic Oceanic Oceanic
[21] Riverine Coastal Shelf/Oceanic Shelf
[25] Coastal
But then….I have tried two ways for matching the right color with the right species (according to that species habitat)
> col.gp<- col.gp[match(gp, names(col.gp))]
> summary(gp) #this also seems to be ok
Coastal Coastal/Riverine Oceanic OceanicDD Riverine
6 1 8 2 1
Shelf Shelf/Oceanic
3 4
> levels(gp)
[1] "Coastal" "Coastal/Riverine" "Oceanic" "OceanicDD"
[5] "Riverine" "Shelf" "Shelf/Oceanic"
when I run this script:
> plot(Delphinidae_PCA_meanshapes, axis1=1, axis2= 2, phylo =TRUE,col= col.gp, phylo.par= list(tip.labels = TRUE, node.labels=FALSE, edge.color= "black", edge.width= 1, tip.txt.cex =0.5, tip.txt.adj = c(-0.1, -0.1) ))
> legend('bottomright',legend= levels(gp), fill= c("purple", "lightblue","red","orange","green","black","grey","blue"),cex=0.3)
> title("Vertebra PCA",cex.main=1.5)
I get this plot…. in which species do not have the color they should

If I run this:
> plot(Delphinidae_PCA_meanshapes, axis1=1, axis2= 2, phylo =TRUE,col= c("purple", "lightblue","red","orange","green","black","grey","blue" ), phylo.par= list(tip.labels = TRUE, node.labels=FALSE, edge.color= "black", edge.width= 1, tip.txt.cex =0.5, tip.txt.adj = c(-0.1, -0.1) ))
> legend('topleft',legend= levels(gp), fill= c("purple", "lightblue","red","orange","green","black","grey","blue"),cex=0.3)
> title("Vertebra PCA",cex.main=1.5)
I get a similar plot, but colors do not match the habitats of each species either…for example Sfluv should be riverine but it is not green….

Thank you for taking the time to help me with this, I have been struggling for hours now!
To view this discussion on the web, visit https://groups.google.com/d/msgid/geomorph-r-package/CAJgz5LzPxwemOvxoSD97cNkgfLSm4m5hh48Lw5jrThB04x5tmw%40mail.gmail.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/geomorph-r-package/CAJgz5Lx%2BOWE9O%2Btqn51M%3D%3DvzvM6_A5nNR8zsDZUzypo%2Bc5PgAQ%40mail.gmail.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/geomorph-r-package/FA317744-AA21-432D-916E-9264627ABAD5%40gmail.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/geomorph-r-package/8F8FADFF-5D49-43E0-892C-8C963CF0C359%40gmail.com.
Yes that is correct. The example ‘plethspecies’ has data and a tree out of order, but still runs.
One possible issue here is that Leonardo’s tree has polytomies. For most R-based comparative methods analyses, one must first use ‘multi2di’ in ‘ape’ to resolve them via zero-length branches. That does not change the resulting phylogenetic covariance matrix, but makes a difference for some analyses.
A quick check though with this data does not seem to resolve it. But I bring it up because it is good coding practice to remember this step: it can make a difference.
I will investigate the current dataset challenge more when I can.
Dean
Dr. Dean C. Adams (he/him)
Distinguished Professor of Evolutionary Biology
Director of Graduate Education, EEB Program
Department of Ecology, Evolution, and Organismal Biology
Iowa State University
https://faculty.sites.iastate.edu/dcadams/
phone: 515-294-3834
To view this discussion on the web, visit https://groups.google.com/d/msgid/geomorph-r-package/CAJgz5LxSm%3DvQtWDZNdq7xivcc%3DFKOZMFQmjuRHyS_nzkY5tCtQ%40mail.gmail.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/geomorph-r-package/CAJgz5LxSm%3DvQtWDZNdq7xivcc%3DFKOZMFQmjuRHyS_nzkY5tCtQ%40mail.gmail.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/geomorph-r-package/CH2PR04MB66170814CC63957EB1F94E3BA2309%40CH2PR04MB6617.namprd04.prod.outlook.com.
Leonardo,
The difference between PGLS and gm.prcomp is that the latter requires ancestral state estimation. That requires a resolved phylogeny, which is where the polytomies come back into this.
As Mike stated, mathematically there are only N-1 nodes for a fully resolved tree of N tips. For some reason, the ‘multi2di’ function in ape (which geomorph uses) is returning N+1 nodes for your tree. I again suspect that is part of the problem as that will cascade into ancestral state estimation (though I do not have the full story yet).
I will try to investigate this when I can, but both Mike and I have confirmed it is a particular problem related to your tree. And even though have pinpointed where, we do not know why it occurs, or how to resolve this. I will try to find time to investigate the latter within geomorph, but I make no promises (it is rather hard to devise a solution to a problem that should not exist mathematically!).
Please be patient while we continue to dig.
To view this discussion on the web, visit https://groups.google.com/d/msgid/geomorph-r-package/ca4f0d73-f846-4087-bb28-cdf7179cc202n%40googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/geomorph-r-package/ca4f0d73-f846-4087-bb28-cdf7179cc202n%40googlegroups.com.
Leonardo et al.,
After quite a bit of digging, the issue was not related to the polytomies. Instead, Leonardo’s tree had two internal ‘nodes’ that were embedded within branches. That is, there were nodes with only a single descendant; meaning they were found along an internal branch. To see these try this:
phytools:::plotTree.singletons(phylo)
Such issues are rare, and why they occur are not immediately clear to me. They are external to geomorph, and likely to R. As alluded to earlier, it is a tree issue.
I have updated gm.prcomp (or rather its internal functions) to catch such oddities, and have confirmed that for Leonardo’s tree things now longer throw an error. Please install from Github as:
devtools::install_github(‘geomorphR/geomorph’, ref = ‘Stable’)
To view this discussion on the web, visit https://groups.google.com/d/msgid/geomorph-r-package/ca4f0d73-f846-4087-bb28-cdf7179cc202n%40googlegroups.com.
You received this message because you are subscribed to a topic in the Google Groups "geomorph R package" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/geomorph-r-package/114YaHRT99Y/unsubscribe.
To unsubscribe from this group and all its topics, send an email to geomorph-r-pack...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/geomorph-r-package/CH2PR04MB6617009C8183B01B53FC0674A2329%40CH2PR04MB6617.namprd04.prod.outlook.com.
![]() |
|
<clip_image001.png>
If I run this:
> plot(Delphinidae_PCA_meanshapes, axis1=1, axis2= 2, phylo =TRUE,col= c("purple", "lightblue","red","orange","green","black","grey","blue" ), phylo.par= list(tip.labels = TRUE, node.labels=FALSE, edge.color= "black", edge.width= 1, tip.txt.cex =0.5, tip.txt.adj = c(-0.1, -0.1) ))
> legend('topleft',legend= levels(gp), fill= c("purple", "lightblue","red","orange","green","black","grey","blue"),cex=0.3)
> title("Vertebra PCA",cex.main=1.5)
I get a similar plot, but colors do not match the habitats of each species either…for example Sfluv should be riverine but it is not green….
<clip_image002.png>
To view this discussion on the web, visit https://groups.google.com/d/msgid/geomorph-r-package/CAJgz5LwZPyNtSg1hECF3%2B92uLM56dsnWGGMFq9ZCx1c2NfftgQ%40mail.gmail.com.