user969113
user969113

Reputation: 2429

ggplot2 facet_grid arrange panels

the following example creates a ggplot with the 4 panels "A", "B", "C", "D" in one row.

I figured out how to plot these 4 panels in one column. However, what still remains a mystery is how to arrange the 4 panels so that "A" and "B" are in the first row and "C" and "D" are put in a separate (second) row?

Here's my code:

df <- data.frame(
x = rep(rep(1:10, each=10), 2),
y = rep(rep(1:10, 20), 2),
grid = rep(LETTERS[1:4], each=100)
)

ggplot(df, aes(x = x, y = y)) +
geom_point() +
facet_grid(. ~ grid, scales = "free")

Upvotes: 13

Views: 13429

Answers (1)

Andrie
Andrie

Reputation: 179578

Use facet_wrap instead of facet_grid:

library(ggplot2)
ggplot(df, aes(x = x, y = y)) +
  geom_point(aes(colour=grid)) +
  facet_wrap(~ grid, scales = "free")

enter image description here

Upvotes: 16

Related Questions