liamrevell / phytools

GNU General Public License v3.0
207 stars 56 forks source link

Tree branches colors #68

Closed AgustinPardo closed 3 years ago

AgustinPardo commented 4 years ago

Hello, I am running this code:

library(phytools)
library(mapdata)
gps<-read.csv("/home/agustin/workspace/covid-19/mapeo_arbol/gps_data.dat",header=TRUE)
rownames(gps) <- gps$ID
nwk=read.newick("/home/agustin/workspace/covid-19/mapeo_arbol/arbol_id_root_10.nwk")

latitudes_uniques=unique(gps$Lat)
latitudes_uniques=as.integer(latitudes_uniques)

library("RColorBrewer")
display.brewer.all()
colores=brewer.pal(n = 6, name = 'Set1')
#colores= c("#c6d4e1", "#2f2016", "#fcfaea", "#456789", "#4FD5D6", "#FF0000")
names(colores)=latitudes_uniques

latitudes_id=gps[,1:2]
ids=gps$ID
latitudes=as.integer(gps$Lat)
names(latitudes)=gps$ID

colores_ids=c()
colores_vector=c()
id_vector=c()
for (i in (1:dim(latitudes_id)[1])) {
  color=(colores[toString(latitudes[i])])
  colores_vector[i]=color
  id=names(latitudes[i])
  id_vector[i]=id
}
names(colores_vector)=id_vector

obj<-phylo.to.map(compute.brlen(nwk),gps[1:179,2:3],fsize=0.3,lwd=0.5,pts=FALSE,lty="solid",colors=colores_vector,
                  direction="rightwards",database="worldHires", 
                  regions=c('Brazil', 'Colombia', 'Uruguay', 'Chile', 'Argentina', 'Peru',"Falkland"), 
                  rotate=FALSE)

To get this figure:

Screenshot from 2020-06-03 11-12-04

Do you how I could also add color to the branches by the color of the arrow? That is the closest branch colored by the color of the arrow!

Regards

rthapa1 commented 4 years ago

Hi, were you able to figure out the issue?

rthapa1 commented 4 years ago

Hi, were you able to figure out the issue?

rthapa1 commented 4 years ago

Hi, were you able to figure out the issue?

rthapa1 commented 4 years ago

Hi, were you able to figure out the issue?

rthapa1 commented 4 years ago

Hi, were you able to figure out the issue?

rthapa1 commented 4 years ago

Hi, were you able to figure out the issue?

rthapa1 commented 4 years ago

Hi, were you able to figure out the issue?

rthapa1 commented 4 years ago

Hi, were you able to figure out the issue?

rthapa1 commented 4 years ago

Hi, were you able to figure out the issue?

rthapa1 commented 4 years ago

Hi, were you able to figure out the issue?

AgustinPardo commented 4 years ago

No, I could not solve it!

Rathanin-github commented 3 years ago

Hi @AgustinPardo Thank you so much for your sample script above. I am very new in R and I am going to plot similar to you. I have a phylogenetic tree of about 300 bacterial genomes and I have the CSV lat and long like this: lat long Bacterial 1 -53.75945233 89.82698 Bacterial 2 -53.75945233 89.82698 Bacterial 3 -53.75945233 89.82698 Bacterial 4 -5.28332361 -160.89541 Bacterial 5 -5.28332361 -160.89541 Bacterial 6 -5.28332361 -160.89541 Bacterial 1-3 from province A (country) and bacterial 4-6 from province B. However, I don't understand various line of your script of how gps be generated and how color be added to get the beautiful plot above. for example: latitudes_id=gps[,1:2] What are latitudes_id and input gps look like? Could I see the example of your gps_data.dat in csv? Or could you please explain your scrip a bit more? It may help me a lot to follow your script. I was woundering if you could you suggest me how to generate my input gps data above to get the beautiful plot like you. Really appreciate for any help.

AgustinPardo commented 3 years ago

I send to you the input files (attached as "data.zip"), so you can explore them!

data.zip

liamrevell commented 3 years ago

Unfortunately, this isn't a feature yet in phylo.to.map. One option is to use get("last_plot.phylo", envir = .PlotPhyloEnv) (which does work with phylo.to.map) to get the coordinates of the plotted tree, and then just overplot if with the colors that you want to show. This requires some advanced R scripting, though, unfortunately.