r - 如何在R中拥有一个图形矩阵
问题描述
想象一下你有 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 的横坐标
谢谢
编辑2:
[矩阵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
最后编辑:
你知道我们怎么能做同样的事情,但只针对每个变量的 redidus 有点像函数pairs() 但与 residus 配对
reg=lm(formula=baby$bwt~baby$weight+baby$gestation+baby$age)
summary(reg)
plot(reg)
我想在这 3 个变量(体重、妊娠、年龄)的函数中得到 baby$bwt 的残差
解决方案
据我所知,没有使用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)
输出:
您可以在以下位置找到其他答案:对散点图;一对多,并在 n 个图中针对 n 个数值变量绘制一个数值变量。
编辑1:
要使比例不同,请将scales="free"
参数添加到facet_grid
:
ggplot(df2, aes(x=value, y=weight.of.the.baby.at.birth)) +
geom_point() + facet_grid(.~variable, scales="free")
输出:
编辑2:
由于您希望固定变量成为您的 x 轴,因此您需要更改variable
in的位置facet_grid
:
ggplot(df2, aes(x=value, y=weight.of.the.baby.at.birth)) +
geom_point() + facet_grid(variable~., scales="free")
输出:
编辑3:
创建模型:
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")
输出:
推荐阅读
- python - 替换文件中第 n 次出现之间的所有内容
- java - 按钮仅适用于使用 jQuery 的分页页面 1
- javascript - 如何在博客中添加自定义标签下拉列表?
- c# - 等价于 C# 中 RxJava 的 concatMapCompletable
- java - 测量 Spring 中请求所需的时间
- html - 如何使用 CSS 更好地保持响应式大小和定位?
- android - 尝试在空对象引用上调用接口方法“boolean com.google.common.util.concurrent.ListenableFuture.cancel(boolean)”
- scala - scala 直接映射序列元素的字段
- numpy - AttributeError:“numpy.ndarray”对象没有属性“value_counts”
- python - 如何合并两个数据框并处理重复项