use ggalluvial to draw Sankey, but an error occured:Error: Aesthetics must be either length 1 or the same as the data (122): fill

I tried to use R's ggplot2 and ggalluvial to draw sankey, at first my code is

ggplot(as.data.frame(mydata_alluvia),aes(y=FDI.changes,axis1=Primary.sender,axis2=Target.state))+
   geom_alluvium(aes(fill=Episodes),width=0)+
    geom_stratum(width=1/8,fill="white",color="grey")+
    geom_text(stat="stratum",label.strata=TRUE,size=1.5,check_overlap = TRUE)+
  scale_x_discrete(limits=c("Primary sender","Target state"),expand=c(.05,.05))+
     scale_fill_brewer(type = "qual",palette="Set1")+ 
   ggtitle("Sanctions during 1970-2017")

the gragh came out, but the countries are too many to see it clearly, so then I want to color the countries by their regions, to make the cylindrical colorful rather than white, the code is:

ggplot(as.data.frame(mydata_alluvia),aes(y=FDI.changes,axis1=Primary.sender,axis2=Target.state))+
   geom_alluvium(aes(fill=Episodes),width=0)+
    geom_stratum(width=1/8,fill=factor(mydata$Target.region),color="grey")+
    geom_text(stat="stratum",label.strata=TRUE,size=1.5,check_overlap = TRUE)+
  scale_x_discrete(limits=c("Primary sender","Target state"),expand=c(.05,.05))+
     scale_fill_brewer(type = "qual",palette="Set1")+ 
   ggtitle("Sanctions during 1970-2017") 

the error cooured:
Error: Aesthetics must be either length 1 or the same as the data (122): fill

I think I wrote the wrong code of the geom_stratum(fill=) , but how to write the right one?
could you pls help me? thanks

You are plotting mydata_alluvia, but using mydata in the geom_stratum. Is that by design? The error message states that mydata$Target.region does not have the same number of rows as mydata_alluvia. Did you perhaps mean

geom_stratum(aes(fill=factor(Target.region)), width=1/8, color="grey")

Thanks very much. I tried

ggplot(as.data.frame(mydata_alluvia),aes(y=FDI.changes,axis1=Primary.sender,axis2=Target.state))+
geom_alluvium(aes(fill=Episodes),width=0)+
geom_stratum(aes(fill=factor(Target.region)),width=1/8,color="grey")+
geom_text(stat="stratum",label.strata=TRUE,size=1.5,check_overlap = TRUE)+    scale_x_discrete(limits=c("Primary sender","Target state"),expand=c(.05,.05))+
 ggtitle("Sanctions during 1970-2017")

and the error became:
Error: Continuous value supplied to discrete scale

my data is like this

 head(mydata_alluvia)
#  Primary.sender                Target.region        Target.state  
1            AUS         East Asia and Pacific               VUT
2            USA            Sub Saharan Africa               BEN
3            USA  Middle East and North Africa               BHR
4            CAN         East Asia and Pacific               IDN
5            USA            Sub Saharan Africa               BWA
6            USA   Latin America and Caribbean               ARG
# Episodes                                     FDI.changes
1 threatened without imposition                    180.52761
2            imposed with threat                   134.42730
3 threatened without imposition                    312.48634
4 threatened without imposition                     25.15487
5 threatened without imposition                     18.46543
6            imposed with threat                    18.66822

I am confused and don't know where is the error from, and how can I make it work?

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

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