【问题标题】:how to have a graphics matrix in R如何在R中拥有一个图形矩阵
【发布时间】:2020-10-24 16:49:16
【问题描述】:

假设你有 3 个变量:

gestation of the mom
height of the mom
weight of the baby at birth

我的 2 个变量 x 是:

gestation of the mom
height of the mom

我的变量 y 是:

weight of the baby at birth

我想要一个图形矩阵来解释婴儿出生时的体重与妈妈的妊娠期的关系以及婴儿出生时的体重与妈妈的身高的关系

我做到了:

pairs((baby$bwt~baby$gestation+baby$age))

我得到了一个像图片上的图形矩阵:matrix_picture

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

感谢阅读

编辑: [matrix2_picture][2]

如您所见,在我的横坐标上,我总是得到相同的值(0 - 300),但我希望获得更好的值以在每个图形上获得更好的可视化,例如年龄,我不能得到 200 或300,所以我想以横坐标 10 m 和 50 max 为例

谢谢

EDIT2:

[矩阵3][3]

最后一个问题,如果我想得到与图片相同的东西,我该如何用 ggplot 做到这一点

首先是妈妈的孕期与婴儿出生时的体重有关,其次是妈妈的年龄与婴儿出生时的体重有关,最后是妈妈的身高与婴儿出生时的体重有关

我做到了:

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

但我得到了它:

[矩阵3][4]

或者你可以看到我的纵坐标总是一样的,不像我使用对时那样。

非常感谢!!! [2]:https://i.stack.imgur.com/jppCJ.png [3]:https://i.stack.imgur.com/TnEBe.png [4]:https://i.stack.imgur.com/BPOUP.png

最后编辑:

你知道我们如何可以做同样的事情,但只针对每个变量的 reidus 有点像函数pairs(),但与residus配对

reg=lm(formula=baby$bwt~baby$weight+baby$gestation+baby$age)
summary(reg)
plot(reg)

我想在这 3 个变量(体重、妊娠、年龄)的函数中得到 baby$bwt 的残差

【问题讨论】:

  • 你是说残留物?现在我们只是在绘制数据,如果你在谈论残差,你必须有一个模型。
  • 是的,我给你看
  • 那么现在你想要y轴上的残差和x轴上的其他变量?
  • 是的,就是这样
  • 然后检查编辑3

标签: r


【解决方案1】:

据我所知,没有使用pairs 的解决方案。还有其他几种选择,我知道的使用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

然后为ggplot重塑数据框:

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

>df2
weight.of.the.baby.at.birth             variable    value
1                     52.92590 gestation.of.the.mom 304.9339
2                     43.06043 gestation.of.the.mom 219.7718
3                     56.19357 gestation.of.the.mom 310.6279
4                     54.33385 gestation.of.the.mom 278.8190
5                     51.95354 gestation.of.the.mom 247.4760
                              ...
21                    52.92590    height.of.the.mom 165.7853
22                    43.06043    height.of.the.mom 185.3528
23                    56.19357    height.of.the.mom 166.5677
24                    54.33385    height.of.the.mom 179.8276
25                    51.95354    height.of.the.mom 186.6949

然后绘图:

library(ggplot2)
ggplot(df2, aes(x=value, y=weight.of.the.baby.at.birth)) +
  geom_point() + facet_grid(.~variable)

输出:

您可以在Pairs scatter plot; one vs manyPlot one numeric variable against n numeric variables in n plots找到其他答案。

EDIT1:

要使比例不同,请将scales="free" 参数添加到facet_grid

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

输出:

EDIT2:

由于您希望固定变量成为您的x轴,您需要更改variablefacet_grid中的位置:

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

绘图:

ggplot(df2, aes(x=value, y=resid)) +
  geom_point() + facet_grid(.~variable, scales="free")

输出:

【讨论】:

  • 谢谢它的工作,但我在新答案中遇到了其他问题,但真的谢谢!
  • 不客气。但是在此评论部分添加新信息或编辑您的原始帖子,而不是通过创建答案。现在,您能否更好地解释“但问题是关于毕业的,我想在矩阵上获得更好的毕业”是什么意思?
  • 我添加了解决方案:)。
  • 谢谢!!那么,我向您展示的结果对您来说是一个图形列矩阵吗?因为我不确定 100%
  • “图形列矩阵”有正式定义吗?对我来说,结果是几个图表组织在一个矩阵中,所以我称之为是的。如果它解决了您的问题,请不要忘记接受将帖子标记为已解决的答案。
猜你喜欢
  • 2016-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-02
  • 1970-01-01
相关资源
最近更新 更多