Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjust location of geom_star and geom_bar? #26

Open
BinhongLiu opened this issue Dec 5, 2022 · 1 comment
Open

adjust location of geom_star and geom_bar? #26

BinhongLiu opened this issue Dec 5, 2022 · 1 comment

Comments

@BinhongLiu
Copy link

BinhongLiu commented Dec 5, 2022

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
Copy link
Member

xiangpin commented Dec 9, 2022

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

@xiangpin xiangpin closed this as completed Dec 9, 2022
@xiangpin xiangpin reopened this Dec 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants