corybrunson / ggalluvial

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

wrong labelling of alluvia/strata when decreasing is set #85

Closed otichy closed 3 years ago

otichy commented 3 years ago

Hi,

Thanks for the extension! I want to use it to plot collocations of words per year.

Everything seems to work fine except for labels. I tried to label alluvia or strata using all the techniques mentioned in the vignettes: geom_text, ggrepel, ggfittext, geom_label.

They all work well as long as "decreasing" is not set (NA), but once I set decreasing=FALSE the labels keep their position no longer corresponding to their respective strata or alluvia.

Here is an example (collocations of the word "vakcína" in Czech):

ggplot(data = alluvial_data,
  aes(x = day, y = freq, alluvium = word, label = word)) +
  geom_alluvium(aes(fill = word, colour = word), alpha = .75, size=1) +
  scale_x_continuous(breaks = seq(2001, 2015, 2)) +
  theme(legend.position="bottom") +
  geom_text(stat = "alluvium", size = 4, min.y = 100)

works fine and gives me: plot_zoom_png

but

ggplot(data = alluvial_data,
  aes(x = day, y = freq, alluvium = word, label = word)) +
  geom_alluvium(aes(fill = word, colour = word), alpha = .75, size=1, decreasing=FALSE) +
  scale_x_continuous(breaks = seq(2001, 2015, 2)) +
  theme(legend.position="bottom") +
  geom_text(stat = "alluvium", size = 4, min.y = 100)

gives me a wrongly labelled plot: plot_zoom_png (1)

Is this a bug or am I getting it all wrong?

Best

Ondrej

corybrunson commented 3 years ago

Hi Ondrej, and thanks for checking! Hopefully this can be resolved easily by consistent use of decreasing. The parameter is specific to the alluvium statistical transformation (stat_alluvium()), so you'll want to give it the same specification any time you use stat_alluvium() or stat = "alluvium" inside a geom. Try either or both of these changes and let me know if they don't fix it:

  1. Add decreasing = FALSE to the geom_text() call.
  2. Before plotting, set options(ggalluvial.decreasing = FALSE). (This tells the alluvium stat to use this as the default.)
otichy commented 3 years ago

Cory, this is great, thanks! I should have realised that it is used & specified repeatedly, not just once.