如何为我的数据类型使用 Facet Grid

Posted

技术标签:

【中文标题】如何为我的数据类型使用 Facet Grid【英文标题】:How to Use Facet Grid For My Type of Data 【发布时间】:2021-12-20 02:26:37 【问题描述】:

我有这个 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)

【问题讨论】:

前3个系列用sd = 1模拟,第4-6个用sd = 3模拟,第7-9个系列用sd = 5模拟,第10-12个系列用sd = 5模拟sd = 10. 因为我想要 3 系列的 4 行,所以第一行标记为sd =1,第二行标记为sd = 3,第三行标记为sd = 5,第四行标记为@987654342 @. 这能回答你的问题吗? Why is the Facet_Grid Output Different from Its Data 【参考方案1】:

为了使用 ggplot,您需要数据框。处理您的问题的一种方法可能是:

edit 在您指定您对布局感到困惑之后,这里是一种方式。当您相当手动地计算时间序列时,您需要通过将 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 创建于 2021-11-07 (v2.0.1)

【讨论】:

4 by 3 布局更好。 拜托,我的data 需要什么修改才能让您从不使用它来进行插图? 我的意思是如何操作我的数据以便它可以用于情节 让我们continue this discussion in chat。 你生成的数据和你的情节永远不一样

以上是关于如何为我的数据类型使用 Facet Grid的主要内容,如果未能解决你的问题,请参考以下文章

使用 facet_grid 在 ggplot 中自动设置数据代表中断

R ggplot facet_grid多箱图

如何(重新)排列 facet_wrap/_grid 的面板?

如何使用 facet_grid 将计数标签添加到直方图的每个条形?

ggplot2,facet_grid 的 x 轴问题

如何为 Seaborn Facet Plot 添加标题