how to shift left only the 'label' text of the second node in geom_text

49 Views Asked by At

The problem is that I am graphing some sankeys and visually it is inefficient for me to see the labels this way, I would like to be able to integrate them to the left, there should be a simpler way but I have not been able to achieve it.

#Data example:
base_preferencia <- data.frame(
  Preferencia_1 = c("Bancoppel", "Bancoppel", "Bancoppel", "Banorte", "Banorte", "Banorte"),
  Preferencia_2 = c("Bancoppel", "Banorte", "Santander", "Bancoppel", "Banorte", "Santander"),
  Freq = c(100, 50, 25, 50, 100, 25),
  Colores = c("Azul", "Gris", "Amarillo", "Gris", "Azul", "Amarillo"),
  Transparencia = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5),
  Pct = c(0.4, 0.2, 0.1, 0.2, 0.4, 0.1)
)

My sankey code:

{ggplot(base_preferencia, aes(y = Freq, axis1 = Preferencia_1, axis2 = Preferencia_2)) + 
    geom_alluvium(aes( fill = Colores , alpha = Transparencia), width = 2/12) +
    geom_stratum(width = 2/12, fill = "white", color = "gray20") +
    geom_text(stat = "stratum", aes(label = after_stat(stratum))) +
    geom_text(stat = "flow",
              aes(label = ifelse(round(base_preferencia$Pct*100) >= 1 & base_preferencia$Preferencia_1 == "Bancoppel"|
                                   round(base_preferencia$Pct*100) >= 1 & base_preferencia$Preferencia_2 == "Bancoppel",
                                 paste0(round(base_preferencia$Pct*100),"%"), ""))) +
    scale_fill_manual(values = alpha(c("#1973cd","#E9E9E9","#f7de0b"), 0.5)) +
    lucify_theme_void()+
    guides(fill = guide_legend(nrow = 1))+
    theme(legend.position = "None")}

my plot:

enter image description here

What I want is for the flow text of the second node to be on the left side and not to the right. enter image description here

2

There are 2 best solutions below

0
On BEST ANSWER

One option would be to use two geom_text layers, one for the left and one for the right labels, where I use stage() in aes() to "filter" for the sides.

library(ggalluvial)

base_preferencia$label <-
  ifelse(
    round(base_preferencia$Pct * 100) >= 1 &
      (base_preferencia$Preferencia_1 == "Bancoppel" |
         base_preferencia$Preferencia_2 == "Bancoppel"),
    paste0(round(base_preferencia$Pct * 100), "%"), ""
  )

ggplot(
  base_preferencia,
  aes(y = Freq, axis1 = Preferencia_1, axis2 = Preferencia_2)
) +
  geom_alluvium(
    aes(fill = Colores, alpha = Transparencia),
    width = 2 / 12
  ) +
  geom_stratum(width = 2 / 12, fill = "white", color = "gray20") +
  geom_text(
    stat = "stratum", aes(label = after_stat(stratum))
  ) +
  geom_text(
    stat = "flow",
    aes(
      label = stage(label, after_stat = ifelse(x == 1, label, ""))
    ),
    nudge_x = .1,
    hjust = 0
  ) +
  geom_text(
    stat = "flow",
    aes(
      label = stage(label, after_stat = ifelse(x == 2, label, ""))
    ),
    nudge_x = -.1,
    hjust = 1
  ) +
  scale_fill_manual(
    values = alpha(c("#1973cd", "#E9E9E9", "#f7de0b"), 0.5)
  ) +
  # lucify_theme_void() +
  guides(fill = guide_legend(nrow = 1)) +
  theme(legend.position = "None")

enter image description here

0
On

For people like me, I listened to Stefan's answer and also tried to give it some of my own touches, for those who also need to reject the percentages, here is a possible solution:

{ggplot(base_preferencia, aes(y = Freq, axis1 = Preferencia_1, axis2 = Preferencia_2)) + 
geom_alluvium(aes( fill = Colores , alpha = Transparencia), width = 2/12) +
geom_stratum(width = 2/12, fill = "white", color = "gray20") +
geom_text(stat = "stratum", aes(label = after_stat(stratum))) +
geom_text_repel(stat = "flow",
                aes(label = ifelse(round(Pct*100) >= 1 & Preferencia_1 == "Bancoppel"|
                                   round(Pct*100) >= 1 & Preferencia_2 == "Bancoppel",
                                   paste0(round(Pct*100),"%"), ""),
                    nudge_x = ifelse(after_stat(flow)=="to", 1.9, 1.101)),
                direction = "x", hjust="inward",segment.color = 'transparent') +
scale_fill_manual(values = alpha(c("#1973cd","#E9E9E9","#f7de0b"), 0.5)) +
lucify_theme_void()+
guides(fill = guide_legend(nrow = 1))+
theme(legend.position = "None")}