Search code examples
rggplot2data-visualizationsankey-diagram

A similar Visulization which is done in Alluvial


I have come across this visualization and I'm trying to make a similar one.

Here is the picture!

US police shooting

It looks like a sankey (ofcourse there is no flow) so I tried doing a sankey with the similar results of it, but I ended up with nothing similar to that. I also tried doing it with Alluvial, but I end up with a different diagram. Could anyone please explain me what is particular visualization called how to do it.

I tried doing it with Alluvial, but I end up with a different diagram.

Here is the one I got with alluvial. The data is different from the example shown above.

Data:

df <- data.frame(Caste = c("SC","ST","OBC","FC"),
                 Total_population = c(0.17, 0.09, 0.52, 0.22),
                 Convicts = c(0.209, 0.137, 0.312, 0.341))

Code for alluvial:

ggplot(df,aes(y = Freq, axis1 = Details, axis2 = Caste)) +
  geom_alluvium(fill = cols ,width = 1/12) +
  geom_stratum(width = 1/12, fill = mycols, color = "grey") +
  geom_label(stat = "stratum", infer.label = TRUE) +
  scale_x_discrete(limits = c("Details", "Caste"), expand =c(.05,.05))+
  scale_fill_brewer(type = "qual", palette = "Set1") +
  ggtitle("Convict rate with total population")

This is the alluvial I have achieved

This is the alluvial I have achieved.

This is not what I want to achieve.

Things I need to know,

  1. how to make an exact version of this visualization.
  2. How to do it with only the results of the data I have, not the entire data table.

Thanks


Solution

  • This should get you started:

    library(ggalluvial)
    library(forcats)
    library(tidyr)
    library(dplyr)
    library(scales)
    
    df1 <- 
      df %>% 
      pivot_longer(-Caste)
    
    
    ggplot(data = df1, aes(y = value, x = fct_rev(name), stratum = Caste, alluvium = Caste, fill = Caste)) +
      geom_flow(width = 1/5, colour = "black")+
      geom_stratum(width = 1/5)+
      geom_text(data = filter(df1, name == "Total_population"), stat = "stratum", infer.label = TRUE, size = 5, nudge_x = -0.2) +
      geom_text(aes(label = percent(value)), stat = "stratum", size = 5, colour = "white") +
      annotate(geom = "text", x = 0.7, y = 0.5, label = "Total population", size = 7)+
      annotate(geom = "text", x = 2.2, y = 0.5, label = "Convicts", size = 7)+
      coord_flip() +
      labs(title = "Convict rate with total population",
           x = NULL,
           y = NULL)+
      theme(legend.position = "none",
            axis.ticks = element_blank(),
            axis.text = element_blank(),
            panel.background = element_blank())
    

    data

    df <- data.frame(Caste = c("SC","ST","OBC","FC"),
                     Total_population = c(0.17, 0.09, 0.52, 0.22),
                     Convicts = c(0.209, 0.137, 0.312, 0.341))
    
    

    Created on 2020-07-11 by the reprex package (v0.3.0)