gpt4 book ai didi

r - facet_grid 中的多行

转载 作者:行者123 更新时间:2023-12-05 00:50:27 25 4
gpt4 key购买 nike

我有一个大致如下所示的数据集:

names = tibble(NAME_2=c("Location1","Location2","Location3","Location4"))
dates = tibble(date = seq(as.Date("2015-01-01"), as.Date("2016-12-31"), by="days"))
types = tibble(type = c("comment","post"))
df <- merge(names,dates)
df <- merge(df, types)
zero <- seq(from=0, to=200, by=1)
df$n <- sample(zero, size=nrow(df), replace=TRUE)

这会产生一个像这样的分面图:
ggplot(data = df, aes(x = date, y = n)) +
geom_line() +
facet_grid(type ~ NAME_2, scale = "free_y")

Facet plot

是否有可能获得类似 ncol=2 的行为?在 facet_wrap以便 Location3 和 Location4 出现在 Location1 和 Location2 下方?实际上,我有大约 12 个位置,这使得在一页上打印并保持清晰是不可能的。

最佳答案

您可以使用 grid.arrange ,如:

library(gridExtra)
grid.arrange(
ggplot(data = df[df$NAME_2 %in% c('Location1','Location2'),], aes(x = date, y = n)) +
geom_line() + xlab(NULL) +
facet_grid(type ~ NAME_2, scale = "free_y"),
ggplot(data = df[df$NAME_2 %in% c('Location3','Location4'),], aes(x = date, y = n)) +
geom_line() +
facet_grid(type ~ NAME_2, scale = "free_y"),
nrow=2)

enter image description here

如果您确定 x 轴范围从上到下排列,您可以抑制第一个图上的 x 轴刻度线/标签。

关于r - facet_grid 中的多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45611725/

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