2

I'm trying to create a graph in ggplot using the facet_wrap argument.

However, I don't want the label over every small graph, I want a label only on top of the graph and on the left.

For example in the graph below, I would like to have at the top labels SI2, SI1, WS2, and on the left the labels D, E, F.

library(tidyverse)

df <- diamonds %>% 
  select(cut, color, clarity, price) %>% 
  filter(clarity %in% c("SI2", "SI1", "VVS2")) %>% 
  filter(color %in% c("D", "E", "F"))


df %>% 
  ggplot(aes(cut, price)) + 
  geom_boxplot() +
  facet_wrap(~color + clarity)

enter image description here

DJV
  • 4,743
  • 3
  • 19
  • 34
  • 2
    Use `facet_grid` : `ggplot(df, aes(cut, price)) + geom_boxplot() + facet_grid(rows = vars(color) , cols = vars(clarity), switch = "y")` – markus Dec 31 '19 at 09:23
  • Thank you! add it as an answer so I'll be able to up-vote. In addition, two questions:1. Is there a way that the label will be on the left of the y-axis?. 2. What is the difference between facet_grid and facet_wrap? – DJV Dec 31 '19 at 09:34

1 Answers1

4

The answer is a merge between @markus's comment and the argument strip.placement = "outside" in theme().

df %>% 
  ggplot(aes(cut, price)) + 
  geom_boxplot() +
  facet_grid(rows = vars(color) , 
               cols = vars(clarity), 
             switch = "y") + 
  theme(strip.placement = "outside")

enter image description here

Info about the differences between facet_wrap and facet_grid can be found here.

DJV
  • 4,743
  • 3
  • 19
  • 34