0

I tried adding label to a ggalluvial plot. I only have axis 1 and 2 and when I try to show label, it shows twice.

Here's my code :

ggplot(bdd_1, 
       aes(axis1 = Mef_R21, axis2 = Mef_R22, y=freq, 
           label = scales::percent(round(Effectifs, digits = 2))))+
  geom_alluvium(color= "black",aes(fill=statut)) +
  geom_stratum() +
  geom_text(stat = "stratum", 
            aes(label = after_stat(stratum)),size = 3,discern=FALSE)+
  geom_text(stat = "flow", nudge_x = 0.2, size = 2.5) +
  theme(legend.position = "bottom")

and my result : enter image description here

I would like to delete the label percentage on the right, outside of the chart (in the red circle on the picture). Is there any possibility to do so? Thank you in advance.

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
Chloé
  • 79
  • 6
  • 1
    which one do you mean? the two label columns in the white boxes to the left of the alluvium labels, or one or the two label columns on the left side of the graph?? – tjebo Feb 13 '23 at 14:20
  • 1
    I edited my question to make it clearer. I mean the label in red circle on the picture. – Chloé Feb 13 '23 at 16:18
  • What about the percentages that appear to the right of the first white column on the left? Do you want to keep those, but just not for the white column on the right? If you want to remove both, take out geom_text(stat = "flow"....) – stomper Feb 13 '23 at 22:11

1 Answers1

1

If you want to remove both columns with percentages (the "from" and the "to") you can remove the second geom_text() statement. If you want to remove just the column of percentages on the right (the "to"), then here is an approach that involves overriding the data points generated by the plot, effectively setting the values in the "to" column of the flow label to "".

## Create a base plot that generates the flow label
g <- ggplot(bdd_1, aes(axis1 = Mef_R21, axis2 = Mef_R22, y=freq, label = scales::percent(round(Effectifs, digits = 2)))) +
   geom_text(stat = "flow", nudge_x = 0.2, size = 2.5) 

## Isolate the data points that are generated for the plot
dat <- ggplot_build(g)$data[[1]] 

## Set the label values to "" where flow is equal to "to"
dat$label[dat$flow == "to"] <- ""

## Run your code for the plot including label = dat$label in the second geom_text().
ggplot(bdd_1, aes(axis1 = Mef_R21, axis2 = Mef_R22, y=freq, label = scales::percent(round(Effectifs, digits = 2))))+
    geom_alluvium(color= "black",aes(fill=statut)) +
    geom_stratum() +
    geom_text(stat = "stratum", aes(label = after_stat(stratum)),size = 3,discern=FALSE)+
    geom_text(stat = "flow",label = dat$label, nudge_x = 0.2, size = 2.5) +
    theme(legend.position = "bottom")

enter image description here

stomper
  • 1,252
  • 1
  • 7
  • 12
  • can I request you to have a look at this question https://stackoverflow.com/questions/75607765/ggraph-node-color-and-legend-not-matching – PesKchan Mar 03 '23 at 08:47
  • This approach seems to mess up the mappings between flows and labels. E.g. the top most flow is labeled as 5.0% which is clearly wrong. – Matt Flor Jul 07 '23 at 09:17