Align data plots to phylogenetic tree

19 Views Asked by At

I want to plot mean and variance stacked by a groupping variable against a phylogenetic tree. I don't know how to align these data using facet_plot. A coarse solution is tune xlim and ylim. Is there a way in ggtree or other R package to automatically align them?

Data:

SO <- structure(list(shift = c(-0.0216889450096601, -0.411652308311412, 
-0.0570724636518575, -0.41100106400568, -0.369045640128441, -0.632160569235785, 
-0.255550570100377, -0.00630756399069805, -0.288104931875999, 
-0.192855016590627, -0.93263141339998, -0.472907752709317, 0.51069799233171, 
0.344112579145409, 1.14513506965736, 0.11666321515443, -1.26296832529506, 
-0.532391390865591, -0.4455538697388, -0.220403140452378, -0.134921508566051, 
-0.587152553034172, -0.458687034492577, 0.0339768984309701, -0.385836955992929, 
-0.393000681366831, -0.26248914513895, -0.187791994564941, -0.240687082428943, 
-0.747230583047816, 0.230816331884097, -0.206293443924491, -0.219282407421773, 
-0.259442551296589, 0.272394868996128, 1.08591951294171, 0.637219894858859, 
0.671158210516089, -0.381749430892772, -0.58390761705883, 0.294114721802668, 
-0.118263997072717, -0.289187290917423, -0.174234824681226, 0.480700001748216, 
-0.420142709162172, -1.01245365196402, -1.24282714164633), se = c(0.577750570471131, 
0.522748882812909, 0.11564001733262, 0.181276325978714, 0.132144961022315, 
0.180876809470063, 0.225320946264326, 0.251189198437696, 0.280210405970264, 
0.149702634356556, 0.442824117386369, 0.345811745655904, 0.997995744955102, 
0.330224143206474, 1.01618551544325, 0.197886858375463, 0.426654699029642, 
0.282049242463801, 0.165671305577042, 0.203806852346156, 0.15321664757947, 
0.200439780050859, 0.386219385020743, 0.335435581556047, 0.129834727610614, 
0.150119350469352, 0.147916813382691, 0.128849024639963, 0.113117574902628, 
0.21353783556743, 0.285445990369942, 0.298965580287788, 0.184036025866901, 
0.222735655254364, 0.625257136847936, 0.814255647057691, 0.572716851444291, 
0.41419670659482, 0.322267258846522, 0.238015032112074, 0.396856620485533, 
0.21375163064932, 0.303437399238619, 0.275178821743851, 0.69967135093238, 
0.296816106885213, 0.993888665653909, 0.405659498349374), age = structure(c(2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), .Label = c("A", 
"J"), class = "factor"), name = c("Remiz pendulinus", "Remiz pendulinus", 
"Parus caeruleus", "Parus caeruleus", "Parus major", "Parus major", 
"Aegithalos caudatus", "Aegithalos caudatus", "Cettia cetti", 
"Cettia cetti", "Hippolais polyglotta", "Hippolais polyglotta", 
"Acrocephalus arundinaceus", "Acrocephalus arundinaceus", "Acrocephalus scirpaceus", 
"Acrocephalus scirpaceus", "Sylvia cantillans", "Sylvia cantillans", 
"Sylvia melanocephala", "Sylvia melanocephala", "Sylvia atricapilla", 
"Sylvia atricapilla", "Turdus philomelos", "Turdus philomelos", 
"Turdus merula", "Turdus merula", "Luscinia megarhynchos", "Luscinia megarhynchos", 
"Erithacus rubecula", "Erithacus rubecula", "Troglodytes troglodytes", 
"Troglodytes troglodytes", "Certhia brachydactyla", "Certhia brachydactyla", 
"Regulus ignicapilla", "Regulus ignicapilla", "Passer montanus", 
"Passer montanus", "Passer domesticus", "Passer domesticus", 
"Carduelis carduelis", "Carduelis carduelis", "Serinus serinus", 
"Serinus serinus", "Carduelis chloris", "Carduelis chloris", 
"Fringilla coelebs", "Fringilla coelebs"), ordre = c(1L, 1L, 
2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 
10L, 10L, 11L, 11L, 12L, 12L, 13L, 13L, 14L, 14L, 15L, 15L, 16L, 
16L, 17L, 17L, 18L, 18L, 19L, 19L, 20L, 20L, 21L, 21L, 22L, 22L, 
23L, 23L, 24L, 24L)), class = "data.frame", row.names = c(NA, 
-48L))

atree <- structure(list(edge = structure(c(25L, 26L, 27L, 28L, 29L, 30L, 
30L, 29L, 31L, 31L, 28L, 32L, 32L, 27L, 26L, 33L, 34L, 35L, 36L, 
36L, 35L, 34L, 33L, 37L, 37L, 25L, 38L, 39L, 40L, 41L, 41L, 40L, 
39L, 42L, 43L, 43L, 42L, 39L, 44L, 44L, 38L, 45L, 46L, 46L, 45L, 
26L, 27L, 28L, 29L, 30L, 12L, 13L, 31L, 14L, 15L, 32L, 16L, 17L, 
18L, 33L, 34L, 35L, 36L, 21L, 22L, 23L, 24L, 37L, 19L, 20L, 38L, 
39L, 40L, 41L, 7L, 8L, 6L, 42L, 43L, 9L, 10L, 11L, 44L, 4L, 5L, 
45L, 46L, 2L, 3L, 1L), .Dim = c(45L, 2L)), tip.label = c("Remiz pendulinus", 
"Cyanistes caeruleus", "Parus major", "Aegithalos caudatus", 
"Cettia cetti", "Hippolais polyglotta", "Acrocephalus arundinaceus", 
"Acrocephalus scirpaceus", "Curruca iberiae", "Curruca melanocephala", 
"Sylvia atricapilla", "Turdus philomelos", "Turdus merula", "Luscinia megarhynchos", 
"Erithacus rubecula", "Troglodytes troglodytes", "Certhia brachydactyla", 
"Regulus ignicapilla", "Passer montanus", "Passer domesticus", 
"Carduelis carduelis", "Serinus serinus", "Chloris chloris", 
"Fringilla coelebs"), Nnode = 22L, edge.length = c(1.02827051586099, 
0.484634391607678, 3.76796190386597, 12.2727951677715, 11.2178070381091, 
12.1273526567436, 12.1273526567436, 7.94415299129617, 15.4010067035565, 
15.4010067035565, 8.98248828956543, 26.6354665730587, 26.6354665730587, 
39.3859167664901, 10.9728528950103, 5.17316714401477, 13.3175132324587, 
2.14243133559783, 8.2645865510162, 8.2645865510162, 10.407017886614, 
23.7245311190728, 22.6634472067282, 6.23425105635935, 6.23425105635935, 
1.90556652718816, 9.82095933749187, 16.513488390144, 2.52279729444108, 
10.1360101246937, 10.1360101246937, 12.6588074191348, 15.6186580787029, 
9.75436939171025, 3.7992683388656, 3.7992683388656, 13.5536377305759, 
6.76920500775894, 22.4030908015198, 22.4030908015198, 5.34933050456991, 
16.1187233515358, 17.525201290665, 17.525201290665, 33.6439246422007
)), class = "phylo", order = "cladewise", RSS = 83.7604001018881)

My code:

library(ggtree)
library(ggplot2)
library(cowplot)
library(phytools)

data <- ggplot(SO, aes(xmin= shift-se, xmax= shift+se, y= ordre, col= age)) +
  geom_linerange(size= 0.5, position= position_dodge2(.5)) +
  geom_pointrange(aes(shift), size= .1,  position= position_dodge2(.5)) +
  guides(y= 'none') + labs(x = '', y = '') + theme(legend.position= 'none',
  plot.background= element_blank(), panel.grid.major= element_blank(),
  panel.grid.minor= element_blank(), panel.border= element_blank(),
  axis.line.x= element_line(size= 0.5, linetype= "solid", colour= "black"))

p <- ggtree(atree, branch.length= "none") + geom_tiplab() + xlim(0, 20) + ylim(-1, 24)

plot_grid(p, data, ncol= 2)
0

There are 0 best solutions below