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

can ggtreeExtra add errorbar for geom_bar #15

Open
Arvinlike opened this issue Sep 16, 2021 · 2 comments
Open

can ggtreeExtra add errorbar for geom_bar #15

Arvinlike opened this issue Sep 16, 2021 · 2 comments

Comments

@Arvinlike
Copy link

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

@xiangpin
Copy link
Member

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

@Arvinlike
Copy link
Author

Arvinlike commented Nov 5, 2021 via email

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