Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot2:具有两个变量的多层图的面_包裹失败_R_Ggplot2 - Fatal编程技术网

R ggplot2:具有两个变量的多层图的面_包裹失败

R ggplot2:具有两个变量的多层图的面_包裹失败,r,ggplot2,R,Ggplot2,我正在尝试使用ggplot2创建一个自由缩放的多面绘图。按照设计,facet\u grid,无法实现我所需要的。而facet\u wrap失败,出现了一个神秘的错误。你能告诉我,你对如何修复这个错误有什么建议吗?下面给出了一个可复制的示例 让我们创建示例数据: require(tidyverse) require(modelr) d1 <- tibble( x = 1:100, y = 1:100 + rnorm(10), z = y ^ 2, dataset_name

我正在尝试使用
ggplot2
创建一个自由缩放的多面绘图。按照设计,
facet\u grid
,无法实现我所需要的。而
facet\u wrap
失败,出现了一个神秘的错误。你能告诉我,你对如何修复这个错误有什么建议吗?下面给出了一个可复制的示例

让我们创建示例数据:

require(tidyverse)
require(modelr)

d1 <- tibble(
  x = 1:100,
  y = 1:100 + rnorm(10),
  z = y ^ 2,
  dataset_name = "d1"
)

d2 <- tibble(
  x = 1:1000,
  y = 1:1000 + rnorm(10),
  z = y ^ 2,
  dataset_name = "d2"
)

#these data will be used for the 1st layer
actuals <- bind_rows(d1, d2)

#these data will be used for the 2nd layer
predictions <- bind_rows(
  d1 %>% gather_predictions(
    "m1" = lm(y ~ x, data = d1),
    "m2" = lm(y ~ x + z, data = d1),
    .pred = "y"
  ),
  d2 %>% gather_predictions(
    "m1" = lm(y ~ x, data = d2),
    "m2" = lm(y ~ x + z, data = d2),
    .pred = "y"
  )
)
如果我只想为一个数据集(即,
预测
)绘制数据,它将按预期工作,我得到4个方面:

ggplot(predictions, aes(x, y)) +
  geom_point() +
  facet_wrap( ~ model + dataset_name, scales = "free")

但是,如果我尝试将
实际值
预测值
组合如下:

ggplot(actuals, aes(x, y)) +
  geom_point() +
  geom_line(data = predictions, colour = "red") +
  facet_wrap( ~ model + dataset_name, scales = "free")
predictions <- bind_rows(
  d1 %>% gather_predictions(
    "m1" = lm(y ~ x, data = d1),
    "m2" = lm(y ~ x + z, data = d1),
    .pred = "y.pred"
  ),
  d2 %>% gather_predictions(
    "m1" = lm(y ~ x, data = d2),
    "m2" = lm(y ~ x + z, data = d2),
    .pred = "y.pred"
  )
)

然后出现以下错误:
gList中的错误(列表(x=0.5,y=0.5,width=1,height=1,just=“center”),:在“gList”中只允许“grobs”

尝试通过
模型和
数据集名称的交互来创建单个变量

# these two blocks of code are equivalent

library(magrittr)
predictions %<>% mutate(mod_dn = interaction(model, dataset_name))

感谢大家的帮助!似乎一个更简单的解决方案(即使它会改变原来的问题)是调整
预测,如下所示:

ggplot(actuals, aes(x, y)) +
  geom_point() +
  geom_line(data = predictions, colour = "red") +
  facet_wrap( ~ model + dataset_name, scales = "free")
predictions <- bind_rows(
  d1 %>% gather_predictions(
    "m1" = lm(y ~ x, data = d1),
    "m2" = lm(y ~ x + z, data = d1),
    .pred = "y.pred"
  ),
  d2 %>% gather_predictions(
    "m1" = lm(y ~ x, data = d2),
    "m2" = lm(y ~ x + z, data = d2),
    .pred = "y.pred"
  )
)

这将呈现。

Thank you@Melissa!请注意,如果我正确调整了您的解决方案,它将破坏实际的数据呈现。基本上,所有四个图在两个图上都显示了
d1
d2
数据的并集,请参见此。好的提示-我解决了问题,现在应该按照需要执行。
%%
表示法需要
magrittr
包,但它在效果上与您的调整相同。您可以跳过获取交互项的步骤,只连接两个数据帧,然后使用
facet_wrap(~model+dataset_name)
。据我所知,唯一的区别是条带标签是单标签(例如“m1.d1”)还是双标签堆叠(例如“m1”)也就是说,单条标签在很多方面看起来都比较干净。我认为问题在于
实际值中没有
模型
变量。
predictions <- bind_rows(
  d1 %>% gather_predictions(
    "m1" = lm(y ~ x, data = d1),
    "m2" = lm(y ~ x + z, data = d1),
    .pred = "y.pred"
  ),
  d2 %>% gather_predictions(
    "m1" = lm(y ~ x, data = d2),
    "m2" = lm(y ~ x + z, data = d2),
    .pred = "y.pred"
  )
)
ggplot(predictions, aes(x, y)) +
  geom_point() +
  geom_line(aes(x, y.pred), colour = "red") +
  facet_wrap( ~ model + dataset_name, scales = "free")