teunbrand / ggh4x

ggplot extension: options for tailored facets, multiple colourscales and miscellaneous
https://teunbrand.github.io/ggh4x/
Other
564 stars 33 forks source link

[FR] Relative size of facets groups for nested_facets #41

Closed pank closed 3 years ago

pank commented 3 years ago

Currently, nested facets seem to get exactly the same width (for the y-axis) for the outer and the inner facet. But either the inner or the outer facet might have more text.

It would be useful to be able to specify the relative width that the inner and outer facet label each get. For instance in the example below, it would have maybe been nice to specify that the outer facet takes 1/3 of the horizontal space and the inner takes 2/3 of the horizontal space.

I have not been able to figure out how to do this with grid (but I am also not proficient with it).
It seems both facets are in the same grid column, but I can't figure out how to set relative levels.
In any case, it would be nicer to be able to specify this at the ggplot2-level

Many thanks,

Example:

library(ggplot2)
library(grid)
library(ggh4x)
library(patchwork)

df <- data.frame(outer = c("A","B", "B", "C", "C", "C"),
                   inner = paste("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua", 1:6),
                   x=1, y =1)

theme_set(theme_gray() + theme(strip.text.y.left  =  element_text(angle = 0),
                              strip.background.y = element_rect(color = "white", size = 4)
                              ))

g1 <- ggplot(df, aes(x,y)) +
    geom_point() +
    facet_nested(outer + inner ~ .,
                 scales = "free_y",
                 switch = "y",
                 space = "free_y",
                 labeller = label_wrap_gen(40)
                 ) +
    scale_y_discrete(label = NULL)
g1
## It would be nice to somehow tell ggplot2 to give most of the space to the inner facet,
## rather than making each facet level the same width.

image

teunbrand commented 3 years ago

Hi there,

I had this issue myself too (question on stack overflow) and this also happens in facet_grid(), on which facet_nested() is based. The ggplot2 maintainer Thomas is aware of this strip size issue, but it seems that it has been postponed until later. I don't think it makes much sense for me to work on this, if ggplot2 already plans to work on this (and thus facet_nested() will inherit from facet_grid()).

In any case, I don't think setting relative widths would be the solution to this problem. If I get around to #40 you would be able to set different margins for the text in different levels, which feels like a more natural integration than setting relative sizes.

Best, Teun

pank commented 3 years ago

Hi,

Interesting. You are right, this is definitely an upstream issue. I must admit, I forgot to check vanilla ggplot2 as nested facets aren't so useful there.

Thanks for the informative links.

I might see if there's scope for opening an upstream issue.

Thanks!

26 Apr 2021 19.01.18 Teun van den Brand @.***>:

Hi there,

I had this issue myself too (question on stack overflow[https://stackoverflow.com/questions/62595394/crop-strip-background-when-combining-long-and-short-facet-labels]) and this also happens in facet_grid(), on which facet_nested() is based. The ggplot2 maintainer Thomas is aware[https://github.com/tidyverse/ggplot2/pull/3683#issue-355673008] of this strip size issue, but it seems that it has been postponed until later. I don't think it makes much sense for me to work on this, if ggplot2 already plans to work on this (and thus facet_nested() will inherit from facet_grid()).

In any case, I don't think setting relative widths would be the solution to this problem. If I get around to

40[https://github.com/teunbrand/ggh4x/issues/40] you would be able to

set different margins for the text in different levels, which feels like a more natural integration than setting relative sizes.

Best, Teun

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub[https://github.com/teunbrand/ggh4x/issues/41#issuecomment-826999341], or unsubscribe[https://github.com/notifications/unsubscribe-auth/AAFCZXM3ZULB3MHJYPE7AGTTKWL5NANCNFSM43TGNFIQ].

[data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEgAAABICAYAAABV7bNHAAAABHNCSVQICAgIfAhkiAAAACtJREFUeJztwQENAAAAwqD3T20ON6AAAAAAAAAAAAAAAAAAAAAAAAAAAD4MUUgAARy2AfAAAAAASUVORK5CYII=###24x24:true###][Tracking image][https://github.com/notifications/beacon/AAFCZXNHKCVIRBWDZBU53DTTKWL5NA5CNFSM43TGNFI2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOGFFQELI.gif]

teunbrand commented 3 years ago

I'm going to reopen this, because I've changed my mind on the feasability. I've read up some source code and I've become persuaded that this is doable.

teunbrand commented 3 years ago

The following should now work with the github version:

library(ggplot2)
library(ggh4x)

df <- data.frame(outer = c("A","B", "B", "C", "C", "C"),
                 inner = paste("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua", 1:6),
                 x=1, y =1)

theme_set(theme_gray() + theme(strip.text.y.left  =  element_text(angle = 0)))

ggplot(df, aes(x,y)) +
  geom_point() +
  facet_nested(outer + inner ~ .,
               scales = "free_y",
               switch = "y",
               space = "free_y",
               labeller = label_wrap_gen(40),
               strip = strip_nested(size = "variable")
  ) +
  scale_y_discrete(label = NULL)

Created on 2021-06-29 by the reprex package (v1.0.0)

Please be aware that this still may contain some bugs (see #45), and let me know if you find some more :)