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()
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)