Skip to content

Instantly share code, notes, and snippets.

@talonsensei
Created July 5, 2016 17:04
Show Gist options
  • Save talonsensei/e1fad082657054207f249ec98f0920eb to your computer and use it in GitHub Desktop.
Save talonsensei/e1fad082657054207f249ec98f0920eb to your computer and use it in GitHub Desktop.
library("ggtree")
library("dplyr")
library("colorspace")
#Load Tree data
hc_tree<-structure(list(edge = structure(c(11L, 11L, 12L, 13L, 14L, 15L,
15L, 14L, 13L, 16L, 17L, 18L, 19L, 19L, 18L, 17L, 16L, 12L, 1L,
12L, 13L, 14L, 15L, 2L, 3L, 4L, 16L, 17L, 18L, 19L, 5L, 6L, 7L,
8L, 9L, 10L), .Dim = c(18L, 2L)), Nnode = 9L, tip.label = c("XPA.85.007",
"XPA.85.002", "XPA.85.001", "XPA.85.009", "XPA.85.013", "XPA.85.012",
"XPA.85.011", "XPA.85.010", "XPA.85.014", "XPA.85.003"), edge.length = c(0.0218860096110355,
0.0218860096110355, 0.0157703305873459, 0.0253235785955029, 0.0304972457273817,
0.0480189746562429, 0.0500981691928612, 0.135321566311564, 0.0432215089373018,
0.112235851237383, 0.243285688496605, 0.0742303504425568, 0.0271071192052938,
0.0260784098242765, 0.134848138865602, 0.374765735649553, 0.159653881377338,
0.093950590734575), node.label = c("", "", "58", "66", "92",
"88", "99", "100", "100"), root.edge = 0), .Names = c("edge",
"Nnode", "tip.label", "edge.length", "node.label", "root.edge"
), class = "phylo", order = "cladewise")
# Load tree annotation data
hc_annot_clean <- structure(list(XPA = c("XPA.85.001", "XPA.85.002", "XPA.85.003",
"XPA.85.007", "XPA.85.009", "XPA.85.010", "XPA.85.011", "XPA.85.012",
"XPA.85.013", "XPA.85.014"), HCfam = c("H03", "H03", "H03", "H03",
"H03", "H04", "H01", "H01", "H01", "H03")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -10L), .Names = c("XPA",
"HCfam"))
# Load Heatmap data - reusing same data as tree annotation data for simplicity
clean_data<-structure(list(HCfam = c("H03", "H03", "H03", "H03", "H03", "H04",
"H01", "H01", "H01", "H03")), .Names = "HCfam", class = "data.frame", row.names = c("XPA.85.001",
"XPA.85.002", "XPA.85.003", "XPA.85.007", "XPA.85.009", "XPA.85.010",
"XPA.85.011", "XPA.85.012", "XPA.85.013", "XPA.85.014"))
# Get the heavy chain families
hc <- lapply(distinct(select(hc_annot_clean,HCfam)),as.character)$HCfam
hc_cls<-list()
for(x in 1:length(hc))
{
chain<-hc[x]
hc_cls[[chain]] <- lapply(select(filter(hc_annot_clean,HCfam==chain),XPA),as.character)$XPA
}
# Have to reset XPA's as rownames so that geom_text will properly display families on the branch
# Have to convert object to data.frame instead of a multi-classed obj (tbl_df,'tbl' and 'data.frame')
# that is the result of reading in a table from file
branch_labels <- data.frame(hc_annot_clean)
rownames(branch_labels)<-branch_labels[,1]
# Group the tree by heavy chain family
annot_tree <- groupOTU(hc_tree, hc_cls, group_name="hcfamily")
# Create the tree graph object
p<-ggtree(annot_tree, aes(color=hcfamily), size=2, branch.length="none") +
geom_tiplab(size=3) +
ggtitle("Heavy chain families") +
scale_color_manual(values=c("black", rainbow_hcl(length(hc))))
# Attach branch labels to tree graphic object
p <- p %<+% branch_labels
# Create combined tree/matrix object
p2<-gheatmap(p, clean_data, offset=1.5, width=0.5,colnames=F)+ theme(legend.position="none") + geom_text2(aes(color=hcfamily, label=HCfam), hjust=-4, vjust=0.6, size=3)
# Get matrix labels and draw rotated per https://github.com/GuangchuangYu/ggtree/issues/26
df = get_heatmap_column_position(p2, by="bottom")
# Next step generates error: Error in eval(expr, envir, enclos) : object 'hcfamily' not found
#####################################################################
p2 + geom_text(data=df, aes(x, y, label=label), nudge_y=-1, angle=45)
@GuangchuangYu
Copy link

p2 + geom_text(data=df, aes(x, y, label=label), nudge_y=-1, angle=45, inherit.aes=FALSE)

Add inherit.aes=FALSE to prevent inherit aes from ggtree will solve the issue.

see also discussion on https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/bioc-ggtree/44PvWXT7ojk/Y05e1w8BCAAJ.

BTW: I would like to add a link in https://guangchuangyu.github.io/2016/07/how-to-bug-author/ to this gist as an example of make a reproducible example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment