首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么Facet_Grid输出与其数据不同?

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

Stack Overflow用户
提问于 2021-09-24 13:23:05
回答 2查看 122关注 0票数 1

我有如下所示的时间序列数据

代码语言:javascript
复制
## 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做一次面部提升,我尝试了如下所示

代码语言:javascript
复制
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.2505125,而highest4.6956397。在Facet-Grid Plot中,lowest is -20+highest is 30+

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-11-09 20:25:56

当我读到这些评论时,我看到你要删除这个问题。不过,我想请你花点时间看看我的解决办法。我认为它更简单,更透明。

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

代码语言:javascript
复制
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)
)

输出

代码语言:javascript
复制
# 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函数。

代码语言:javascript
复制
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)
    )
}

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

代码语言:javascript
复制
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")

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

代码语言:javascript
复制
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函数时会发生什么。我会一步一步地给你看。

代码语言:javascript
复制
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变量出现在这里,在本例中它基本上是空的(它包含零值)。

代码语言:javascript
复制
# 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]

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

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

代码语言:javascript
复制
# 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]>

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

现在我们要做的就是求解我们的data变量。我们将在第三步中这样做。

代码语言:javascript
复制
# 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及以后发生的一切只是创建适当的图表。

票数 4
EN

Stack Overflow用户

发布于 2021-11-08 03:36:30

代码

创建数据框架

代码语言:javascript
复制
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)))
)

地块数据

代码语言:javascript
复制
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")

数据

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69315757

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档