Legend and color scale order in gheatmap

876 views
Skip to first unread message

oetl

unread,
May 10, 2022, 5:23:36 AM5/10/22
to ggtree
Hi,
I am having a problem with the legend order in gheatmap. I have the degree of expression in a trait that is ordinally ordered from -3 to +3. When I plot this, gheatmap assumes that -1 is the lowest value, counts up to -3, then 0, and then starts from 1 to 3 (as you can see in my example).
repr.example.png

Here is a reproducible example:

library(geiger)
library(ggplot2)
library(ggtree)

set.seed(12345)
tree <- sim.bdtree(b = 0.1, d = 0, stop = "time", t = 20, seed = 12345)

ptree <- ggtree(tree)
trait.mode <- data.frame(trait = as.factor(sample( -3:3, length(tree$tip.label), replace = T)))
rownames(trait.mode) <- tree$tip.label
gheatmap(ptree,trait.mode, offset =1, width=.05, colnames=F, colnames_offset_y = 8 )   +
  scale_fill_viridis_d(option="A", name="Parental\ncare")

I tried to convert the traits to integers but then it complains that I am using a discrete color scale for a continuous trait.
Any Idea how I can fix it?

jonnations

unread,
May 10, 2022, 7:05:32 AM5/10/22
to oetl, ggtree
It’s a viridis issue not a ggtree issue. Do ‘??scale_color_viridis’ and read about the ‘c’ at the end. 

--
1. Data Integration, Manipulation and Visualization of Phylogenetic Trees (1st ed.). Chapman and Hall/CRC. 2022. ISBN: 9781032233574
2. ggtreeExtra: Compact visualization of richly annotated phylogenetic data. Molecular Biology and Evolution. 2021, 38(9):4039-4042. doi: 10.1093/molbev/msab166
3. Using ggtree to visualize data on tree-like structures. Current Protocols in Bioinformatics. 2020, 69:e96. doi: 10.1002/cpbi.96
4. treeio: an R package for phylogenetic tree input and output with richly annotated and associated data. Molecular Biology and Evolution. 2020, 37(2):599-603. doi: 10.1093/molbev/msz240
5. Two methods for mapping and visualizing associated data on phylogeny using ggtree. Molecular Biology and Evolution, 2018, 35(2):3041-3043. doi: 10.1093/molbev/msy194
6. ggtree: an R package for visualization and annotation of phylogenetic trees with their covariates and other associated data. Methods in Ecology and Evolution. 2017, 8(1):28-36. doi: 10.1111/2041-210X.12628
---
You received this message because you are subscribed to the Google Groups "ggtree" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bioc-ggtree...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bioc-ggtree/0ebf6970-466b-456d-9e90-b196f0e05aecn%40googlegroups.com.
--
Jonathan A. Nations
NSF Postdoctoral Researcher
Slater Lab, University of Chicago |
Research Associate

oetl

unread,
May 10, 2022, 11:33:42 PM5/10/22
to ggtree
Thanks for the reply. I don"t think it is a viridis issue as the problem persists without using it.

library(geiger)
library(ggplot2)
library(ggtree)

set.seed(12345)
tree <- sim.bdtree(b = 0.1, d = 0, stop = "time", t = 20, seed = 12345)

ptree <- ggtree(tree)
trait.mode <- data.frame(trait = as.factor(sample( -3:3, length(tree$tip.label), replace = T)))
rownames(trait.mode) <- tree$tip.label
gheatmap(ptree,trait.mode, offset =1, width=.05, colnames=F, colnames_offset_y = 8 )  

without_viridis.png

jonnations

unread,
May 11, 2022, 8:47:08 AM5/11/22
to oetl, ggtree
I suggested that you read the documentation for the ‘scale_fill_viridis’ command. It explains that ‘scale_fill_viridis_d’ is for discrete palettes and ‘scale_fill_viridis_c’ is for continuous palettes.  Also ‘factors’ are not continuous variables so you need to change your factors to numerics, or drop the as.factors() command. 

oetl

unread,
May 11, 2022, 9:21:21 AM5/11/22
to ggtree
Yes I did that. But I was under the impression that my trait is a ordinal categorical variable, not  continuous, as it can only take values between -3 and 3 (which refer to intensity of male or female bias). So I didn't see a problem with using the viridis categorical variable.

oetl

unread,
May 20, 2022, 3:17:34 AM5/20/22
to ggtree
I got it working now by specifying my own scale

tree <- sim.bdtree(b = 0.1, d = 0, stop = "time", t = 20, seed = 12345)

ptree <- ggtree(tree)
trait.mode <- data.frame(trait = as.factor((sample( -3:3, length(tree$tip.label), replace = T))))
rownames(trait.mode) <- tree$tip.label
my_colors <- magma(7)
names(my_colors) <- levels(trait.mode$trait)
names(my_colors)

my_scale <- scale_fill_manual(name = "Group", values = my_colors)              

gheatmap(ptree,trait.mode, offset =1, width=.05, colnames=F, colnames_offset_y = 8 ) +
  my_scale + geom_cladelab(label = rownames(trait.mode), node = 1:17, align = T, offset =0, offset.text = 2 )
Rplot.png

Yu, Guangchuang

unread,
Jun 11, 2022, 5:26:34 AM6/11/22
to oetl, ggtree
trait.mode <- data.frame(trait = as.factor((sample( -3:3, length(tree$tip.label), replace = T))))

The as.factor rules the order.



--
--~--~---------~--~----~------------~-------~--~----~
Guangchuang Yu PhD
Professor, Director
Department of Bioinformatics
School of Basic Medical Sciences
Southern Medical University
Guangzhou, China
-~----------~----~----~----~------~----~------~--~---
Reply all
Reply to author
Forward
0 new messages