Reputation: 174
I'm producing a graphic using ggalluvial and want to group my variables on the left.
library(tidyverse)
library(ggalluvial)
data <- tibble(left = c("a","b", "c", "c", "d", "d"),
right = c("e", "e", "e", "f", "e", "f"),
values = c(1,2,3,2,3,2),
group = c("Group 3", "Group 2", "Group 1", "Group 1", "Group 2", "Group 2"))
ggplot(data,
aes(y = values, axis1 = left, axis2 = right)) +
geom_alluvium(aes(fill = group), width = 1/12) +
geom_stratum(width = 1/12, fill = "black", color = "grey") +
geom_text(stat = "stratum", infer.label = TRUE,
nudge_x = -.1, fontface = "bold") +
scale_fill_brewer(type = "qual", palette = "Set1")
This produces the figure below:
I have coloured the flows based upon their group. But I want to group the left hand side based upon their group. I.e. the new order should be c, b, d, a from top to bottom - rather than alphabetical as default.
I'd be very thankful for help getting to a solution.
Thanks.
Upvotes: 1
Views: 186
Reputation: 24252
You need to define left
as factor with levels in the required order c("c", "b", "d", "a")
.
data$left <- factor(data$left, levels=c("c", "b", "d", "a"))
ggplot(data,
aes(y = values, axis1 = left, axis2 = right)) +
geom_alluvium(aes(fill = group), width = 1/12) +
geom_stratum(width = 1/12, fill = "black", color = "grey") +
geom_text(stat = "stratum", infer.label = TRUE,
nudge_x = -.1, fontface = "bold") +
scale_fill_brewer(type = "qual", palette = "Set1")
Upvotes: 1