1

I am trying to plot a cluster by individuals (6x3) plot using ggplot facet_wrap. However, the order of the subplots are not what I want.

My data and current plot:

df = data.frame(ID = rep(c(1:18),each=3), cluster = rep(c(1:6),each=9), 
            val = runif(54,5,8), date = rep(c(1:3),18))

ggplot(df, aes(date, val,)) + geom_bar(stat = 'identity') + 
            facet_wrap( ~ cluster*ID, nrow=3,ncol=6) + theme_bw()

enter image description here

However, as you can see, the plots are in order of the ID. What I want is to have each column as clusters (i.e. column 1 contains cluster 1 with ID 1,2,3; column 2 contains cluster 2 with ID 4,5,6 etc).

Is there anyway to do this? Please help! Thank you!

TYL
  • 1,577
  • 20
  • 33
  • Possible duplicate of [Is it possible to reorder only the facets of facet\_wrap, without reordering the underlying factor levels?](https://stackoverflow.com/questions/38834814/is-it-possible-to-reorder-only-the-facets-of-facet-wrap-without-reordering-the) – NelsonGon Jul 19 '19 at 14:35
  • Take a look at this: https://stackoverflow.com/questions/35859487/r-ggplot2-facet-wrap-reordering-subfigure-and-set-different-colors-for-each-id-l?rq=1 – NelsonGon Jul 19 '19 at 14:36
  • 1
    Most of these posts deal with one variable facet_wrap but what I have here is an interaction of 2 variables – TYL Jul 19 '19 at 14:40
  • OK, can edit to highlight that. – NelsonGon Jul 19 '19 at 14:41

1 Answers1

1

I think you just need to indicate the direction with dir

ggplot(df, aes(date, val,)) + geom_bar(stat = 'identity') +
            facet_wrap(~cluster * ID, nrow = 3, ncol = 6, dir = "v") + theme_bw()

enter image description here

Matt
  • 2,947
  • 1
  • 9
  • 21