我有這個 12 時間序列生成的資料,我使用ggplot2. 我想將 12 個圖排列成1 個單圖,使其成為3D使用facet_grid列名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)
uj5u.com熱心網友回復:
為了使用 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 包(v2.0.1)于 2021 年 11 月 7 日創建
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/354439.html
