knst4444
knst4444

Reputation: 88

facet_wrap with different factor levels in each subplot

Is there any possibility to remove "f e d" levels from the first subplot and "c b a" from the second subplot using facet_wrap? In other words I want to have only "c b a" columns on first subplot and only "f e d" columns on the 2nd.

Example data.frame:

df <- data.frame(x = letters[1:6], gr = c(rep("kk", 3), rep("yy", 3)), v = 10:15) 

Plot call:

ggplot(data = df, aes(x = x, y = v)) +
  geom_col() +
  coord_flip() +
  facet_wrap(~gr, nrow = 2)

Current result

Upvotes: 0

Views: 392

Answers (1)

Dave
Dave

Reputation: 980

To avoid a fixed scale for your y-axis, simply add scales = "free_y" to your facet_wrap() command.

ggplot(data = df, aes(x = x, y = v)) +
  geom_col() +
  coord_flip() +
  facet_wrap(~gr, nrow = 2, scales = "free_y")

Upvotes: 1

Related Questions