1
votes

I'm running a MacOS Mojave with R Studio Version 1.2.1335. I am trying to change the colors of the tip labels on a phylogenetic tree that I uploaded to R via a nexus file. I have a separate CSV file with all of the species names in the exact format as the nexus file. In the CSV I have a "yes" or "no" in the column next to the species names. The species name column is called Tree_name and the other column is "have". I want the species that have "no" next to them to be colored red and the others colored black. I am new to using R to plot phylogenies. Any help would be greatly appreciated!

I have tried to do a similar thing using dots but keep coming up with this error

Error in res[edge[i, 2]] <- res[edge[i, 1]] + el[i] : 

replacement has length zero

This is the code I have tried:

shark.data<-read.csv("sharks_nosquatinis_format.csv",row.names=1)
dotTree(str_tree_sharks,shark.data,colors=setNames(c("blue","red"),
c("yes","no")),ftype="i",fsize=0.7)
1

1 Answers

0
votes

Hard to tell without a running example but you can use something along those lines:

## Creating a random tree (you can use your nexus one)
my_tree <- rtree(5)

## Creating a random dataset (same)
my_data <- data.frame("column_of_interest" = c("yes", "no", "yes", "no", "yes"))

## Creating a vector of colours
my_colours <- c("black", "orange")

## Plotting the tree with colored tip labels
plot(my_tree,
     tip.color = my_colours[(my_data$column_of_interest == "yes")+1])

The column_of_interest is of course the column you have the information you want to plot. The part (my_data$column_of_interest == "yes")+1 is finding which rows in column_of_interest have the element "yes" and returns a TRUE/FALSE vector. You can then add the +1 to convert this logical vector into a numeric one where TRUE becomes 2 (1+1) and FALSE becomes 1 (0+1). The my_colours[(my_data$column_of_interest == "yes")+1] part will then attribute the first colour to FALSE (in this examples things that are not "yes") and the second colour to TRUE ("yes").