1

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)
Darren
  • 1,352
  • 5
  • 19
  • 49
olivegr
  • 21
  • 3

1 Answers1

0

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").

Thomas Guillerme
  • 1,747
  • 4
  • 16
  • 23
  • So, Thomas, unfortunately I spoke too soon and the code is not working as I had previously thought. I am now running into an issue where some of the species that are labeled "yes" in the CSV are red on the phylogenetic tree and others are black but there does not seem to be a pattern. The code I am using is: `str_tree_sharks <-read.nexus("str_constree_sharks_nosquatini.nex") shark.data <-read.csv("sharks_nosquatinis_format.csv", row.names = 1) plot(maj_tree_sharks,tip.color = my_colours[(shark.data$have == "yes")+1])` – olivegr Aug 20 '19 at 19:12
  • ^sorry re: new comment the tree that it should be calling in the `plot()` function is `str_tree_sharks` not `maj_tree_sharks` I copied the wrong line so this would not be the cause of that problem – olivegr Aug 20 '19 at 19:20
  • Any chance of a more generic answer with >2 colours – user3329732 Apr 21 '20 at 09:11
  • You can use the same logic as above but use `my_colours[as.factor(my_data$column_of_interest)]` to convert the variables on interest into numerical factors (by default they are attributed in alphabetical order). – Thomas Guillerme Apr 21 '20 at 09:14