Add legend into ggtree (ggplot) R

ぐ巨炮叔叔 提交于 2019-12-24 02:54:09

问题


I'm building a tree with ggtree on R.

library(ggtree)

here is the newick file content:

((Tribolium_castaneum:1.00000,Anoplophora_glabripennis:1.00000):1.23589,(((((Ichneumoninae_B:1.00000,Ichneumoninae_A:1.00000)1:1.27089,((Tryphoninae_A:1.00000,Tryphoninae_B:1.00000)1:1.34418,((Ophioninae_A:1.00000,Campopleginae:1.00000)1:0.14225,(Cremastinae_B:1.00000,Cremastinae_A:1.00000)1:0.66653)1:0.19416)1:0.08638)1:0.63371,(((Microplitis_demolitor:1.00000,Cotesia_vestalis:1.00000)1:1.02258,(Macrocentrus_cingulum:1.00000,(Meteorus_colon_F:1.00000,Meteorus_cinctellus:1.00000)1:1.63033)1:0.04483)1:0.17281,(Aphidius_colemani:1.00000,(Fopius_arisanus:1.00000,(Diachasma_alloeum:1.00000,(Psyttalia_lounsbury:1.00000,Psyttalia_concolor:1.00000)1:0.70706)1:0.12291)1:1.20999)1:0.07801)1:0.39656)1:0.33771,((Orussus_abietinus:1.00000,(Cephus_cinctus:1.00000,(Neodiprion_lecontei:1.00000,Athalia_rosae:1.00000)1:1.17186)1:0.06599)1:0.09879,(Goniozus_legneri:1.00000,((Polistes_canadensis:1.00000,Polistes_dominulam:1.00000)1:1.90810,(((Dufourea_novaeangliae:1.00000,(Lasioglossum_albipes:1.00000,Nomia_melanderi:1.00000)1:0.20141)1:0.39884,(Megachile_rotundata:1.00000,(Ceratina_calcarata:1.00000,(Habropoda_laboriosa:1.00000,((Euglossa_dilemma:1.00000,Eufriesea_mexicana:1.00000)1:0.83283,(((Lepidotrigona_ventralis:1.00000,Melipona_quadrifasciata:1.00000)1:0.60748,(Bombus_impatiens:1.00000,Bombus_terrestris:1.00000)1:0.95254)1:0.18916,(Apis_mellifera:1.00000,(Apis_florea:1.00000,(Apis_dorsata:1.00000,Apis_cerana:1.00000)1:0.14919)1:0.04191)1:1.01881)1:0.03751)1:0.19407)1:0.05417)1:0.12994)1:0.16387)1:0.77078,((Dinoponera_quadriceps:1.00000,Harpegnathos_saltator:1.00000)1:0.80142,(Pseudomyrmex_gracilis:1.00000,(Ooceraea_biroi:1.00000,((Linepithema_humile:1.00000,(Lasius_niger:1.00000,(Formica_exsecta:1.00000,Camponotus_floridanus:1.00000)1:0.12539)1:0.61781)0.98:0.03658,(Pogonomyrmex_barbatus:1.00000,(Vollenhovia_emeryi:1.00000,((Monomorium_pharaonis:1.00000,(Solenopsis_fugax:1.00000,Solenopsis_invicta:1.00000)1:0.46257)1:0.21934,((Wasmannia_auropunctata:1.00000,(Cyphomyrmex_costatus:1.00000,(Trachymyrmex_zeteki:1.00000,(Trachymyrmex_septentrionalis:1.00000,(Trachymyrmex_cornetzi:1.00000,(Acromyrmex_echinatior:1.00000,(Atta_cephalotes:1.00000,Atta_colombica:1.00000)1:0.41172)1:0.15915)1:0.05554)1:0.28594)1:0.13185)1:0.55280)1:0.03648,(Temnothorax_curvispinosus:1.00000,(Aphaenogaster_rudis:1.00000,(Aphaenogaster_picea:1.00000,(Aphaenogaster_miamiana:1.00000,(Aphaenogaster_fulva:1.00000,(Aphaenogaster_ashmeadi:1.00000,Aphaenogaster_floridana:1.00000)1:0.18360)1:0.28954)1:0.34182)1:0.32515)1:1.17754)1:0.04502)0.64:0.01460)0.89:0.02079)1:0.12106)1:0.31960)1:0.08063)1:0.05751)1:0.17326)1:0.76734)1:0.06023)1:0.14466)1:0.25842)0.97:0.03545)0.99:0.03857,((((Platygaster_equestris:1.00000,Platygaster_orseoliae:1.00000)1:0.69930,(Trissolcus_brochymenae:1.00000,Trissolcus_japonicus:1.00000)1:1.79298)1:0.22798,((Trichopria_sp_970989:1.00000,Trichopria_drosophilae:1.00000)1:0.85941,((Trichogramma_brassicae:1.00000,Trichogramma_pretiosum:1.00000)1:2.32841,(Eretmocerus_eremicus:1.00000,(Encarsia_formosa:1.00000,((Anagyrus_pseudococci:1.00000,(Copidosoma_sp:1.00000,Copidosoma_floridanum:1.00000)1:1.04986)1:0.46123,((Ceratosolen_corneri:1.00000,(Ceratosolen_marchali:1.00000,Ceratosolen_solmsi:1.00000)1:0.66285)1:1.36401,((Ormyrus_nitidulus:1.00000,Ormyrus_pomaceus:1.00000)1:1.57330,((Torymus_sinensis:1.00000,(Torymus_geranii:1.00000,Torymus_flavipes:1.00000)1:0.17300)1:1.18566,(((Eupelmus_urozonus:1.00000,Eupelmus_kiefferif:1.00000)1:0.22452,(Eupelmus_annulatus:1.00000,Eupelmus_azureus:1.00000)1:0.47943)1:1.14280,((Megastigmus_dorsalis:1.00000,Megastigmus_stigmatizans:1.00000)1:1.33147,((Cecidostiba_fungosa:1.00000,Cecidostiba_semifascia:1.00000)1:0.13650,(Trichomalopsis_sarcophagae:1.00000,(Nasonia_vitripennis:1.00000,(Nasonia_giraulti:1.00000,Nasonia_longicornis:1.00000)1:0.21751)1:0.23007)1:0.53980)1:0.92370)0.48:0.00548)1:0.05788)0.84:0.02621)0.96:0.02843)1:0.07843)0.77:0.01624)0.8:0.02331)0.92:0.04228)1:0.66512)1:0.21996)1:0.04466,(((Ganaspis_sp:1.00000,Ganaspis_brasiliensis:1.00000)1:0.27165,(Lep_boulardi:1.00000,(Lep_heterotoma:1.00000,Lep_clavipes:1.00000)1:0.14791)1:0.64772)1:0.52394,(Synergus_japonicus:1.00000,Synergus_umbraculus:1.00000)1:1.41758)1:0.94088)1:0.08418):0.06505)1; 

tree = read.newick("newick_tree")

Here is the code I use:

colfunc <- colorRampPalette(c("red", "blue"))
col=colfunc(110)


for (i in 1:length(tree$node.label)){
  if(as.vector(tree$node.label[i])>0.99){
    col[i]<-'#CB1414'
  } else if(as.vector(tree$node.label[i])>0.90 & (as.vector(tree$node.label[i]) < 0.99)){
    col[i]<-'#F0F014'
  } else if(as.vector(tree$node.label[i])>0.76 & (as.vector(tree$node.label[i]) < 0.90)){
    col[i]<-'#098527'
  } else if(as.vector(tree$node.label[i])>0.51 & (as.vector(tree$node.label[i]) < 0.76)){
    col[i]<-'#18D8F7'
  } else if(as.vector(tree$node.label[i])>0 & (as.vector(tree$node.label[i]) < 0.50)){
    col[i]<-'#1B72DB'
  }
}
p<-ggtree(tree,size=0.5,branch.length="none")   +geom_tiplab(size = 2, col ="black")  + geom_nodepoint(color=col, alpha=1, size=1.5, show.legend = TRUE)+ 
  geom_treescale(x=30, y=1)

In order to get point color depending on the bootstrap level I used this part: geom_nodepoint(color=col, alpha=1, size=1.5, show.legend = TRUE) where col is a variable such as:

> col
  [1] "#CB1414" "#FC0002" "#FA0004" "#F70007" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414"
 [15] "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#F0F014" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414"
 [29] "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414"
 [43] "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#F0F014" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#098527" "#CB1414"
 [57] "#CB1414" "#18D8F7" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414"
 [71] "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#F0F014" "#098527" "#098527" "#CB1414"
 [85] "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#F0F014" "#CB1414" "#098527" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#1B72DB"
 [99] "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414"

That I made depending on the number in this variable:

> tree$node.labe
  [1] "1"    ""     ""     "0.99" "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"   
 [21] "1"    "0.97" "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"   
 [41] "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "0.98" "1"    "1"    "1"    "1"    "0.89" "1"    "1"    "0.64" "1"    "1"   
 [61] "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"   
 [81] "0.92" "0.8"  "0.77" "1"    "1"    "1"    "1"    "1"    "0.96" "1"    "0.84" "1"    "1"    "1"    "1"    "1"    "1"    "0.48" "1"    "1"   
[101] "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"    "1"   

And now I just would like to display the 5 ranges color legend in the tree such as :

but the option show.legend = TRUE does not work for that. Does anyone have an idea? I also tried to add p + theme(legend.position="right") but there is still no legend.

Thank you for your help.

here is the deput(tree)

> dput(tree)
structure(list(edge = structure(c(112L, 113L, 113L, 112L, 114L, 
115L, 116L, 117L, 118L, 118L, 117L, 119L, 120L, 120L, 119L, 121L, 
122L, 122L, 121L, 123L, 123L, 116L, 124L, 125L, 126L, 126L, 125L, 
127L, 127L, 128L, 128L, 124L, 129L, 129L, 130L, 130L, 131L, 131L, 
132L, 132L, 115L, 133L, 134L, 134L, 135L, 135L, 136L, 136L, 133L, 
137L, 137L, 138L, 139L, 139L, 138L, 140L, 141L, 142L, 142L, 143L, 
143L, 141L, 144L, 144L, 145L, 145L, 146L, 146L, 147L, 148L, 148L, 
147L, 149L, 150L, 151L, 151L, 150L, 152L, 152L, 149L, 153L, 153L, 
154L, 154L, 155L, 155L, 140L, 156L, 157L, 157L, 156L, 158L, 158L, 
159L, 159L, 160L, 161L, 161L, 162L, 162L, 163L, 163L, 160L, 164L, 
164L, 165L, 165L, 166L, 167L, 167L, 168L, 168L, 166L, 169L, 170L, 
170L, 171L, 171L, 172L, 172L, 173L, 173L, 174L, 174L, 175L, 175L, 
176L, 176L, 169L, 177L, 177L, 178L, 178L, 179L, 179L, 180L, 180L, 
181L, 181L, 182L, 182L, 114L, 183L, 184L, 185L, 186L, 186L, 185L, 
187L, 187L, 184L, 188L, 189L, 189L, 188L, 190L, 191L, 191L, 190L, 
192L, 192L, 193L, 193L, 194L, 195L, 195L, 196L, 196L, 194L, 197L, 
198L, 198L, 199L, 199L, 197L, 200L, 201L, 201L, 200L, 202L, 203L, 
203L, 204L, 204L, 202L, 205L, 206L, 207L, 207L, 206L, 208L, 208L, 
205L, 209L, 210L, 210L, 209L, 211L, 212L, 212L, 211L, 213L, 213L, 
214L, 214L, 215L, 215L, 183L, 216L, 217L, 218L, 218L, 217L, 219L, 
219L, 220L, 220L, 216L, 221L, 221L, 113L, 1L, 2L, 114L, 115L, 
116L, 117L, 118L, 3L, 4L, 119L, 120L, 5L, 6L, 121L, 122L, 7L, 
8L, 123L, 9L, 10L, 124L, 125L, 126L, 11L, 12L, 127L, 13L, 128L, 
14L, 15L, 129L, 16L, 130L, 17L, 131L, 18L, 132L, 19L, 20L, 133L, 
134L, 21L, 135L, 22L, 136L, 23L, 24L, 137L, 25L, 138L, 139L, 
26L, 27L, 140L, 141L, 142L, 28L, 143L, 29L, 30L, 144L, 31L, 145L, 
32L, 146L, 33L, 147L, 148L, 34L, 35L, 149L, 150L, 151L, 36L, 
37L, 152L, 38L, 39L, 153L, 40L, 154L, 41L, 155L, 42L, 43L, 156L, 
157L, 44L, 45L, 158L, 46L, 159L, 47L, 160L, 161L, 48L, 162L, 
49L, 163L, 50L, 51L, 164L, 52L, 165L, 53L, 166L, 167L, 54L, 168L, 
55L, 56L, 169L, 170L, 57L, 171L, 58L, 172L, 59L, 173L, 60L, 174L, 
61L, 175L, 62L, 176L, 63L, 64L, 177L, 65L, 178L, 66L, 179L, 67L, 
180L, 68L, 181L, 69L, 182L, 70L, 71L, 183L, 184L, 185L, 186L, 
72L, 73L, 187L, 74L, 75L, 188L, 189L, 76L, 77L, 190L, 191L, 78L, 
79L, 192L, 80L, 193L, 81L, 194L, 195L, 82L, 196L, 83L, 84L, 197L, 
198L, 85L, 199L, 86L, 87L, 200L, 201L, 88L, 89L, 202L, 203L, 
90L, 204L, 91L, 92L, 205L, 206L, 207L, 93L, 94L, 208L, 95L, 96L, 
209L, 210L, 97L, 98L, 211L, 212L, 99L, 100L, 213L, 101L, 214L, 
102L, 215L, 103L, 104L, 216L, 217L, 218L, 105L, 106L, 219L, 107L, 
220L, 108L, 109L, 221L, 110L, 111L), .Dim = c(220L, 2L)), edge.length = c(1.23589, 
1, 1, 0.06505, 0.03857, 0.33771, 0.63371, 1.27089, 1, 1, 0.08638, 
1.34418, 1, 1, 0.19416, 0.14225, 1, 1, 0.66653, 1, 1, 0.39656, 
0.17281, 1.02258, 1, 1, 0.04483, 1, 1.63033, 1, 1, 0.07801, 1, 
1.20999, 1, 0.12291, 1, 0.70706, 1, 1, 0.03545, 0.09879, 1, 0.06599, 
1, 1.17186, 1, 1, 0.25842, 1, 0.14466, 1.9081, 1, 1, 0.06023, 
0.77078, 0.39884, 1, 0.20141, 1, 1, 0.16387, 1, 0.12994, 1, 0.05417, 
1, 0.19407, 0.83283, 1, 1, 0.03751, 0.18916, 0.60748, 1, 1, 0.95254, 
1, 1, 1.01881, 1, 0.04191, 1, 0.14919, 1, 1, 0.76734, 0.80142, 
1, 1, 0.17326, 1, 0.05751, 1, 0.08063, 0.03658, 1, 0.61781, 1, 
0.12539, 1, 1, 0.3196, 1, 0.12106, 1, 0.02079, 0.21934, 1, 0.46257, 
1, 1, 0.0146, 0.03648, 1, 0.5528, 1, 0.13185, 1, 0.28594, 1, 
0.05554, 1, 0.15915, 1, 0.41172, 1, 1, 0.04502, 1, 1.17754, 1, 
0.32515, 1, 0.34182, 1, 0.28954, 1, 0.1836, 1, 1, 0.08418, 0.04466, 
0.22798, 0.6993, 1, 1, 1.79298, 1, 1, 0.21996, 0.85941, 1, 1, 
0.66512, 2.32841, 1, 1, 0.04228, 1, 0.02331, 1, 0.01624, 0.46123, 
1, 1.04986, 1, 1, 0.07843, 1.36401, 1, 0.66285, 1, 1, 0.02843, 
1.5733, 1, 1, 0.02621, 1.18566, 1, 0.173, 1, 1, 0.05788, 1.1428, 
0.22452, 1, 1, 0.47943, 1, 1, 0.00548, 1.33147, 1, 1, 0.9237, 
0.1365, 1, 1, 0.5398, 1, 0.23007, 1, 0.21751, 1, 1, 0.94088, 
0.52394, 0.27165, 1, 1, 0.64772, 1, 0.14791, 1, 1, 1.41758, 1, 
1), Nnode = 110L, node.label = c("1", "", "", "0.99", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "0.97", "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", "0.98", "1", "1", "1", "1", "0.89", 
"1", "1", "0.64", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"0.92", "0.8", "0.77", "1", "1", "1", "1", "1", "0.96", "1", 
"0.84", "1", "1", "1", "1", "1", "1", "0.48", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1"), tip.label = c("Tribolium_castaneum", 
"Anoplophora_glabripennis", "Ichneumoninae_B", "Ichneumoninae_A", 
"Tryphoninae_A", "Tryphoninae_B", "Ophioninae_A", "Campopleginae", 
"Cremastinae_B", "Cremastinae_A", "Microplitis_demolitor", "Cotesia_vestalis", 
"Macrocentrus_cingulum", "Meteorus_colon_F", "Meteorus_cinctellus", 
"Aphidius_colemani", "Fopius_arisanus", "Diachasma_alloeum", 
"Psyttalia_lounsbury", "Psyttalia_concolor", "Orussus_abietinus", 
"Cephus_cinctus", "Neodiprion_lecontei", "Athalia_rosae", "Goniozus_legneri", 
"Polistes_canadensis", "Polistes_dominulam", "Dufourea_novaeangliae", 
"Lasioglossum_albipes", "Nomia_melanderi", "Megachile_rotundata", 
"Ceratina_calcarata", "Habropoda_laboriosa", "Euglossa_dilemma", 
"Eufriesea_mexicana", "Lepidotrigona_ventralis", "Melipona_quadrifasciata", 
"Bombus_impatiens", "Bombus_terrestris", "Apis_mellifera", "Apis_florea", 
"Apis_dorsata", "Apis_cerana", "Dinoponera_quadriceps", "Harpegnathos_saltator", 
"Pseudomyrmex_gracilis", "Ooceraea_biroi", "Linepithema_humile", 
"Lasius_niger", "Formica_exsecta", "Camponotus_floridanus", "Pogonomyrmex_barbatus", 
"Vollenhovia_emeryi", "Monomorium_pharaonis", "Solenopsis_fugax", 
"Solenopsis_invicta", "Wasmannia_auropunctata", "Cyphomyrmex_costatus", 
"Trachymyrmex_zeteki", "Trachymyrmex_septentrionalis", "Trachymyrmex_cornetzi", 
"Acromyrmex_echinatior", "Atta_cephalotes", "Atta_colombica", 
"Temnothorax_curvispinosus", "Aphaenogaster_rudis", "Aphaenogaster_picea", 
"Aphaenogaster_miamiana", "Aphaenogaster_fulva", "Aphaenogaster_ashmeadi", 
"Aphaenogaster_floridana", "Platygaster_equestris", "Platygaster_orseoliae", 
"Trissolcus_brochymenae", "Trissolcus_japonicus", "Trichopria_sp_970989", 
"Trichopria_drosophilae", "Trichogramma_brassicae", "Trichogramma_pretiosum", 
"Eretmocerus_eremicus", "Encarsia_formosa", "Anagyrus_pseudococci", 
"Copidosoma_sp", "Copidosoma_floridanum", "Ceratosolen_corneri", 
"Ceratosolen_marchali", "Ceratosolen_solmsi", "Ormyrus_nitidulus", 
"Ormyrus_pomaceus", "Torymus_sinensis", "Torymus_geranii", "Torymus_flavipes", 
"Eupelmus_urozonus", "Eupelmus_kiefferif", "Eupelmus_annulatus", 
"Eupelmus_azureus", "Megastigmus_dorsalis", "Megastigmus_stigmatizans", 
"Cecidostiba_fungosa", "Cecidostiba_semifascia", "Trichomalopsis_sarcophagae", 
"Nasonia_vitripennis", "Nasonia_giraulti", "Nasonia_longicornis", 
"Ganaspis_sp", "Ganaspis_brasiliensis", "Lep_boulardi", "Lep_heterotoma", 
"Lep_clavipes", "Synergus_japonicus", "Synergus_umbraculus")), class = "phylo", order = "cladewise")

回答1:


The number of node points in your tree is 221, with 111 final points. Thus, your col vector needs to have 221 elements; the first 111 specify the colors of the final points.
Here is a way to create the proper col vector starting from tree$node.label:

x <- c(rep(NA,111), as.numeric(tree$node.label)*100)
col <- cut(x, breaks=c(0,50,75,90,99,100))
col <- factor(col, levels=rev(levels(col)))
col <- factor(col, labels=c("100%","91-99%","76-90%","51-75%","0-50%"))

Node points can be colored using the color aesthetic:

library(ggtree)
library(ggplot2)
p <- ggtree(tree, size=0.5,branch.length="none")   +
    geom_tiplab(size = 2, col ="black")  + 
    geom_point(aes(color=col), alpha=1, size=1.5, show.legend = TRUE) + 
    geom_treescale(x=30, y=1) +
    theme(legend.position="right") +
    scale_colour_manual(na.translate = F, name="Bootstrap support",
                        values=c("#CB1414","#F0F014","#098527","#18D8F7","#1B72DB")) +
    guides(color = guide_legend(override.aes = list(size = 5))) 
p



来源:https://stackoverflow.com/questions/55722055/add-legend-into-ggtree-ggplot-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!