Set alpha for geom_tippoint for just a defined group

23 views
Skip to first unread message

Andrew Holtz

unread,
May 9, 2023, 7:53:23 AM5/9/23
to ggtree
Hello everyone, 

Thank you very much for your help on this package. I have used this package in many different instances, and it's been great. 

Right now, I am trying to change the transparency of the tippoint according to the data that is coupled to my tree. I have a tree object called, tree. The data from this tree can be found via tree@data. Inside tree@data, I have a column called country, tree@data$country. You can see how I do this below: (note, the color of the branches and the tips are defined by tree@data$clade_membership). 

final_tree <- ggtree::ggtree(tree,
                                   aes(color = tree@data$clade_membership,
                                       text = c(paste(" Clade:", tree@data$clade_membership, '<br>',
                                                      "Country:", toupper(tree@data$country), '<br>',
                                                      "Date: ", round(as.numeric(tree@data$num_date),3))))) +
  ggtree::geom_tippoint(size = 1) +
  ggtree::geom_tippoint(aes(color = tree@data$clade_membership), size = 1) +
  labs(color = "Clade") +
  scale_colour_manual(values=sample(c25)) 


I would like to highlight the tips that are from a specific country. Let's say there is the variable country_id. Let's say: 
country_id  <- 'Italy'

I would now like to highlight only the tips in the tree that have 'Italy' as their entry in tree@data$country. I can do this either by making the alpha of all other tips low (0.25) and the alpha of the tips with Italy high (1). Another option is to include the coloring for the clade membership for only those tips defined as 'Italy' and then all other tips should be grey. 

This is what I tried for the transparency:
final_tree$layers[[1]]$aes_params$alpha <- ifelse(final_tree@data$country_id == country, 1, 0.5)

But nothing changed. 

I also tried removing the tip shapes for all tips not defined as 'Italy' like this, but this did not work either. I still had the tip shapes for all:
limited_tree1 <- final_tree+
  geom_tiplab(aes(color=final_tree@data$country != country))


If anyone know a better way to either make branches and tips transparent, color tip shapes, or remove tip shapes  by a column in the data file, I would really appreciate some guidance. 

Thank you, 
Andrew

Reply all
Reply to author
Forward
0 new messages