3

Using facet_grid(), how can I place the group names, i.e. the grey strips, on the top of the plots when graphing the plots stacked vertically in a single column?

From what I have tried, when stacking the plots vertically, using facet_grid(), you cannot place the strip on the top or bottom of the graph; you can only place the strip on the right hand side or the left hand side of the plots using the switch = c("y", "x", "both") argument.

I am aware that facet_wrap() (and facet_col() from ggforce) can place the strip anywhere using the strip.position = c("top", "bottom", "left", "right") argument, irrespective of how the graphs are being plotted: vertically or horizontally. I used facet_wrap() to produce Fig.1 below (code is in the Reproducible Code section).

However, I would like to be able to produce the plot below (with the strip on "top") using facet_grid() not facet_wrap.

Fig.1: I want to be able to plot this using facet_grid()

Fig.1

Ideally, it would be great if the switch argument could be extended or the strip.position argument could be added to facet_grid(), to be able to place the strip on top. I tried to do it myself by trying to change the source code from facet_grid() and facet_wrap() functions, but could not figure it out.

The reason I am not simply using facet_wrap() function is because it unfortunately does not have the space="free" argument which is available in facet_grid() and is essential in my application.

Below is a reproducible example taken from, with slight modifications, the code of Brandon Bertelsen answer's here:

Reproducible Code:

The Data:

dat <- data.frame(value=runif(30)*10,
                  grouping=c(rep("Group 1",10),
                             rep("Group 2",10),
                             rep("Group 3",10)),
                  letters=rep(LETTERS[1:10], 3)

Using facet_wrap to get strip on top when plotting graphs vertically:

ggplot(dat, aes(letters,value, label = letters)) + 
  geom_bar(stat="identity") + 
  facet_wrap(grouping~., dir = "v")

Produces Fig.1:

Fig.1:

Using facet_grid with the switch = "y" places the strip to the left hand side:

ggplot(dat, aes(letters,value, label = letters)) + 
  geom_bar(stat="identity") + 
  facet_grid(grouping~., switch = "y")

Produces Fig.2:

Fig.2

Using facet_grid with the switch = "x" places the strip to the right hand side:

ggplot(dat, aes(letters,value, label = letters)) + 
     geom_bar(stat="identity") + 
     facet_grid(grouping~., switch = "x")

Produces Fig.3:

Fig.3

With no other options remaining I turn here for help.

  • If you leave `scales = 'free'` out and just do `facet_wrap(grouping~., dir = "v")` you should get what you want. – MarBlo Jun 13 '20 at 06:39
  • Hi @MarBlo thank you for your response. However, this is not my question at all. Perhaps my question was not very clear and I have modified my post. I need to use facet_grid() not facet_wrapt() to produce the plot; scales = 'free' does not help. Is there some way using facet_grid() to produce Fig.1. Thanking you – Faizan Khalid Mohsin Jun 15 '20 at 02:38
  • This sounds like a version of the [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What you really want is to have free space on the y-axis while keeping strip titles at the top of each plot, no? Or is there some other reason why you absolutely need to use `facet_grid`? For example, `facet_col` from the `ggforce` package may serve your needs. See discussion [here](https://github.com/tidyverse/ggplot2/issues/2933). – Z.Lin Jun 25 '20 at 02:59
  • Thank you @Z.Lin for your comment. Indeed, I do absolutely need to use facet_grid(). I tried facet_col() like facet_wrap() and it does not work for me. I will also change my post and mention facet_col() there for other people. The thing is my visualization is a bit more complex than the toy example I have created for stackoverflow. I will try to convert my code to something reproducible and post how facet_grid looks vs facet_col and facet_wrap look for my visualization. But nonetheless, yes, I do need to use facet_grid() and need to somehow place the strip on the "top" of the plot. – Faizan Khalid Mohsin Jul 03 '20 at 17:44

1 Answers1

0

Is that what you want? Then just leave sales = 'free' out.

library(ggplot2)

dat <- data.frame(value=runif(26)*10,
                  grouping=c(rep("Group 1",10),
                             rep("Group 2",10),
                             rep("Group 3",6)),
                  letters=LETTERS[1:26])

ggplot(dat, aes(letters,value, label = letters)) + 
  geom_bar(stat="identity") + 
  facet_wrap(grouping~., dir = "v")

Created on 2020-06-13 by the reprex package (v0.3.0)

MarBlo
  • 4,195
  • 1
  • 13
  • 27
  • Hi @MarBlo , No this not what I want. My question is how can the above graph be created using facet_grid() with the Group 1, Group 2, Group 3 strips on the top of the plots not using facet_wrap(). Thanking you – Faizan Khalid Mohsin Jun 15 '20 at 02:47