corybrunson / ggalluvial

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

Allowing position_dodge (and other position_* functions) to work with stat_stratum ? #73

Closed GustafRydevik closed 3 years ago

GustafRydevik commented 3 years ago

Because of the way stat_stratum is implemented, it doesn't seem to be possible to adjust labels to not overlap.

Using e.g. position_dodge or ggstance's position_dodgev causes an error:

Reproducible example (preferably using reprex::reprex())

ggplot(vaccinations %>% filter(!(survey=="ms460_NSA"&response=="Never"&freq>15)), aes(y = freq, x = survey, stratum = response, fill = response, label = response)) + stat_stratum(width = .5) + geom_text(stat = "stratum", aes(label = scales::percent(after_stat(prop), accuracy = .1)), position=position_dodge())

Error in -data$group : invalid argument to unary operator

From digging into it, it seems the reason is that stat_stratum doesn't return a group column, and position_dodge and several other position functions sorts by position and then the group via a call to order(data$xmin, -data$group). Would it be possible to create a group column duplicating the stratum column to allow the above functions to work unhindered? It could be argued that this is an issue with the position functions, but since the above pattern seems common, I wonder if it's worth fixing it here instead?

corybrunson commented 3 years ago

@GustafRydevik there's a lot that might be said about making the ggalluvial stat layers compatible with position options, and i'm not averse to considering it. First, though, please have a look at the vignette on labeling small strata and see if one of the options there can address your specific issue. Thanks for the prompt.

GustafRydevik commented 3 years ago

Thanks @corybrunson - That vignette was exactly what I needed. GGrepel certainly does the trick for now!

corybrunson commented 3 years ago

@GustafRydevik great! Thank you for letting me know. I'll close this issue now, but please feel free to reopen it if you again feel that positioning should be considered.