ggplot2 facet_grid安排面板

use*_*113 11 layout r ggplot2

以下示例ggplot在一行中创建一个带有4个面板"A","B","C","D"的面板.

我想出了如何在一列中绘制这4个面板.然而,仍然是一个谜团是如何安排4个面板,使"A"和"B"在第一行,"C"和"D"放在一个单独的(第二)行?

这是我的代码:

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")
Run Code Online (Sandbox Code Playgroud)

And*_*rie 15

使用facet_wrap而不是facet_grid:

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

在此输入图像描述