Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying the `stratum` and `alluvium` parameters without attaching ggalluvial

Tags:

r

ggplot2

I use ggalluvial with ggplot2, though, I'd like to be able to generate the same plot without attaching ggalluvial but only specify its use with ggalluvial::. If it is not attached, I get the following error: Error: Can't find stat called "stratum".

d <- data.frame(

    status = rep(c("state1","state2","state3"), rep(90, times=3)),
    cellIndex = rep(seq_len(90), times=3),
    cellCategory = c(rep(letters[seq_len(3)], each=30),
                     rep(letters[c(2,3,1)], each=30), 
                     rep(letters[c(3,1,2)], each=30))
)


ggplot2::ggplot(data=d, ggplot2::aes(x=status, stratum=cellCategory, alluvium=cellIndex,
                fill=cellCategory, label=cellCategory)) +

    ggalluvial::geom_flow(stat="alluvium", lode.guidance="rightleft", color="darkgray") +

    ggalluvial::geom_stratum() +

    ggplot2::geom_text(stat="stratum", size=3)
like image 624
LostIT Avatar asked Dec 15 '18 22:12

LostIT


1 Answers

This was a tough one---digging into the code for ggplot2, the stat argument pastes the string you give and then looks for that object (in this case "StatStratum") in the environment you're in. Because you don't want to load the package, it won't be able to find it (and there's no way to change the argument itself).

Answer

So you need to save that object from the ggalluvial package like so:

StatStratum <- ggalluvial::StatStratum

Then leave the rest of your code as is.

like image 167
twedl Avatar answered Oct 05 '22 22:10

twedl