y axis on ggalluvial sankey diagram

Hello,

I have created a saneky diagram using the ggalluvial package. I would like the y-axis to have a scale from 0 - x number of children but haven't been able to work out how to do this. Any help would be much appreciated.

I have created an example dataset (below) and this is the code I have used to generate the plot (image below):
#load libraries

library(ggalluvial)

#load data and check uploaded correctly

data <- read.csv("file_location/gal_example.csv", header = T)
head(data)

#change order of levels

levels(data$result)
data$result<-factor(data$result, level = c('3','2','1','0'), order = TRUE)
levels(data$result)

#create plot

plot <- ggplot(data, aes(x = as.factor(year), stratum = result, alluvium = as.factor(id), fill = result, label = result)) + geom_flow() + geom_stratum(alpha = .5) + theme(legend.position = "none") + theme_minimal() + scale_x_discrete(name = "Year") + scale_y_discrete(name = "Number of Children")

plot

image

This is the data contained in the gal_example.csv file:
(so for example I have 26 children here and would be looking for a scale of 0 - 30 on the y axis of the above graph).

id result year
A 3 1
A 2 2
A 3 3
B 3 1
B 3 2
B 3 3
C 1 1
C 2 2
C 3 3
D 1 1
D 2 2
D 3 3
E 1 1
E 2 2
E 3 3
F 1 1
F 1 2
F 2 3
G 3 1
G 1 2
G 0 3
H 0 1
H 1 2
H 3 3
I 1 1
I 1 2
I 1 3
J 1 1
J 1 2
J 1 3
K 2 1
K 2 2
K 1 3
L 1 1
L 2 2
L 3 3
M 1 1
M 2 2
M 3 3
N 2 1
N 2 2
N 3 3
O 0 1
O 1 2
O 2 3
P 3 1
P 0 2
Q 2 1
R 2 3
S 2 2
T 1 1
U 3 1
U 3 2
V 0 1
V 2 2
W 1 2
W 1 3
X 2 1
X 3 2
X 3 3
Y 1 1
Y 2 2
Z 1 2
Z 3 3

Hi,
If I'm interpreting your question correctly, I think your trouble is due to how you rename the x and y axes. Instead of doing scale_x_discrete(name = "Year") and scale_y_discrete(name = "Number of Children"), try using xlab() and ylab() instead. So like this:

ggplot(data, aes(x = as.factor(year), stratum = result, 
                 alluvium = as.factor(id), fill = result, 
                 label = result)) + 
  geom_flow() + 
  geom_stratum(alpha = .5) + 
  theme(legend.position = "none") + 
  theme_minimal() +
  xlab("Year") +
  ylab("Number of Children")

Does that help?

1 Like

Thanks so much! Works a treat and a very simple fix, I think I just overthought it. :grinning:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.