I have created the following alluvial diagram in R
as follows:
df <- data.frame(Variable = c("X1", "X2", "X3", "X4", "X5", "X6"),
Pearson1 = c(6, 3, 2, 5, 4, 1),
Spearman1 = c(6, 5, 1, 2, 3, 4),
Kendall1 = c(6, 5, 1, 2, 3, 4),
Pearson2 = c(6, 5, 1, 2, 3, 4),
Spearman2 = c(6, 5, 1, 2, 4, 3),
Kendall2 = c(6, 5, 1, 2, 3, 4))
df$freq<-1
alluvial(df[1:7], freq=df$freq, cex = 0.7,col= "red")
How can I set some specific lines to have different col
than red? e.g. X1
from Variables
to Pearson1
, and then again from Kendall1
to Spearman2
and X3
in all states? I see I can't do that based on alluvial()
. How can I recreate the above alluvial based on another function??
CodePudding user response:
Need to rush somewhere, here already a draft of an answer - will comment more later.
library(ggalluvial)
#> Loading required package: ggplot2
library(tidyverse)
df <- data.frame(Variable = c("X1", "X2", "X3", "X4", "X5", "X6"),
Pearson1 = c(6, 3, 2, 5, 4, 1),
Spearman1 = c(6, 5, 1, 2, 3, 4),
Kendall1 = c(6, 5, 1, 2, 3, 4),
Pearson2 = c(6, 5, 1, 2, 3, 4),
Spearman2 = c(6, 5, 1, 2, 4, 3),
Kendall2 = c(6, 5, 1, 2, 3, 4))
df_long <-
df %>%
mutate(across(everything(), as.character)) %>%
rownames_to_column("ID")%>%
pivot_longer(-ID) %>%
## correct order of your x
mutate(name = factor(name, levels = names(df)),
highlight = case_when(ID == 3 ~ "blue",
ID == 1 & name %in% c("Variable", "Pearson1") ~ "orange",
TRUE ~ NA_character_))
ggplot(df_long,
aes(x = name, stratum = value, alluvium = ID, label = value, fill = highlight))
geom_flow(stat = "alluvium", lode.guidance = "frontback",
color = "darkgray")
geom_stratum()
geom_text(stat = "stratum")
scale_fill_identity()
#> Warning: Using the `size` aesthetic in this geom was deprecated in ggplot2 3.4.0.
#> ℹ Please use `linewidth` in the `default_aes` field and elsewhere instead.
Created on 2023-01-28 with reprex v2.0.2