ggalluvial with one value in axis1

46 Views Asked by At

I am supposed to make a ggalluvial with the following dataframe :

df <- data.frame(departure = c("Paris"), arrival = c("Nantes","Caen","Nice"), value = c(0.5625, 0.312, 0.125))

When I do it, as I just have one element in axis 1, it doesn't really make the 'alluvial effect'. I obtain the following result :

df %>% 
  group_by(departure) %>% 
  ggplot(aes(y = value*100, axis1 = departure, axis2 = arrival)) +
  geom_alluvium(aes(fill = arrival), width = 1/12) +
  geom_stratum(width = 1/12, fill = "white", color = "grey") +
  geom_label(stat = "stratum", aes(label = after_stat(stratum))) +
  scale_x_discrete(limits = c("departure", "arrival"), expand = c(.05, .05)) +
  scale_fill_brewer(type = "qual", palette = "Set1") +
  theme(legend.position = "none")

enter image description here

I tried to cheat by adding a fictive row in order to get the expected alluvial effect.

df <- rbind(df, c("-", "Nantes", 0.5625))
df <- rbind(df, c("-", "Caen", 0.262))
df$value <- as.numeric(df$value)

enter image description here

Is it possible to make an aesthetic alluvial plot with one axis1 ? Thank you in advance.

1

There are 1 best solutions below

0
Allan Cameron On BEST ANSWER

Your data structure does not lend itself well to an alluvial plot. If this is an excercise to create an alluvial plot from the data, then you have succeeded; this is what an alluvial plot with the given data should look like. If you want something that is more in keeping with the look of an alluvial plot, you could try making a graph with more separation between the left-hand labels. This requires a bit of calculation to work out the positions

library(tidygraph)
library(ggraph)

df %>%
  mutate(departure = paste(departure, arrival)) %>%
  as_tbl_graph() %>%
  mutate(label = ifelse(grepl("Paris", name), "Paris", name),
         xpos = ifelse(label == "Paris", 1, 2),
         ypos = ifelse(label == "Paris", cumsum(df$value) - df$value/2, 
                       cumsum(df$value + 0.5) - 1)) %>%
  ggraph(layout = "manual", x = ypos, y = xpos) +
  geom_edge_diagonal(aes(width = value, color = factor(to)), alpha = 0.3) +
  geom_node_tile(aes(width = rep(df$value, 2)), height = 0.1, fill = "white") +
  geom_node_text(aes(label = label), angle = 90) +
  annotate("rect", xmin = 0, xmax = 1, ymin = 0.95, ymax = 1.05, 
           fill = "white", color = "black", linewidth = 0.2) +
  annotate("text", x = 0.5, y = 1, angle = 90, label = "Paris") +
  scale_edge_width_continuous(range = c(0, 118), limits = c(0, 1)) +
  theme(legend.position = "none") +
  coord_flip()

enter image description here