corybrunson / ggalluvial

ggplot2 extension for alluvial plots
http://corybrunson.github.io/ggalluvial/
GNU General Public License v3.0
499 stars 34 forks source link

How to adjust box sizes on geom_stratum? #116

Closed aisneto closed 1 year ago

aisneto commented 1 year ago

I am constructing an alluvial diagram to represent the frequency of paired discrete data. Everything went okay using ggplot and ggalluvial. However, in my end result, the text size does not quite fit the boxes created by geom_stratum. I am aware that I can change the text size to make it fit horizontally; however, the height of the boxes with a low count (n=1) is too small to fit any legible text. Making the font as small as these boxes would render the plot unreadable. The height of these boxes is defined by the size of alluvia that connects to them, and I can't find a way to expand those somehow. Could someone help me with this?

My code:

        ggplot(aes(axis1 = Fim.proposto, axis2 = Técnica.utilizada,
                        y = n)) +
    geom_alluvium(aes(fill = Fim.proposto)) +
    geom_stratum() +
    geom_text(stat = "stratum",
              aes(label = after_stat(stratum))) +
    scale_x_discrete(limits = c("Fim.proposto", "Técnica.utilizada"),
                     expand = c(0.15, 0.15)) +
    theme_void() +
    theme(legend.position = "none") +
    scale_color_okabeito()

Result: figura2

corybrunson commented 1 year ago

Hi @aisneto, could you say more specifically what you want to do in this plot? Or, are you looking for more ideas of how to handle alluvial plots with small boxes that nevertheless need labels? I illustrate several options in one of the package vignettes, which you can view on the website here or run as follows:

vignette("labels", package = "ggalluvial")
aisneto commented 1 year ago

Thank you for the indication, @corybrunson. The vignette was very helpful, and I believe the most adequate approach would be applying ggrepel. However, even after using this package, there is still some confusion around the text placement due to some strata being too small and having too many levels. Is there a way to adjust the minimum size for strata, or to add an space between each one?

To clarify, this chart aims to represent the frequency of methods employed for specific applications, and some of these methods were found to be employed in more than one application. Initially, I thought that an alluvial chart would be ideal to represent this relationship, but perhaps it is not? Are there any similar alternatives that you could suggest to express this relationship?

corybrunson commented 1 year ago

Hi @aisneto, i'm glad the vignette helps, and i can point you to a couple of other resources that might be useful to you.

First, see this example from the {ggrepel} site. The direction parameter is useful for allowing labels to dodge each other vertically but remain rigidly positioned horizontally.

Second, if you're not concerned with representing totals along the vertical axis, then you might try the less rigid diagram family provided by {ggsankey}. The README on GitHub shows what you can do, though the syntax is significantly different from that of {ggalluvial}.

Let me know where you want to go from here, and i'll provide any more insight i can!

aisneto commented 1 year ago

Thank you once again for your insights and attention, @corybrunson. I will be closing this, as the suggestions you kindly provided are sufficient to address my issue.