1

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

  • Could you confirm the logic of your data for avoidance of doubt; as I understand it the data says for Caste "SC", they comprise 17% of the population and 20.9% of the convicts? It would help make your question reproducible if you added the data as a data frame object eg `data <- data.frame(...)` – Peter Jul 11 '20 at 10:21

2 Answers2

4

This is just for fun - I've taken Peter's answer (who deserves full credit and should be awarded the accepted answer) and made some modifications to get it more like the example in the question:

enter image description here

Code

library(ggalluvial)
library(forcats)
library(tidyr)
library(scales)

dfl < pivot_longer(df, -Caste)

ggplot(data = dfl, aes(y = value, x = fct_rev(name), 
                       stratum = Caste, alluvium = Caste, fill = Caste)) +
  geom_flow(colour = "black", size = 3, linetype = 1, alpha = 1)+
  geom_flow(fill = "black", size = 3, linetype = 1, alpha = 0.5)+
  geom_stratum(colour = "black", size = 3)+
  geom_text(stat = "stratum", infer.label = TRUE, size = 5, 
            nudge_x = -0.2, aes(alpha = fct_rev(name)), color = "white") +
  geom_text(aes(label = percent(value)), stat = "stratum", size = 5, color = "white") +
  annotate(geom = "text", x = 0.7, y = 0.5, label = "Total population", 
           size = 10, color = "white") +
  annotate(geom = "text", x = 2.3, y = 0.5, label = "Convicts", 
           size = 10, color = "white") +
  coord_flip() +
  scale_alpha_manual(values = c(1, 0)) +
  scale_fill_manual(values = rev(c("#bbbacc", "#9d9caa", "#767583", "#585865", "#40404a"))) +
  labs(title = "Convict rate with total population",
       x = NULL,
       y = NULL)+
  theme_void() +
  theme(legend.position = "none", 
        plot.background = element_rect(fill = "black"),
        axis.ticks = element_blank(),
        axis.text = element_blank(),
        panel.background = element_blank(),
        panel.spacing = margin(0, 0, 0, 0))
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
2

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)

Peter
  • 11,500
  • 5
  • 21
  • 31