3

I have a simple data.frame, made into a simple alluvial map using the alluvial package. How can I edit the plot? My questions, in order of importance, are:

  1. Change the color scheme so that flows coming from the same "Admitted To" unit are the same color.
  2. Add a title
  3. Save this plot so I can later plot it into a grid with a few ggplots

Caveat: ggalluvial might be easier but unfortunately I can't install it at work, so the solution needs to use base r, ggplot, or the alluvial package.

library(alluvial)
df <- structure(list(Admitted.To = 
             c("UnitC", "UnitC", "UnitC", "UnitC", "UnitD", "UnitD", 
               "UnitD", "UnitD", "UnitE", "UnitE", "UnitE", "UnitF", 
               "UnitB", "UnitB", "UnitB", "UnitB", "UnitB", "UnitG", 
               "UnitH", "UnitA", "UnitA", "UnitA", "UnitA", "UnitA"), 
           Discharged.From = c("UnitC", "UnitD", "UnitE", "UnitA", 
                               "UnitC", "UnitD", "UnitE", "UnitA", 
                               "UnitD", "UnitE", "UnitA", "UnitF", 
                               "UnitD", "UnitI", "UnitE", "UnitB", 
                               "UnitA", "UnitG", "UnitH", "UnitC", 
                               "UnitD", "UnitI", "UnitE", "UnitA"), 
           n = c(136, 2, 1, 2, 1, 162, 2, 3, 1, 213, 1, 3, 5, 1, 7, 
                 22, 23, 1, 32, 10, 9, 39, 9, 607)), 
      .Names = c("Admitted.To", "Discharged.From", "n"), 
      row.names = c(NA, -24L),
      class = c("tbl_df", "tbl", "data.frame"))

I've been using the color code below until I figure out how to map the colors to the "Admitted To" group

set.seed(8) # for nice colors
cols <- hsv(h = sample(1:8/10), s = sample(3:8)/8, v = sample(3:8)/8) 

And my alluvial plot code:

alluvial(df[,1:2], 
     freq = 8,
     blocks = T,
     col = cols)

I've tried adding title = "SampleTitleHere" into my code but it just plots another column. I haven't found much documentation on this package.

jesstme
  • 604
  • 2
  • 10
  • 25

1 Answers1

4

Thanks for using the alluvial package. Addressing your questions one by one:

1 Change the color scheme so that flows coming from the same "Admitted.To" unit are the same color.

E.g. like that


pal <- RColorBrewer::brewer.pal(8, "Set1") # colors to use
alluvial(
  f[,1:2], 
  freq = 8,
  blocks = T,
  col = k[ match(f$Admitted.To, unique(f$Admitted.To))    ]
)
  1. Add a title

Perhaps we will add a title or main argument. Meanwhile use mtext() to add a "margin text" on top


pal <- RColorBrewer::brewer.pal(8, "Set1")
alluvial(
  f[,1:2], 
  freq = 8,
  blocks = T,
  col = k[ match(f$Admitted.To, unique(f$Admitted.To))    ]
)
mtext("A title", 3, line=3, font=2)

If your title spans more than one line or you'd rather have larger margin or space between the title and the plot you can (a) make the margin larger by using a larger number of the third element of a vector you pass to mar argument; (b) mess with line argument to mtext() to adjust how far away from the plot the title should appear.

  1. Save this plot so I can later plot it into a grid with a few ggplots

I dont have a quick answer for that. If you need to mix it with other ggplot-based figures using ggalluvial would be a better choice. See below how you might get it to work.

Caveat: ggalluvial might be easier but unfortunately I can't install it at work, so the solution needs to use base r, ggplot, or the alluvial package.

You should be able to install and use any R package (like ggalluvial) even if you are not an administrator of your system. You only need to install them somewhere you have a permission to write files. This can be even the folder you keep your analysis. See e.g. https://csg.sph.umich.edu/docs/R/localpackages.html or http://www.stat.osu.edu/computer-support/mathstatistics-packages/installing-r-libraries-locally-your-home-directory, or Google for "R user library tree".

Michał
  • 2,755
  • 1
  • 17
  • 20
  • I was able to install ggalluvial but it wouldn't run on my outdated version of R, and I didn't have IT's permission to use R at all, much less update it! Luckily I've since found an ally in IT who changed my permissions :) Your solution for the colors & title worked well, regardless. Thanks again! And yes, a title as well as subtitle argument would be greatly appreciated. – jesstme Apr 20 '17 at 23:21
  • 1
    @jesstme `title` argument is now available if you install dev version from Github – Michał Apr 26 '17 at 18:22