为什么 Facet_Grid 输出与其数据不同

Why is the Facet_Grid Output Different from Its Data

我有这些时间序列数据,我绘制如下

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

我现在想用facet_grid给它整容,我试过如下

df <- data.frame(xx, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12)
reshapp <- reshape2::melt(df, id = "xx")

NEWDAT <- data.frame(y = reshapp$value, x = reshapp$xx, sd = rep(c(1,3,5,10), each = 3), phi = rep(c(.8, .9, .95), 4))

ggplot(NEWDAT, aes(x = x, y = y)) +  geom_line() +  geom_point() + facet_grid(sd ~ phi,  scales = "free_y") +

theme_bw() + ggplot2::scale_y_continuous(展开 = c(0.0, 0.00))

我想要什么

如何让 Facet_Grid Plot 成为 Grid_Arranged Plot 的样子?

指针

可以看到第一个系列中的 lowest 数据点是 -0.2505125highest4.6956397。在 Facet-Grid Plot lowest is -20+highest is 30+.

代码

创建数据框

df <- data.frame(
    timestamp = rep(1:10, times = 12),
    value = c(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12),
    # via sprintf() to add a leading zero for easier ordering
    sim_id = paste0("x", sprintf("%02d", rep(1:12, each = 10)))
)

绘图数据

ggplot(df, aes(x = timestamp, y = value)) +
    geom_line(color = "#F2AA4CFF") +
    geom_point(color = "#101820FF") +
    labs(x = "lb", y = "RMSE") +
    theme_bw() + 
    scale_y_continuous(expand = c(0, 0)) +
    # Facet wrap with set number of columns and free scales
    facet_wrap(. ~ sim_id, ncol = 3, scales = "free")

数据

library(ggplot2)

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

看评论的时候,我看到你们要删除这个问题。但是,我想请您花点时间看看我的解决方案。我认为它更简单,更透明。

我们将从一个简单的 tibble 开始,其中将包含您的计算参数。

library(tidyverse)

df = tibble(
  seed = rep(c(289805, 671086, 799837), 4),
  sd = rep(c(1, 3, 5, 10), each=3),
  ar = rep(c(.8, .9, .95), 4)
)

产出

# A tibble: 12 x 3
     seed    sd    ar
    <dbl> <dbl> <dbl>
 1 289805     1  0.8 
 2 671086     1  0.9 
 3 799837     1  0.95
 4 289805     3  0.8 
 5 671086     3  0.9 
 6 799837     3  0.95
 7 289805     5  0.8 
 8 671086     5  0.9 
 9 799837     5  0.95
10 289805    10  0.8 
11 671086    10  0.9 
12 799837    10  0.95

现在我们正在准备一个简单的 farima 函数。

farima = function(seed, n, ar, order, sd){ 
  set.seed(seed)
  model = arima.sim(n=n, model=list(ar = ar, order = order), sd = sd)
  tibble(
    name = paste("sd -", sd, "ar -", ar),
    x = model %>% as.numeric(), 
    lb = 1:length(x)
    )
}

之后你所要做的就是一个简单的突变!

df %>% nest_by(seed, ar, sd) %>% 
  mutate(data = list(farima(seed, 10, ar, c(1, 0, 0), sd))) %>% 
  unnest(data) %>% 
  ggplot(aes(lb, x))+
  geom_line(color = "#F2AA4CFF")+
  geom_point(color = "#101820FF")+
  theme_bw()+
  scale_y_continuous(expand = c(0, 0))+
  facet_wrap(.~name, ncol = 3, scales = "free")

但你也可以做一些不同的事情。

df %>% nest_by(seed, ar, sd) %>% 
  mutate(data = list(farima(seed, 10, ar, c(1, 0, 0), sd))) %>% 
  unnest(data) %>% 
  ggplot(aes(lb, x))+
  geom_line(color = "#F2AA4CFF")+
  geom_point(color = "#101820FF")+
  theme_bw()+
  scale_y_continuous(expand = c(0, 0))+
  facet_grid(vars(sd), vars(ar), scales = "free")

希望您喜欢我的解决方案,您不会删除此问题,而是将其标记为有用。

@Daniel James 的小更新

你必须很好地理解当你使用nest_by函数时会发生什么。我会一步一步地告诉你。

df %>% nest_by(seed, ar, sd) %>% #Step 1
  mutate(data = list(farima(seed, 10, ar, c(1, 0, 0), sd))) %>% #Step 2
  unnest(data) %>% #Step 3
  ggplot(aes(lb, x))+ #Step 4
  geom_line(color = "#F2AA4CFF")+
  geom_point(color = "#101820FF")+
  theme_bw()+
  scale_y_continuous(expand = c(0, 0))+
  facet_wrap(.~name, ncol = 3, scales = "free")

在第一步之后,您会得到一个破碎的 tibble,但是,不是按变量而是 按行 date 变量出现在这里,在这种情况下它基本上是空的(它包含零值)。

# A tibble: 12 x 4
# Rowwise:  seed, ar, sd
     seed    ar    sd               data
    <dbl> <dbl> <dbl> <list<tibble[,0]>>
 1 289805  0.8      1            [1 x 0]
 2 289805  0.8      3            [1 x 0]
 3 289805  0.8      5            [1 x 0]
 4 289805  0.8     10            [1 x 0]
 5 671086  0.9      1            [1 x 0]
 6 671086  0.9      3            [1 x 0]
 7 671086  0.9      5            [1 x 0]
 8 671086  0.9     10            [1 x 0]
 9 799837  0.95     1            [1 x 0]
10 799837  0.95     3            [1 x 0]
11 799837  0.95     5            [1 x 0]
12 799837  0.95    10            [1 x 0]

如果您现在进行突变,它将应用于每一行中的值。

那么让我们看看第二步之后我们有什么。

# A tibble: 12 x 4
# Rowwise:  seed, ar, sd
     seed    ar    sd data             
    <dbl> <dbl> <dbl> <list>           
 1 289805  0.8      1 <tibble [10 x 3]>
 2 289805  0.8      3 <tibble [10 x 3]>
 3 289805  0.8      5 <tibble [10 x 3]>
 4 289805  0.8     10 <tibble [10 x 3]>
 5 671086  0.9      1 <tibble [10 x 3]>
 6 671086  0.9      3 <tibble [10 x 3]>
 7 671086  0.9      5 <tibble [10 x 3]>
 8 671086  0.9     10 <tibble [10 x 3]>
 9 799837  0.95     1 <tibble [10 x 3]>
10 799837  0.95     3 <tibble [10 x 3]>
11 799837  0.95     5 <tibble [10 x 3]>
12 799837  0.95    10 <tibble [10 x 3]>

请注意,我们在 mutate 函数中使用的 farima 函数用于参数 seedarsd 在每一行上接收单个值!还要注意使用 list 函数,您将在其中包装我们的 farima 函数。

现在我们要做的就是解决我们的 data 变量。我们将在第 3 步中执行此操作。

# A tibble: 120 x 6
# Groups:   seed, ar, sd [12]
     seed    ar    sd name                 x    lb
    <dbl> <dbl> <dbl> <chr>            <dbl> <int>
 1 289805   0.8     1 sd - 1 ar - 0.8 -0.251     1
 2 289805   0.8     1 sd - 1 ar - 0.8  0.886     2
 3 289805   0.8     1 sd - 1 ar - 0.8  0.198     3
 4 289805   0.8     1 sd - 1 ar - 0.8  1.54      4
 5 289805   0.8     1 sd - 1 ar - 0.8  4.70      5
 6 289805   0.8     1 sd - 1 ar - 0.8  4.44      6
 7 289805   0.8     1 sd - 1 ar - 0.8  3.02      7
 8 289805   0.8     1 sd - 1 ar - 0.8  1.81      8
 9 289805   0.8     1 sd - 1 ar - 0.8  1.17      9
10 289805   0.8     1 sd - 1 ar - 0.8  1.16     10
# ... with 110 more rows

第 4 步及以后发生的一切都只是创建适当的图表。