0

I am looking to format the value labels with "," separators, particularly on the stratums (bar columns) of Alluvial/ Sankey plot using R ggalluvial.

While similar answers were found on other charts, the same attempt has returned an error (notice the missing value labels and messed up flow connections):

library(ggplot2)
library(ggalluvial)
library(scales)

vaccinations$freq = vaccinations$freq * 1000 
ggplot(vaccinations,
       aes(x = survey, stratum = response, alluvium = subject,
           y = freq,
           fill = response, label = comma(freq))) +
  scale_x_discrete(expand = c(.1, .1)) +
  geom_flow() +
  geom_stratum(alpha = .5) +
  geom_text(stat = "stratum", size = 3) +
  theme(legend.position = "bottom") +
  ggtitle("vaccination survey responses at three points in time")
Warning message:
Removed 12 rows containing missing values (geom_text).
INeedCodes
  • 61
  • 2
  • 6
  • I think, this has something to do with the way that ggalluvial handles the label information passed to `stat_straum`. It expects a numerical value, and I couldn't find a possibility to change the formatting afterwards. You could try to calculate the content of `stratum` yourself and use a separate dataframe for `geom_text`. – TobiO Sep 03 '19 at 15:53

1 Answers1

1

The internals of ggalluvial prevent this from working, as @TobiO suspects. Specifically, when a numeric-valued variable is passed to label and processed by one of the alluvial stats, it is automatically totaled. When a character-valued variable is passed to label, this can't be done. So the formatting must take place after the statistical transformation.

A solution is provided by ggfittext: The function geom_fit_text() has a formatter parameter to which a formatting function can be passed—though the function must be compatible with the type of variable passed to label! Here's an example:

library(ggalluvial)
#> Loading required package: ggplot2
library(ggfittext)
library(scales)
data(vaccinations)
vaccinations <- transform(vaccinations, freq = freq * 1000)
ggplot(vaccinations,
       aes(x = survey, stratum = response, alluvium = subject,
           y = freq,
           fill = response, label = freq)) +
  scale_x_discrete(expand = c(.1, .1)) +
  geom_flow() +
  geom_stratum(alpha = .5) +
  geom_fit_text(stat = "stratum", size = 10, min.size = 6, formatter = comma) +
  theme(legend.position = "bottom") +
  ggtitle("vaccination survey responses at three points in time")

Created on 2019-09-04 by the reprex package (v0.2.1)

Cory Brunson
  • 668
  • 4
  • 10