mbojan / alluvial

Alluvial diagrams
Other
139 stars 26 forks source link

Legend #20

Closed CarolineXGao closed 8 years ago

CarolineXGao commented 8 years ago

Hi,

I am trying this plot, it is fantastic for my purpose. Is it possible to add a Legend ?

Thanks a lot. Caroline

mbojan commented 8 years ago

At this moment it is possible to legends, in the same way as to any base graphics plots, using legend. However, it may be not that easy as alluvial usually covers the whole plotting region with reduced margins. One trick/hack is to create two plotting regions e.g. with layout and use one for the legend and the other for the alluvial diagram. For example:

h <- 0.12
layout(matrix(1:2, 2, 1), heights=c( h, 1-h ) )
op <- par(mar=rep(0,4))
plot(0:1, type="n", axes=FALSE, ann=FALSE)
legend("top", pch=15, 
       bty="n",
       ncol=2,
       pt.cex=2,
       plot=TRUE,
       col=c("red", "grey"),
       legend=c("No", "Yes"),
       title="Survived")
par(op)
alluvial(tit3d[,1:3], freq=tit3d$Freq, alpha=1, xw=0.2,
         col=ifelse( tit3d$Survived == "No", "red", "gray"),
         layer = tit3d$Sex != "Female",
         border="white")

Depending on

  1. the size of the plotting device (rectangle panel of RStudio or usually a square otherwise)
  2. the size of the legend

you might want to tweak the value of h which is the relative height of the part containing the legend. It should be between 0 and 1, probably small.

Hopefully adding legends will be easier with the upcoming ggplot version of alluvial on which we are working...