ggtreeExtra icon indicating copy to clipboard operation
ggtreeExtra copied to clipboard

can ggtreeExtra add errorbar for geom_bar

Open Arvinlike opened this issue 4 years ago • 2 comments

sometimes we need to add error_bar for geom_bar, How to make it ?

Arvinlike avatar Sep 16 '21 08:09 Arvinlike

Yes, but the xmin or xmax mapping is not supported by ggtreeExtra. However, the geom_errorbar also can be added by controlling the stat parameters. You can refer to the following codes.

using geomExtra

 library(ggtree)
 library(ggtreeExtra)
 library(ggplot2)
 set.seed(1024)
 tr <- rtree(10)

 dat <- data.frame(id=rep(tr$tip.label, 5), value=abs(rnorm(50)))
 df <- data.frame(id=rep(tr$tip.label), group=rep(c("A", "B", "C", "D", "E"), 2))
 tr %<>% tidytree::left_join(dat, by=c("label"="id")) %>%
         tidytree::left_join(df, by=c("label"="id"))
 tr
 p1 <- tr %>% ggtree() + geom_tiplab()
 p2 <- p1 +
       geom_fruit_list(
           geom_fruit(
               data = td_unnest(value),
               geom = geom_bar,
               mapping = aes(x = value, fill = group),
               stat = "summary",
               fun = mean,
               orientation = "y",
               position = position_identityx(), # This should be position_identidyx() for geom_bar in here.
               pwidth = 3,  # The pwidth and offset should be equal with geom_errorbar layer.
               offset = 0.2
           ),
           geom_fruit(
               data = td_unnest(value),
               geom = geom_errorbar,
               mapping = aes(x = value, group = group),
               stat = "summary",
               fun.data = "mean_se",
               orientation = "y",
               position = position_identityx(),
               width = 0.2, 
               pwidth = 3, # pwidth  and offset should be equal with geom_bar.
               offset = 0.2,
               axis.params = list(axis="x", text.size = 3) 
           )
       )
p2

xx1

# you can also use point to replace bar plot.
pp2 <- p1 +
       geom_fruit_list(
           geom_fruit(
               data = td_unnest(value),
               geom = geom_point,
               mapping = aes(x = value, color = group),
               stat = "summary",
               fun = mean,
               orientation = "y",
               size = 3,
               pwidth = 3,  # The pwidth and offset should be equal with geom_errorbar layer.
               offset = 0.2
           ),
           geom_fruit(
               data = td_unnest(value),
               geom = geom_errorbar,
               mapping = aes(x = value, group = group),
               stat = "summary",
               fun.data = "mean_se",
               orientation = "y",
               position = position_identityx(),
               width = 0.2, 
               pwidth = 3, # pwidth  and offset should be equal with geom_bar.
               offset = 0.2,
               axis.params = list(axis="x", text.size = 3) 
           )
       )
pp2

x3

You can also use geom_facet to display it when the layout of tree is not circular.

 f2 <- p1 +
       geom_facet(
           data = td_unnest(value),
           panel = "errorbar",
           geom = geom_bar,
           mapping = aes(x = value, fill = group),
           stat = "summary",
           fun = mean,
           orientation = "y"
       ) +
       geom_facet(
           data = td_unnest(value),
           panel = "errorbar",
           geom = geom_errorbar,
           mapping = aes(x = value, group = group),
           stat = "summary",
           fun.data = "mean_se",
           orientation = "y",
           width = 0.2
       ) +
       theme_tree2()
f2

xx2

# Using point layer to replace bar plot
library(ggstar)
ff2 <- p1 +
       geom_facet(
           data = td_unnest(value),
           panel = "errorbar",
           geom = geom_star,
           mapping = aes(x = value, fill = group),
           stat = "summary",
           fun = mean,
           size = 3,
           orientation = "y"
       ) +
       geom_facet(
           data = td_unnest(value),
           panel = "errorbar",
           geom = geom_errorbar,
           mapping = aes(x = value, group = group),
           stat = "summary",
           fun.data = "mean_se",
           orientation = "y",
           width = 0.2
       ) +
       theme_tree2()
ff2

xx3

xiangpin avatar Sep 16 '21 12:09 xiangpin

Great Job. Thanks very much ! Best wishes to Yu LAB.

At 2021-09-16 20:54:43, "Shuangbin Xu" @.***> wrote:

Yes, but the xmin or xmax mapping is not supported by ggtreeExtra. However, the geom_errorbar also can be added by controlling the stat parameters. You can refer to the following codes.

using geomExtra library(ggtree) library(ggtreeExtra) library(ggplot2) set.seed(1024) tr <- rtree(10)

dat <- data.frame(id=rep(tr$tip.label, 5), value=abs(rnorm(50))) df <- data.frame(id=rep(tr$tip.label), group=rep(c("A", "B", "C", "D", "E"), 2)) tr %<>% tidytree::left_join(dat, by=c("label"="id")) %>% tidytree::left_join(df, by=c("label"="id")) tr p1 <- tr %>% ggtree() + geom_tiplab() p2 <- p1 + geom_fruit_list( geom_fruit( data = td_unnest(value), geom = geom_bar, mapping = aes(x = value, fill = group), stat = "summary", fun = mean, orientation = "y", position = position_identityx(), # This should be position_identidyx() for geom_bar in here. pwidth = 3, # The pwidth and offset should be equal with geom_errorbar layer. offset = 0.2 ), geom_fruit( data = td_unnest(value), geom = geom_errorbar, mapping = aes(x = value, group = group), stat = "summary", fun.data = "mean_se", orientation = "y", position = position_identityx(), width = 0.2, pwidth = 3, # pwidth and offset should be equal with geom_bar. offset = 0.2, axis.params = list(axis="x", text.size = 3) ) ) p2

you can also use point to replace bar plot.

pp2 <- p1 + geom_fruit_list( geom_fruit( data = td_unnest(value), geom = geom_point, mapping = aes(x = value, color = group), stat = "summary", fun = mean, orientation = "y", size = 3, pwidth = 3, # The pwidth and offset should be equal with geom_errorbar layer. offset = 0.2 ), geom_fruit( data = td_unnest(value), geom = geom_errorbar, mapping = aes(x = value, group = group), stat = "summary", fun.data = "mean_se", orientation = "y", position = position_identityx(), width = 0.2, pwidth = 3, # pwidth and offset should be equal with geom_bar. offset = 0.2, axis.params = list(axis="x", text.size = 3) ) ) pp2

You can also use geom_facet to display it when the layout of tree is not circular.

f2 <- p1 + geom_facet( data = td_unnest(value), panel = "errorbar", geom = geom_bar, mapping = aes(x = value, fill = group), stat = "summary", fun = mean, orientation = "y" ) + geom_facet( data = td_unnest(value), panel = "errorbar", geom = geom_errorbar, mapping = aes(x = value, group = group), stat = "summary", fun.data = "mean_se", orientation = "y", width = 0.2 ) + theme_tree2() f2

Using point layer to replace bar plot

library(ggstar) ff2 <- p1 + geom_facet( data = td_unnest(value), panel = "errorbar", geom = geom_star, mapping = aes(x = value, fill = group), stat = "summary", fun = mean, size = 3, orientation = "y" ) + geom_facet( data = td_unnest(value), panel = "errorbar", geom = geom_errorbar, mapping = aes(x = value, group = group), stat = "summary", fun.data = "mean_se", orientation = "y", width = 0.2 ) + theme_tree2() ff2

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android.

Arvinlike avatar Nov 05 '21 02:11 Arvinlike