代码之家  ›  专栏  ›  技术社区  ›  abichat

ggtree::facet\u plot-第二个面板使用第一个面板中的xlim参数

  •  2
  • abichat  · 技术社区  · 7 年前

    我已经对很多属进行了统计测试,这些属都是用树进行层次结构的,所以我对树中的每个属都有一个p值。

    我想在面板图中可视化树和p值 ggtree .

    包和树数据:

    library(ape)
    # source("https://bioconductor.org/biocLite.R"); biocLite("ggtree")
    library(ggtree)
    
    tree <- structure(list(
      edge = structure(c(102L, 103L, 104L, 105L, 106L, 
                         107L, 103L, 108L, 109L, 110L, 111L, 111L, 109L, 112L, 113L, 109L, 
                         114L, 115L, 115L, 115L, 115L, 115L, 115L, 114L, 116L, 109L, 117L, 
                         118L, 108L, 119L, 120L, 121L, 119L, 122L, 123L, 123L, 108L, 124L, 
                         125L, 126L, 125L, 127L, 127L, 108L, 128L, 129L, 130L, 130L, 129L, 
                         131L, 103L, 132L, 133L, 134L, 135L, 134L, 136L, 136L, 134L, 137L, 
                         137L, 134L, 138L, 138L, 134L, 139L, 139L, 134L, 140L, 134L, 141L, 
                         141L, 103L, 142L, 143L, 144L, 145L, 103L, 146L, 147L, 148L, 149L, 
                         146L, 150L, 151L, 152L, 103L, 153L, 154L, 155L, 156L, 153L, 157L, 
                         158L, 159L, 159L, 159L, 159L, 159L, 157L, 160L, 161L, 160L, 162L, 
                         103L, 163L, 164L, 165L, 166L, 165L, 167L, 167L, 165L, 168L, 168L, 
                         165L, 169L, 164L, 170L, 171L, 170L, 172L, 163L, 173L, 174L, 175L, 
                         175L, 175L, 173L, 176L, 177L, 177L, 177L, 173L, 178L, 179L, 179L, 
                         163L, 180L, 181L, 182L, 181L, 183L, 183L, 183L, 181L, 184L, 184L, 
                         184L, 184L, 184L, 184L, 184L, 184L, 184L, 184L, 181L, 185L, 185L, 
                         185L, 181L, 186L, 181L, 187L, 187L, 181L, 188L, 188L, 188L, 188L, 
                         188L, 163L, 189L, 190L, 191L, 191L, 163L, 192L, 193L, 194L, 194L, 
                         194L, 194L, 194L, 194L, 194L, 103L, 195L, 196L, 197L, 198L, 196L, 
                         199L, 200L, 103L, 201L, 202L, 203L, 204L, 204L, 102L, 205L, 206L, 
                         207L, 208L, 209L, 206L, 210L, 211L, 212L, 206L, 213L, 214L, 215L, 
                         103L, 104L, 105L, 106L, 107L, 1L, 108L, 109L, 110L, 111L, 2L, 
                         3L, 112L, 113L, 4L, 114L, 115L, 5L, 6L, 7L, 8L, 9L, 10L, 116L, 
                         11L, 117L, 118L, 12L, 119L, 120L, 121L, 13L, 122L, 123L, 14L, 
                         15L, 124L, 125L, 126L, 16L, 127L, 17L, 18L, 128L, 129L, 130L, 
                         19L, 20L, 131L, 21L, 132L, 133L, 134L, 135L, 22L, 136L, 23L, 
                         24L, 137L, 25L, 26L, 138L, 27L, 28L, 139L, 29L, 30L, 140L, 31L, 
                         141L, 32L, 33L, 142L, 143L, 144L, 145L, 34L, 146L, 147L, 148L, 
                         149L, 35L, 150L, 151L, 152L, 36L, 153L, 154L, 155L, 156L, 37L, 
                         157L, 158L, 159L, 38L, 39L, 40L, 41L, 42L, 160L, 161L, 43L, 162L, 
                         44L, 163L, 164L, 165L, 166L, 45L, 167L, 46L, 47L, 168L, 48L, 
                         49L, 169L, 50L, 170L, 171L, 51L, 172L, 52L, 173L, 174L, 175L, 
                         53L, 54L, 55L, 176L, 177L, 56L, 57L, 58L, 178L, 179L, 59L, 60L, 
                         180L, 181L, 182L, 61L, 183L, 62L, 63L, 64L, 184L, 65L, 66L, 67L, 
                         68L, 69L, 70L, 71L, 72L, 73L, 74L, 185L, 75L, 76L, 77L, 186L, 
                         78L, 187L, 79L, 80L, 188L, 81L, 82L, 83L, 84L, 85L, 189L, 190L, 
                         191L, 86L, 87L, 192L, 193L, 194L, 88L, 89L, 90L, 91L, 92L, 93L, 
                         94L, 195L, 196L, 197L, 198L, 95L, 199L, 200L, 96L, 201L, 202L, 
                         203L, 204L, 97L, 98L, 205L, 206L, 207L, 208L, 209L, 99L, 210L, 
                         211L, 212L, 100L, 213L, 214L, 215L, 101L), .Dim = c(214L, 2L)), 
      edge.length = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
                      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
                      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
                      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
                      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
                      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
                      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
                      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
                      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
                      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
                      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
                      1, 1, 1, 1, 1, 1, 1, 1, 1, 1), 
      Nnode = 114L, 
      tip.label = c("Brachyspira", 
                    "Haemophilus", "Aggregatibacter", "Acinetobacter", "Klebsiella", 
                    "Salmonella", "Escherichia", "Enterobacter", "Shigella", 
                    "Citrobacter", "Hafnia", "Succinatimonas", "Corallococcus", 
                    "Bilophila", "Desulfovibrio", "Azospirillum", "Acidiphilium", 
                    "Acetobacter", "Sutterella", "Parasutterella", "Oxalobacter", 
                    "Porphyromonas", "Paraprevotella", "Prevotella", "Alistipes", 
                    "Rikenella", "Tannerella", "Parabacteroides", "Odoribacter", 
                    "Butyricimonas", "Bacteroides", "Coprobacter", "Barnesiella", 
                    "Fusobacterium", "Coraliomargarita", "Akkermansia", "Bifidobacterium", 
                    "Gordonibacter", "Eggerthella", "Cryptobacterium", "Adlercreutzia", 
                    "Enterorhabdus", "Collinsella", "Olsenella", "Lactobacillus", 
                    "Weissella", "Oenococcus", "Lactococcus", "Streptococcus", 
                    "Enterococcus", "Staphylococcus", "Bacillus", "Dialister", 
                    "Veillonella", "Megasphaera", "Megamonas", "Mitsuokella", 
                    "Selenomonas", "Phascolarctobacterium", "Acidaminococcus", 
                    "Oscillibacter", "Intestinibacter", "Peptoclostridium", "Peptostreptococcus", 
                    "Dorea", "Roseburia", "Anaerostipes", "Tyzzerella", "Coprococcus", 
                    "Blautia", "Butyrivibrio", "Marvinbryantia", "Lachnoclostridium", 
                    "Oribacterium", "Flavonifractor", "Intestinimonas", "Pseudoflavonifractor", 
                    "Eubacterium", "Clostridium", "Butyricicoccus", "Faecalibacterium", 
                    "Ruminococcus", "Anaerotruncus", "Subdoligranulum", "Ruminiclostridium", 
                    "Parvimonas", "Peptoniphilus", "Catenibacterium", "Solobacterium", 
                    "Coprobacillus", "Holdemania", "Erysipelatoclostridium", 
                    "Turicibacter", "Stoquefichus", "Mycoplasma", "Acholeplasma", 
                    "Pyramidobacter", "Synergistes", "Methanobrevibacter", "Methanomethylophilus", 
                    "Methanoculleus"), root.edge = 1), 
      .Names = c("edge", "edge.length", 
                 "Nnode", "tip.label", "root.edge"), class = "phylo", order = "cladewise"
    )
    

    代码:

    df <- data.frame(id = tree$tip.label, p = runif(length(tree$tip.label)))
    
    p1 <- ggtree(tree) +
      geom_tiplab()
    
    facet_plot(p1, panel = "p-value", data = df, geom = geom_point, aes(x = p))
    

    enter image description here

    但是这里,属的名称被截断了,所以我修改了 xlim 参数来完整地查看它们。

    p2 <- 
      ggtree(tree) + 
      geom_tiplab() + 
      xlim(c(0,7))
    
    facet_plot(p2, panel = "p-value", data = df, geom = geom_point, aes(x = p))
    

    enter image description here

    它起作用了!然而 xlim公司 扩展到第二个面板。。。我怎样才能解决这个问题?

    我试图添加 xlim(0:1) xlim = 0:1 在里面 facet_plot() 但这不起作用。。。


    编辑 在F.Privé之后回答:

    我需要将标签放在分支的右侧,因为我必须在它们上添加一些标签/统计信息。我希望他们左对齐。

    2 回复  |  直到 7 年前
        1
  •  2
  •   Droplet    7 年前

    ggtree 提供 xlim_expand 专门为此目的发挥作用。

    您需要指定限制以及要将其应用到的面板。在您的情况下,您希望将其应用于 Tree 面板:

    p1 <- ggtree(tree) +
      geom_tiplab(size = 2) + 
      xlim_expand(c(0,15), panel = "Tree")
    
    facet_plot(p1, panel = "p-value", data = df, geom = geom_point, aes(x = p)) 
    

    example_plot

    我列举了这个例子的限制,但你可以根据自己的喜好进行调整。

        2
  •  1
  •   F. Privé    7 年前

    你可以玩 hjust offset 参数(尽管我不太了解):

    p2 <- 
      ggtree(tree) + 
      geom_tiplab(hjust = 1, offset = 8)
    
    facet_plot(p2, panel = "p-value", data = df, geom = geom_point, aes(x = p))