Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.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中创建图形矩阵_R - Fatal编程技术网

如何在R中创建图形矩阵

如何在R中创建图形矩阵,r,R,假设您有3个变量: gestation of the mom height of the mom weight of the baby at birth 我的两个变量x是: gestation of the mom height of the mom 我的变量y是: weight of the baby at birth 我想得到一个图形矩阵,它解释了婴儿出生时的体重和母亲怀孕的关系,以及婴儿出生时的体重和母亲身高的关系 我做到了: pairs((baby$bwt~baby$gestati

假设您有3个变量:

gestation of the mom
height of the mom
weight of the baby at birth
我的两个变量x是:

gestation of the mom
height of the mom
我的变量y是:

weight of the baby at birth
我想得到一个图形矩阵,它解释了婴儿出生时的体重和母亲怀孕的关系,以及婴儿出生时的体重和母亲身高的关系

我做到了:

pairs((baby$bwt~baby$gestation+baby$age))
df3 <- reshape2::melt(baby, "bwt")
 
 ggplot(df3, aes(x=bwt, y=value)) +
   geom_point() + facet_grid(.~variable,scales="free") 
我得到一个图形矩阵,如图所示:

但我想知道我怎么能得到x1函数中的y和x2函数中的y,因为在我的图片上,我得到了所有,换句话说,我只想得到图片的第一行

谢谢你读我的书

编辑: [图片][2]

正如你们所看到的,在我的横坐标上,我得到的值总是相同的(0-300),但我希望得到更好的值,以便在每个图形上获得更好的可视化效果,例如,对于年龄,我不能得到200或300,所以我希望横坐标为10 m,最大值为50

谢谢

编辑2:

[现场][3]

最后一个问题,如果我想得到和图片上一样的东西,我怎么能用ggplot呢

首先是母亲的妊娠期与婴儿出生时体重的函数关系,其次是母亲的年龄与婴儿出生时体重的函数关系,最后是母亲的身高与婴儿出生时体重的函数关系

我做到了:

pairs((baby$bwt~baby$gestation+baby$age))
df3 <- reshape2::melt(baby, "bwt")
 
 ggplot(df3, aes(x=bwt, y=value)) +
   geom_point() + facet_grid(.~variable,scales="free") 

我想要婴儿$bwt的剩余值作为这3个变量(体重、妊娠、年龄)的函数

据我所知,没有一个解决方案使用
。还有几个其他选项,我知道的一个使用
ggplot2

首先生成一些虚拟数据:

df <- data.frame(
  `gestation of the mom` = rnorm(20,300,30),
  `height of the mom` = rnorm(20,170,10),
  `weight of the baby at birth` = rnorm(20,50,5))

>df
gestation.of.the.mom height.of.the.mom weight.of.the.baby.at.birth
1              304.9339          165.7853                    52.92590
2              219.7718          185.3528                    43.06043
3              310.6279          166.5677                    56.19357
4              278.8190          179.8276                    54.33385
5              247.4760          186.6949                    51.95354
输出:

您可以在中找到其他答案:、和

EDIT1:

要使比例不同,请将
scales=“free”
参数添加到
facet\u网格中:

ggplot(df2, aes(x=value, y=weight.of.the.baby.at.birth)) +
  geom_point() + facet_grid(.~variable, scales="free")
ggplot(df2, aes(x=value, y=weight.of.the.baby.at.birth)) +
  geom_point() + facet_grid(variable~., scales="free")
输出:

EDIT2:

由于您希望固定变量成为x轴,因此需要更改
变量
facet\u网格中的位置

ggplot(df2, aes(x=value, y=weight.of.the.baby.at.birth)) +
  geom_point() + facet_grid(.~variable, scales="free")
ggplot(df2, aes(x=value, y=weight.of.the.baby.at.birth)) +
  geom_point() + facet_grid(variable~., scales="free")
输出:

EDIT3:

创建模型:

reg = lm(df$weight.of.the.baby.at.birth ~ df$gestation.of.the.mom + df$height.of.the.mom)
添加带有残差的列(重塑之前),然后重塑:

df$resid = resid(reg)

df2 <- reshape2::melt(df, c("weight.of.the.baby.at.birth","resid"))
输出:


谢谢你,伙计,这很有效,但我在新答案中遇到了一个其他问题,但我真的非常感谢!!不客气。但在评论部分添加新信息或编辑原始帖子,而不是创建答案。现在,你能更好地解释你所说的“但问题在于毕业,我希望在矩阵上有更好的毕业”是什么意思吗?我补充了解决方案:)。谢谢!!那么,我给你看的结果是一个图形列矩阵吗?因为我不确定100%是否有“图形列矩阵”的正式定义?对我来说,结果是几个图组织在一个矩阵中,所以我称之为是的。如果它解决了你的问题,别忘了接受答案,把帖子标记为已解决。你是说残留物?现在我们只是绘制数据,如果你说的是残差,你必须有一个模型。是的,我向你展示了,所以你现在想要y轴的残差和x轴的其他变量?是的,这完全是编辑3