Hi Joseph and Liam

The error trace shows that phenogram is trying to access
lty[names(tree$maps[[i]])[j]] and failing, which may indicate a
version-specific bug in how phytools handles the colors argument — in
phytools v2.4-4 there may be a bug where phenogram incorrectly handles line
types when colors from contMap are provided. It tries to match line types
to color segments but passes an invalid value, triggering the 'invalid line
type' error. Here are a few ideas:
A quick fix could be to add lty=1 to explicitly set the line type

phenogram(obj$tree, fineness_ratio, colors=obj$cols, spread.labels=TRUE,
          spread.cost=c(1,0), fsize=0.3, ftype="reg", lwd=1.25, lty=1,
          ylab="Fineness ratio", ylim=c(0,2.5))

Update phytools:

install.packages("phytools")
# Then restart R session

Try the dev version from GitHub in case it works?

remotes::install_github("liamrevell/phytools")


Best wishes

Michael Zyphur
Professor and Director
Instats | instats.org
Follow Instats: Bluesky <https://bsky.app/profile/instats.bsky.social>
Linkedin <https://www.linkedin.com/company/81837504/admin/dashboard/>
Facebook <https://www.facebook.com/InstatsTraining/>


On Tue, 9 Sept 2025 at 23:54, Joseph Brown <josep...@umich.edu> wrote:

> That code doesn't seem to work?
>
> Error in plot.xy(xy.coords(x, y), type = type, ...) : invalid line type
> 4.
> plot.xy(xy.coords(x, y), type = type, ...)
> 3.
> lines.default(a, b, col = colors[names(tree$maps[[i]])[j]], lwd = lwd,
> lty = lty[names(tree$maps[[i]])[j]], type = type)
> 2.
> lines(a, b, col = colors[names(tree$maps[[i]])[j]], lwd = lwd,
> lty = lty[names(tree$maps[[i]])[j]], type = type)
> 1.
> phenogram(mammal_cmap$tree, lnBodyMass, colors = mammal_cmap$cols,
> spread.cost = c(1, 0), fsize = 0.8, lwd = 3, ftype = "i")
>
> My info:
> R version 4.3.3 (2024-02-29)
> Platform: x86_64-pc-linux-gnu (64-bit)
> Running under: Ubuntu 24.04.3 LTS
>
> other attached packages:
> [1] phytools_2.4-4 maps_3.4.3     ape_5.8-1
>
> JWB
>
> On Tue, Sep 9, 2025 at 9:44 AM Liam J. Revell via R-sig-phylo <
> r-sig-phylo@r-project.org> wrote:
>
> > Hi Lindsey.
> >
> > To be honest, I don't see a problem with your code -- unless your R
> > graphical device does not support lwd=1.25. (That's the only thing I
> > could think of without being able to reiterate your analysis & debug it.)
> >
> > Here's a fully worked example of the same type of plot that uses data
> > packaged with /phytools/.
> >
> >     library(phytools)
> >     data("mammal.tree")
> >     data("mammal.data")
> >
> >     lnBodyMass<-setNames(log(mammal.data$bodyMass),
> >        rownames(mammal.data))
> >
> >     mammal_cmap<-contMap(mammal.tree,lnBodyMass,
> >        plot=FALSE)
> >
> >     mammal_cmap<-setMap(mammal_cmap,hcl.colors(n=100))
> >
> >     plot(mammal_cmap) ## standard contMap plot
> >
> >     dev.off()
> >
> >     phenogram(mammal_cmap$tree,lnBodyMass,
> >        colors=mammal_cmap$cols,
> >        spread.cost=c(1,0),fsize=0.8,lwd=3,
> >        ftype="i")
> >
> > If this doesn't help you figure out what's going wrong, please feel free
> > to send me your data & code (or, better yet, a saved R workspace) and
> > I'd be happy to troubleshoot it here.
> >
> > Best wishes, Liam
> >
> > Liam J. Revell
> > Professor of Biology, University of Massachusetts Boston
> > Web: http://faculty.umb.edu/liam.revell/
> > Book: Phylogenetic Comparative Methods in R
> > <https://press.princeton.edu/books/phylogenetic-comparative-methods-in-r
> >
> > (/Princeton University Press/, 2022)
> >
> >
> > On 9/9/2025 9:27 AM, Lindsey Dehaan wrote:
> > > [You don't often get email fromlmdeh...@umich.edu. Learn why this is
> > important athttps://aka.ms/LearnAboutSenderIdentification ]
> > >
> > > CAUTION: EXTERNAL SENDER
> > >
> > > Hi all,
> > >
> > > I have a quick question that I think someone could answer for me in a
> > > couple minutes rather than spend 2 hours of my day trying to figure it
> > out.
> > >
> > > I want to make a phenogram (or traitagram) showing the evolutionary
> > > trajectory of a trait of a group of fishes. While this would be
> > > sufficient enough for the figure, I think it would be neat to map the
> > color
> > > scheme of the continuous character produced by contMap() onto the
> > branches
> > > of the phenogram. However, I am running into an error and I am not
> > exactly
> > > sure how to fix it. The phenogram plots no problem, the contMap plots
> no
> > > problem, but I am having trouble combining the two outputs. The error I
> > > keep getting is "Error in plot.xy(xy.coords(x, y), type = type, ...) :
> > > invalid line type"
> > >
> > > I have tried to tweak the code so many times to get around this error
> > but I
> > > am finally at a loss. I would appreciate any pointers or tips anyone
> > > could throw my way. My code is below and I am happy to share the data
> if
> > > needed.
> > >
> > > Best,
> > > Lindsey
> > >
> > >> phenogram(locate_fossil_phy, fineness_ratio, fsize = 0.5, ftype =
> "reg",
> > > xlab=NULL, ylab="log Fineness Ratio")
> > >
> > > #Create contMap of continuous trait
> > >> obj<-contMap(locate_fossil_phy,fineness_ratio,plot=TRUE, res=50)
> > >> plot(obj,legend=0.75*max(nodeHeights(locate_fossil_phy)),
> > >       fsize=c(0.2,.8), lwd=2, label.offset=2,
> > >       outline=FALSE, ftype="off", fsize=0.66)
> > >
> > > #Change Colors
> > >> obj <- setMap(obj, c("#0000FF","#6600CC", "#FF0000", "#990000")) #Blue
> > to
> > > Red
> > >
> > > # Plot phenogram with obj$colors
> > > phenogram(obj$tree,fineness_ratio,colors=obj$cols,spread.labels=TRUE,
> > >           spread.cost=c(1,0),fsize=0.3,ftype="reg",lwd=1.25,
> > >           ylab="Fineness ratio", ylim=c(0,2.5))
> > >
> > >
> > >
> > > *Error in plot.xy(xy.coords(x, y), type = type, ...) : invalid line
> type*
> > > *-- *
> > > Lindsey DeHaan
> > > Ph.D. Candidate
> > > Department of Earth and Environmental Sciences
> > > University of Michigan
> > >
> > >          [[alternative HTML version deleted]]
> > >
> > > _______________________________________________
> > > R-sig-phylo mailing list -R-sig-phylo@r-project.org
> > > https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
> > > Searchable archive athttp://
> > www.mail-archive.com/r-sig-phylo@r-project.org/
> >         [[alternative HTML version deleted]]
> >
> > _______________________________________________
> > R-sig-phylo mailing list - R-sig-phylo@r-project.org
> > https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
> > Searchable archive at
> > http://www.mail-archive.com/r-sig-phylo@r-project.org/
> >
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-phylo mailing list - R-sig-phylo@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
> Searchable archive at
> http://www.mail-archive.com/r-sig-phylo@r-project.org/
>

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-phylo mailing list - R-sig-phylo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at http://www.mail-archive.com/r-sig-phylo@r-project.org/

Reply via email to