YuLab-SMU / ggtreeExtra

:lemon: Add Geom Layers On Circular Or Other Layout Tree Of “ggtree”
https://doi.org/10.1093/molbev/msab166
GNU General Public License v3.0
86 stars 15 forks source link

adjust location of geom_star and geom_bar? #26

Open BinhongLiu opened 1 year ago

BinhongLiu commented 1 year ago

library("ggplot2") library("ggtree") library("ggtreeExtra")

tree

trfile <- system.file("extdata", "tree.nwk", package="ggtreeExtra") tr <- read.tree(trfile) numtip <- length(tr$tip.label)

data

dat2 <- data.frame(ID=tr$tip.label, Location=c(rep("HK", 50), rep("TW", 36), rep("SX", 30), rep("GD", 48), rep("HN", 20), rep("AH", 20), rep("FJ", 26)), Length=abs(rnorm(n=numtip, mean=0.6)), Group=c(rep("Yes", 200), rep("No", 30)), Abundance=abs(rnorm(n=numtip, mean=10, sd=0.00000001)))

p <- ggtree(tr) + geom_tiplab(align=TRUE,fontface="italic",size = 2) + geom_treescale(x=6, y=0, fontsize=1.2, linesize=0.3)+ hexpand(5)

p + geom_fruit_list(geom_fruit(data=dat2, geom=geom_star, mapping=aes(y=ID, fill=Location), pwidth=0.1, stat="identity", orientation="y", axis.param=list(axis="x", nbreak=4, vjust=2), position = position_nudge(50) ), scale_fill_manual(values=c("#D15FEE","#EE6A50","#FFC0CB","#8E8E38","#9ACD32","#006400","#8B4513"))) + new_scale_fill() + geom_fruit(data=dat2, geom=geom_bar, mapping=aes(y=ID, x=Abundance, fill=Location), pwidth=0.1, stat="identity", orientation="y", axis.param=list(axis="x", nbreak=4, vjust=2) )

image

Hi, I want to mark some stars after the tiplab and thereafter add a bar plot. You can see that the bar plot starts from the tip of the tree, but not next after the stars. How should I modify the code to adjust their positions propers? Also, the bar plot is too short now, which makes the whole figure not so pretty. How to scale up and make the bar plot wider? Thanks a lot!

xiangpin commented 1 year ago

Hi, I want to mark some stars after the tiplab and thereafter add a bar plot. You can see that the bar plot starts from the tip of the tree, but not next after the stars. How should I modify the code to adjust their positions propers?

You can use offset (For example offset = .25) parameter to control the distance between external layers, or tree panel and external layers.

library("ggstar")
library("ggplot2")
library("ggtree")
library("ggtreeExtra")

#tree
trfile <- system.file("extdata", "tree.nwk", package="ggtreeExtra")
tr <- read.tree(trfile)
numtip <- length(tr$tip.label)

#data
dat2 <- data.frame(ID=tr$tip.label,
Location=c(rep("HK", 50), rep("TW", 36), rep("SX", 30), rep("GD", 48),
rep("HN", 20), rep("AH", 20), rep("FJ", 26)),
Length=abs(rnorm(n=numtip, mean=0.6)),
Group=c(rep("Yes", 200), rep("No", 30)),
Abundance=abs(rnorm(n=numtip, mean=10, sd=0.00000001)))

p <- ggtree(p) + geom_tiplab(align = T, size = 1.2)
p1 <- p + 
          geom_fruit(
              data = dat2,
              geom = geom_star,
              mapping = aes(y=ID, fill = Location),
              offset = .3
          )
p1

xx1

Also, the bar plot is too short now, which makes the whole figure not so pretty. How to scale up and make the bar plot wider? Thanks a lot!

You can use pwidth parameter to control the width of external layers.

p2 <- p1 + 
          geom_fruit(
              data =dat2,
              geom = geom_col, 
              mapping = aes(x = Abundance, y = ID, fill = Location), pwidth=.4)
p2

xx2