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

two issues about geom_bar with the same number #2

Closed xiayh17 closed 4 years ago

xiayh17 commented 4 years 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, layout="circular", size=0.1) + geom_treescale(x=6, y=0, fontsize=1.2, linesize=0.3)

p + geom_fruit(data=dat2,
               geom=geom_bar,
               mapping=aes(y=ID, x=Abundance, fill=Location),
               width=1,
               stat="identity",
               orientation="y") +
  scale_fill_manual(values=c("#D15FEE","#EE6A50","#FFC0CB","#8E8E38","#9ACD32","#006400","#8B4513"),
                    guide=guide_legend(keywidth=0.5, keyheight=0.5, order=6)) +
  theme(legend.position=c(0.95, 0.5),
        legend.title=element_text(size=7),
        legend.text=element_text(size=6),
        legend.spacing.y = unit(0.02, "cm"))

image

There are two issues about using the geom_fruit plot bar plot 1. The code can be run smoothly. But the results confuse me a bit. The values of the bars are almost the same, so why do they look so uneven? Shouldn't these bars be the same length?

2. I try to set Abundance to the same value, for example: Abundance=rep(10,times=numtip) In this case there is an error that occurs. image

xiangpin commented 4 years ago

In fact, it is because the bar and tree are the same panel. when the values of bar plot are bigger than the value of tree. The tree will be squeezed. To solve the problem, The value of the bar plot will be normalized to the range of value of tree. And this can be controlled by pwidth parameter. It controls the width of the whole bar layer. In your first case, the disparity of values of the barplot is very small. But it does not mean inexistent. You can add geom_axis_text to show the label of the barplot.

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, layout="circular", size=0.1) + geom_treescale(x=6, y=0, fontsize=1.2, linesize=0.3)

p + geom_fruit(data=dat2,
               geom=geom_bar,
               mapping=aes(y=ID, x=Abundance, fill=Location),
               pwidth=0.1,
               stat="identity",
               orientation="y") +
  scale_fill_manual(values=c("#D15FEE","#EE6A50","#FFC0CB","#8E8E38","#9ACD32","#006400","#8B4513"),
                    guide=guide_legend(keywidth=0.5, keyheight=0.5, order=6)) +
  theme(legend.position=c(0.95, 0.5),
        legend.title=element_text(size=7),
        legend.text=element_text(size=6),
        legend.spacing.y = unit(0.02, "cm")) +
  geom_axis_text(angle=-45, hjust=0)

test1

you can adjust the pwidth to control the width of the barplot layer. When the values are constant, It is supported now, You can reinstall it.

xiayh17 commented 4 years ago

In fact, it is because the bar and tree are the same panel. when the values of bar plot are bigger than the value of tree. The tree will be squeezed. To solve the problem, The value of the bar plot will be normalized to the range of value of tree. And this can be controlled by pwidth parameter. It controls the width of the whole bar layer. In your first case, the disparity of values of the barplot is very small. But it does not mean inexistent. You can add geom_axis_text to show the label of the barplot.

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, layout="circular", size=0.1) + geom_treescale(x=6, y=0, fontsize=1.2, linesize=0.3)

p + geom_fruit(data=dat2,
               geom=geom_bar,
               mapping=aes(y=ID, x=Abundance, fill=Location),
               pwidth=0.1,
               stat="identity",
               orientation="y") +
  scale_fill_manual(values=c("#D15FEE","#EE6A50","#FFC0CB","#8E8E38","#9ACD32","#006400","#8B4513"),
                    guide=guide_legend(keywidth=0.5, keyheight=0.5, order=6)) +
  theme(legend.position=c(0.95, 0.5),
        legend.title=element_text(size=7),
        legend.text=element_text(size=6),
        legend.spacing.y = unit(0.02, "cm")) +
  geom_axis_text(angle=-45, hjust=0)

test1

you can adjust the pwidth to control the width of the barplot layer. When the values are constant, It is supported now, You can reinstall it.

Thanks for your help. Problems have been solved.

xiangpin commented 3 years ago

The internal function has been optimized.

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, layout="circular", size=0.1) + geom_treescale(x=6, y=0, fontsize=1.2, linesize=0.3)

p + 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=1)
   ) +
  scale_fill_manual(values=c("#D15FEE","#EE6A50","#FFC0CB","#8E8E38","#9ACD32","#006400","#8B4513"),
                    guide=guide_legend(keywidth=0.5, keyheight=0.5, order=6)) +
  theme(legend.position=c(0.95, 0.5),
        legend.title=element_text(size=7),
        legend.text=element_text(size=6),
        legend.spacing.y = unit(0.02, "cm"))

test2