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

ggsummarytable text angle option not available? #595

Open
mfrisoli126 opened this issue Feb 3, 2024 · 0 comments
Open

ggsummarytable text angle option not available? #595

mfrisoli126 opened this issue Feb 3, 2024 · 0 comments

Comments

@mfrisoli126
Copy link

mfrisoli126 commented Feb 3, 2024

This is mostly a feature request:

It would be awesome if ggsummarytable function could use a text angle argument the same way as geom_text(). Below is an example of how I used ggsummarytable and the digits overlap with large numbers. I would love if I could easily rotate them 90 degrees.

ggpubr::ggsummarytable(data = df.stats, x = paste(color_by), color = paste(color_by),y = "cellcount", size = 2) + ggpubr::clean_table_theme() + theme(legend.position = "none") +
      facet_grid(facets = reformulate(facet_by)) + theme(strip.text.x = element_blank())

Resulting plot:
Screen Shot 2024-02-02 at 7 35 36 PM

If you just edit the function like this, it works:

ggsummarytable <- function (data, x, y, digits = 0, size = 3, angle = 0, color = "black", 
          palette = NULL, facet.by = NULL, labeller = "label_value", 
          position = "identity", ggtheme = theme_pubr(), ...) {
  if (missing(ggtheme) & !is.null(facet.by)) {
    ggtheme <- theme_pubr(border = TRUE)
  }
  if (is.null(names(y))) 
    names(y) <- y
  y_values <- as.vector(y)
  df <- as.data.frame(data)
  df$x <- df[[x]]
  if (color %in% colnames(df)) {
    if (missing(position)) 
      position <- position_dodge(0.8)
    group <- color
  }
  else {
    group <- 1
  }
  df <- df %>% mutate_if(is.double, round, digits) %>% unite(col = "label", 
                                                             !!!syms(y_values), sep = "\n") %>% mutate(y = paste(names(y), 
                                                                                                                 collapse = "\n"))
  p <- ggplot(data, aes(x, y)) + geom_exec(geom_text, data = df, 
                                           label = "label", size = size, color = color, group = group, 
                                           position = position, angle = angle)
  p <- ggpar(p, ggtheme = ggtheme, palette = palette, xlab = x, 
             ...)
  if (!is.null(facet.by)) 
    p <- facet(p, facet.by = facet.by, labeller = labeller, 
               ...)
  p + rremove("ylab")
}

Then users can do this easily:

ggsummarytable(data = df.stats, x = paste(color_by), color = paste(color_by),y = "cellcount", size = 2, angle = 90) + ggpubr::clean_table_theme() + theme(legend.position = "none") +
  facet_grid(facets = reformulate(facet_by)) + theme(strip.text.x = element_blank())

Screen Shot 2024-02-02 at 7 43 13 PM
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

1 participant