ggplot2分面网格排列面板。

13
下面的示例创建了一个具有四个面板"A"、"B"、"C"、"D"在一行中的 ggplot
我已经找到了如何在一列中绘制这四个面板。然而,仍然有一个谜团,那就是如何安排这四个面板,使得"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")

6
也许可以使用 facet_wrap 并设置 ncol = 2 - joran
太好了!我不知道还有这个选项,之前尝试使用grid1和grid2列并将不同的值设置为NA来解决它,但是没有成功 :-) 然而,facet_wrap起作用了!谢谢! - user969113
1个回答

16

使用facet_wrap代替facet_grid

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

输入图像描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接