0

I am trying to make a certain alluvial plot with different widths specified in different columns. Let me try to explain it by drawing it, as I am not sure how to do this in ggalluvial.

Notice that the width of the flow from the Male box represents 3 units, while it represents 10 in box 3. Is it possible to create such graphs in ggalluvial? Or how can one construct such a graph in R?

I haven't drawn the other flows just to focus on the flow from male to 3.

Alluvial

I would hereby would like to present some data to create such a graph:

test_data <- data.table(`2018 - Gender` = c("Male", "Female", "Female", "Male"),
           `2018 - Value`  = c(10, 20, 30, 20),
           `2019 - Gender`  = c("Male", "Female", "Male", "Female"),
           `2019 - Value`  = c(20, 30, 10, 10)
           )

Notice that the column names determine the "columns" in the graphs (i.e. the x-axis). While the Gender variable determines the blocks. The value from 2018 is the starting width, while the value from 2019 is the ending width of the strata.

As some have pointed out that I need to put more focus on my question. The question is how to make flow graphs with different starting and ending width.

Snowflake
  • 2,869
  • 3
  • 22
  • 44
  • Snowflake, I think you are risking downvotes and closure without some data that people can work with (although I haven't personally voted at all...yet!) – Allan Cameron Sep 17 '20 at 16:41
  • Not sure this is possible due to the design of ggalluvial. Strata have the same size, so this would be a bit confusing – csgroen Sep 17 '20 at 16:45
  • This might just require long-form rather than wide-form data. Do [this question](https://stackoverflow.com/q/43053375/4556798) and its answer address it? – Cory Brunson Sep 17 '20 at 20:21

1 Answers1

0

Perhaps the following dummy example gives you a better idea. Please check if your data is in alluvial form with is_alluvia_form(), before you plot it.

c <- c(LETTERS[1:4], LETTERS[2:6], LETTERS[3:7], LETTERS[3:8])
t <- c(rep("Fortnight 1",4), rep("Fortnight 2",5), rep("Fortnight 3",5), rep("Fortnight 4",6))
s <- c(rep(c("Female","Male"),10))
ag <- c(2,3,4,6,11,13)
f <- rnorm(20,20,99)
df <- data.frame(Timeframe=t,Code=c,Sex=s,Freq=round(abs(f))) %>% mutate(Organization=ifelse((row_number() %in% ag), "Agencia2","Agencia1" ))

alluvial_data <- as.data.frame(df %>%select(Organization, Timeframe, Code, Freq, Sex))
alluvial_data <- alluvial_data %>% mutate(id = row_number())

#Remove duplicates
alluvial_data <- alluvial_data %>% 
  distinct(Organization, Timeframe, Code, Sex, .keep_all = TRUE)

#levels(alluvial_data$Timeframe)

# Convert Timeframe to Factor - Categorical Variable
alluvial_data$Timeframe <-as.factor(alluvial_data$Timeframe)

# Convert Code to String
alluvial_data$Code <-as.character(alluvial_data$Code)

library(RColorBrewer)

# Define the number of colors you want
nb.cols <- 10
mycolors <- colorRampPalette(brewer.pal(8, "Set2"))(nb.cols)
mycolor2 <- colorRampPalette(brewer.pal(2, "Set2"))(nb.cols)

# Chart
ggplot(alluvial_data,
       aes(y = Freq, axis1 = Organization, axis2 = Timeframe, axis3 = Code,fill=Sex)) +
  #scale_fill_brewer(type = "qual", palette = "Set2") +
  scale_x_discrete(limits=c("Organization","Timeframe","Code"), expand=c(0.05,0.05)) +
  scale_fill_manual(values = mycolors) +
  geom_flow(stat = "alluvium", lode.guidance = "frontback" #, color="grey"
             ) +
  geom_stratum(width = 1/4, fill = "cyan", color = "grey") +
  geom_label(stat = "stratum", aes(label = after_stat(stratum))) +
  theme(legend.position = "bottom") +
  ggtitle("Organizations") +
  guides(fill=guide_legend(override.aes = list(color=mycolors[1:2])))+
  labs(fill=NULL)

output

YBS
  • 19,324
  • 2
  • 9
  • 27