gpt4 book ai didi

r - ggplot2 facet_grid 排列面板

转载 作者:行者123 更新时间:2023-12-04 01:57:23 45 4
gpt4 key购买 nike

以下示例创建了一个 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")

最佳答案

使用 facet_wrap而不是 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

关于r - ggplot2 facet_grid 排列面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12197437/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com