在ggtree / ggplot2中拆分图例

时间:2017-07-19 08:07:32

标签: r ggplot2 ggtree

我正在使用一个名为ggtree的软件,它基于ggplot2,允许您可视化系统发育树并向其添加元数据。将元数据添加为彩色热图的功能称为gheatmap,据我所知,它只是将data.frame或matrix中的rownames与树的标签相关联。与之相关的其他语言基本上是ggplot2

数据在此处:https://github.com/CJREID/gheatmap

使用以下代码,我生成了以下image

library(ggtree)

#read in tree
tree <- read.tree("example_tree_overflow.tree")
p <- ggtree(tree)

#add tip labels and scale
p <- p + geom_tiplab(size = 2.4, align = TRUE, linesize = .2) +
                      geom_treescale()

#group and colour clades

p <- p %>% groupClade(node=c(72,75,84)) + aes(color=group) +
  scale_color_discrete() + 
  theme(legend.position = NULL)

#import data.frame and add column names

meta <- read.csv("metadata_overflow.csv", stringsAsFactors = TRUE)
rownames(meta) <- meta[,1]
meta <- meta[,2:7]
colnames(meta) <- c("Origin", "Syndrome", "fimH", "CTX-M","gyrA","parC")

#variable to generate the breaks in the legend

q1 <- c("REF","SAN","OBH","CoH",
      "Cystitis","Pyelonephritis","Sepsis",
      "H22","H30","H41",
      "CTX-M-1_15","CTX-M-9_18","CTX-M-9_27",
      "WT","S83L", "S83L_D87N",
      "WT","S80I_E84V")

#colour values for the legend, sorry they're awful, I will fix them later

c1 <- c("black","blue","green","red",
        "yellow","purple","firebrick",
        "turquoise","purple","olivedrab",
        "red","purple","turquoise",
        "white","purple","turquoise",
        "white", "turquoise")

#assign breaks to colours
names(c1) <- q1

#draw heatmap next to tree, assigning colours correctly to the variables 

gheatmap(p, meta, colnames_position = "top", width = .06, offset = .025, 
          font.size = 2, colnames_angle = 50, colnames_offset_y = .2, 
          colnames_offset_x = -0.0019, hjust = 0) + 
          theme(legend.title = element_text()) +
           scale_fill_manual(name="Origin",
                            breaks=q1, 
                            values=c1)

如您所见,我添加的数据分为6个类别"Origin", "Syndrome", "fimH", "CTX-M", "gyrA", "parC"每个类别都有自己的变量,如下所示,

>plots
$Origin
[1] "REF" "SAN" "OBH" "CoH"

$Syndrome
[1] "Cystitis" "Pyelonephritis" "Sepsis"        

$fimH
[1] "H22" "H30" "H41"

$`CTX-M`
[1] "CTX-M-1_15" "CTX-M-9_18" "CTX-M-9_27"

$gyrA
[1] "WT"        "S83L"      "S83L_D87N"

$parC
[1] "WT"        "S80I_E84V".

我有两个主要问题:

  1. 我想将这些变量分别放在图例上各自的标题下,这样它们就是六个离散的图例。正如你所看到的,我只设法放了一个标题&#34; Origin&#34;超过他们的全部。 (我尝试使用上面的plots对象而不是q1作为breaks scale_fill_manual无法使用

  2. 我希望WT下的变量gyrAWT下的parC分开处理,即在各个分类标题下的图例中有自己的方框

  3. 希望解决方案不像我想的那样复杂

    干杯。

0 个答案:

没有答案