1

I'm trying to create an alluvial plot in ggplot2 with the ggalluvial package. Here are the two helpful websites I've been using to create the plot: site 1 and site 2.

My goal is to use the same code found on site 1 but apply it to my own df, as shown below. However, everytime I try to do that I get the following error:

Error in data.frame(first_row, curve_data): arguments imply differing number of rows: 1, 12

This is my df:

t1 <- c("college", "college", "college", "university", "university")
t2 <- c("law", "law", "business", "business", "business")
t3 <- c("public", "private", "private", "private", "public")
t4 <- c(2,4,6,8,10)

df <- data.frame(cbind(t1,t2,t3,t4))
names(df) <- c("Undergrad", "Grad_School", "Sector", "Freq")

This is the original code, as copied from site 1:

library(ggalluvial)
vaccinations
ggplot(data = vaccinations,
       aes(axis1 = survey,
           axis2 = response,
           axis3 = survey,
           y = freq)) +
    geom_alluvium(aes(fill = response)) +
    geom_stratum() +
    geom_text(stat = "stratum",
              aes(label = after_stat(stratum))) +
    scale_x_discrete(limits = c("Survey", "Response"),
                     expand = c(0.15, 0.05)) +
    theme_void() 

This is my code with my df substituted in.

ggplot(data = df,
       aes(axis1 = Undergrad,
           axis2 = Grad_School,
           y = Freq)) +
    geom_alluvium(aes(fill = Sector)) +
    geom_stratum() +
    geom_text(stat = "stratum",
              aes(label = after_stat(stratum))) +
    scale_x_discrete(limits = c("Undergrad", "Grad_School"),
                     expand = c(0.15, 0.05)) +
    theme_void()
Sly
  • 25
  • 4
  • Please share the exact code you're currently trying so we can help. How did you modify the code from the example to use your data? – Dan Adams Jul 18 '22 at 04:46
  • @DanAdams, thanks for your comment. I edited the question to show how I modified the code. – Sly Jul 18 '22 at 05:23
  • Your code works fine for me. If you clear your environment and start again, does it still give an error? – Dan Adams Jul 18 '22 at 05:25
  • Unfortunately, I'm still getting the same error. Your answer below worked perfectly. I'm still trying to understand why my code didn't work. It seems very similar to your answer. – Sly Jul 18 '22 at 05:40
  • Just updated the answer. Compare `str(df)` to `str(d)` and you'll see that yours has `chr` for `Freq` which is why it won't work. – Dan Adams Jul 18 '22 at 05:42

1 Answers1

1

When you cbind() your vectors, you create a matrix which (unlike a data.frame) cannot contain different data types. Therefore, the Freq gets converted to a character. If you just directly create the data.frame without cbind() it works fine.

library(tidyverse)
library(ggalluvial)

t1 <- c("college", "college", "college", "university", "university")
t2 <- c("law", "law", "business", "business", "business")
t3 <- c("public", "private", "private", "private", "public")
t4 <- c(2,4,6,8,10)

df <- data.frame(t1,t2,t3,t4)
names(df) <- c("Undergrad", "Grad_School", "Sector", "Freq")

ggplot(data = df, 
       aes(axis1 = Undergrad,
             axis2 = Grad_School,
             y = Freq)) +
  geom_alluvium(aes(fill = Sector)) +
  geom_stratum() +
  geom_text(stat = "stratum",
            aes(label = after_stat(stratum))) +
  scale_x_discrete(limits = c("Undergrad", "Grad_School"),
                   expand = c(0.15, 0.05)) +
  theme_void()

Created on 2022-07-18 by the reprex package (v2.0.1)

Dan Adams
  • 4,971
  • 9
  • 28