This is a test version of Biostars. For the public version, visit https://www.biostars.org.
R phylogeny plot together with barplot

Hi All,

I was trying to plot a phylogeny tree along with bar plot side by side. I am using the following code. The problem is that my bar plot extend outside the right margin. Any help is appreciated. Thanks in advance.

TREE <- rcoal(10)
tip.order <- TREE$tip.label[TREE$edge[TREE$edge[, 2] <= Ntip(TREE), 2]]
tip.order

data<-matrix(seq(1:10),nrow = 10,ncol = 1,byrow = T)
rownames(data)<-apply(data,1, function(x) paste('t',x,sep = ''))

dordered<-data[tip.order,]

plot(TREE,x.lim=5)

barplot(c(NA,dordered),add=T,horiz = T,space = 0,offset = 1.4,axes = F,axisnames = F,width = 0.95,xlim = (0:10),par(pin=c(1,8)))

lab<-dordered
axis(1,lab + 1.2,label=lab,cex.axis=0.4,padj=0)
r

Thanks. I updated the code.

1 answer

You need to increase the x.lim value when you create the plot. Any value > 10 would do.

plot(TREE, x.lim = 11)

You are setting x.lim = 5 and then trying to add another plot with limits barplot(...xlim = c(0,10)...) that exceeds the previous one.

Thanks komal. Got it.

Log in to answer this question.