如何为我的数据类型使用分面网格

How to Use Facet Grid For My Type of Data

我有这个 12 时间序列生成的数据,我使用 ggplot2 将每个数据绘制为时间图。我想将 12 个图安排成 1 个图,使用 facet_grid 使其成为 3D,其中列名称为 colname <- c("0.8", "0.9", "0.95"),行名称为 rowname <- c("sd = 1", "sd = 3", "sd = 5", "sd = 10"),并且安排将是 4 by 3 布局。

## simulate ARIMA(1, 0, 0)
set.seed(289805)
x1 <- arima.sim(n = 10, model = list(ar = 0.8, order = c(1, 0, 0)), sd = 1)
set.seed(671086)
x2 <- arima.sim(n = 10, model = list(ar = 0.9, order = c(1, 0, 0)), sd = 1)
set.seed(799837)
x3 <- arima.sim(n = 10, model = list(ar = 0.95, order = c(1, 0, 0)), sd = 1)
set.seed(289805)
x4 <- arima.sim(n = 10, model = list(ar = 0.8, order = c(1, 0, 0)), sd = 3)
set.seed(671086)
x5 <- arima.sim(n = 10, model = list(ar = 0.9, order = c(1, 0, 0)), sd = 3)
set.seed(799837)
x6 <- arima.sim(n = 10, model = list(ar = 0.95, order = c(1, 0, 0)), sd = 3)
set.seed(289805)
x7 <- arima.sim(n = 10, model = list(ar = 0.8, order = c(1, 0, 0)), sd = 5)
set.seed(671086)
x8 <- arima.sim(n = 10, model = list(ar = 0.9, order = c(1, 0, 0)), sd = 5)
set.seed(799837)
x9 <- arima.sim(n = 10, model = list(ar = 0.95, order = c(1, 0, 0)), sd = 5)
set.seed(289805)
x10 <- arima.sim(n = 10, model = list(ar = 0.8, order = c(1, 0, 0)), sd = 10)
set.seed(671086)
x11 <- arima.sim(n = 10, model = list(ar = 0.9, order = c(1, 0, 0)), sd = 10)
set.seed(799837)
x12 <- arima.sim(n = 10, model = list(ar = 0.95, order = c(1, 0, 0)), sd = 10)
xx <- 1:10

# ggplot for x1
plot1 <- ggplot2::ggplot(NULL, aes(y = x1, x = xx)) +  ggplot2::geom_line(color = "#F2AA4CFF") + ggplot2::geom_point(color = "#101820FF") + xlab('lb') + ylab('RMSE') +  ggplot2::theme_bw()+ ggplot2::scale_y_continuous(expand = c(0.0, 0.00))

# ggplot for x2
plot2 <- ggplot2::ggplot(NULL, aes(y = x2, x = xx)) +  ggplot2::geom_line(color = "#F2AA4CFF") + ggplot2::geom_point(color = "#101820FF") + xlab('lb') + ylab('RMSE') +  ggplot2::theme_bw()+ ggplot2::scale_y_continuous(expand = c(0.0, 0.00))

# ggplot for x3
plot3 <- ggplot2::ggplot(NULL, aes(y = x3, x = xx)) +  ggplot2::geom_line(color = "#F2AA4CFF") + ggplot2::geom_point(color = "#101820FF") + xlab('lb') + ylab('RMSE') +  ggplot2::theme_bw()+ ggplot2::scale_y_continuous(expand = c(0.0, 0.00))

# ggplot for x4
plot4 <- ggplot2::ggplot(NULL, aes(y = x4, x = xx)) +  ggplot2::geom_line(color = "#F2AA4CFF") + ggplot2::geom_point(color = "#101820FF") + xlab('lb') + ylab('RMSE') +  ggplot2::theme_bw()+ ggplot2::scale_y_continuous(expand = c(0.0, 0.00))

# ggplot for x5
plot5 <- ggplot2::ggplot(NULL, aes(y = x5, x = xx)) +  ggplot2::geom_line(color = "#F2AA4CFF") + ggplot2::geom_point(color = "#101820FF") + xlab('lb') + ylab('RMSE') +  ggplot2::theme_bw()+ ggplot2::scale_y_continuous(expand = c(0.0, 0.00))

# ggplot for x6
plot6 <- ggplot2::ggplot(NULL, aes(y = x6, x = xx)) +  ggplot2::geom_line(color = "#F2AA4CFF") + ggplot2::geom_point(color = "#101820FF") + xlab('lb') + ylab('RMSE') +  ggplot2::theme_bw()+ ggplot2::scale_y_continuous(expand = c(0.0, 0.00))

# ggplot for x7
plot7 <- ggplot2::ggplot(NULL, aes(y = x7, x = xx)) +  ggplot2::geom_line(color = "#F2AA4CFF") + ggplot2::geom_point(color = "#101820FF") + xlab('lb') + ylab('RMSE') +  ggplot2::theme_bw()+ ggplot2::scale_y_continuous(expand = c(0.0, 0.00))

# ggplot for x8
plot8 <- ggplot2::ggplot(NULL, aes(y = x8, x = xx)) +  ggplot2::geom_line(color = "#F2AA4CFF") + ggplot2::geom_point(color = "#101820FF") + xlab('lb') + ylab('RMSE') +  ggplot2::theme_bw()+ ggplot2::scale_y_continuous(expand = c(0.0, 0.00))

# ggplot for x9
plot9 <- ggplot2::ggplot(NULL, aes(y = x9, x = xx)) +  ggplot2::geom_line(color = "#F2AA4CFF") + ggplot2::geom_point(color = "#101820FF") + xlab('lb') + ylab('RMSE') +  ggplot2::theme_bw()+ ggplot2::scale_y_continuous(expand = c(0.0, 0.00))

# ggplot for x10
plot10 <- ggplot2::ggplot(NULL, aes(y = x10, x = xx)) +  ggplot2::geom_line(color = "#F2AA4CFF") + ggplot2::geom_point(color = "#101820FF") + xlab('lb') + ylab('RMSE') +  ggplot2::theme_bw()+ ggplot2::scale_y_continuous(expand = c(0.0, 0.00))

# ggplot for x11
plot11 <- ggplot2::ggplot(NULL, aes(y = x11, x = xx)) +  ggplot2::geom_line(color = "#F2AA4CFF") + ggplot2::geom_point(color = "#101820FF") + xlab('lb') + ylab('RMSE') +  ggplot2::theme_bw()+ ggplot2::scale_y_continuous(expand = c(0.0, 0.00))


# ggplot for x12
plot12 <- ggplot2::ggplot(NULL, aes(y = x12, x = xx)) +  ggplot2::geom_line(color = "#F2AA4CFF") + ggplot2::geom_point(color = "#101820FF") + xlab('lb') + ylab('RMSE') +  ggplot2::theme_bw()+ ggplot2::scale_y_continuous(expand = c(0.0, 0.00))


# plot in a 3 by 5 grid by using plot_layout
plot1 + plot2 + plot3 + plot4 + plot5 + plot6 + plot7 + plot8 + plot9 + plot10 + plot11 + plot12 + patchwork::plot_layout(ncol = 3, byrow = TRUE)

我想要这样

.

编辑

可能需要它的 data frame 版本

df <- data.frame(xx, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12)

为了使用 ggplot,您需要数据框。例如,解决您的问题的一种方法是:

编辑 在你指定你对布局有困难之后,这里是一种方式。当您相当手动地计算时间序列时,您需要通过将 sd/CI 信息添加到数据框中来手动规定它。然后,您可以在 facet_grid

中的公式语法中使用该信息
## This requires an empty environment
## first make a list of all objects in the environment with the pattern x[number]
## mget retrieves all those objects
## the subsetting operator is to bring it into the right order
ls_ts <- mget(ls(pattern = "x[0-9]+"))[paste0("x", 1:length(ls(pattern = "x[0-9]+")))]

newdat <-
  data.frame(
    y = unlist(lapply(ls_ts, as.data.frame)),
    x = xx, sd = rep(rep(c(1, 3, 5, 10), each = 10), each = 3),
    CI = rep(rep(c(.8, .9, .95), each = 10), 4)
  )

ggplot(newdat, aes(x, y)) +
  geom_line() +
  geom_point() +
  labs(x = "lb", y = "RMSE") +
  theme_bw() +
  scale_y_continuous(expand = c(0, 0)) +
  facet_grid(sd ~ CI, scales = "free_y")

reprex package (v2.0.1)

于 2021-11-07 创建