7

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

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")

刻面图

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

4

1 回答 1

5

您可以使用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)

在此处输入图像描述

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

于 2017-08-10T13:16:05.650 回答