对n个图中的n个数字变量绘制一个数字变量 [英] Plot one numeric variable against n numeric variables in n plots

查看:79
本文介绍了对n个图中的n个数字变量绘制一个数字变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个巨大的数据框,我想作一些图以了解不同变量之间的关联.我不能使用

I have a huge data frame and I would like to make some plots to get an idea of the associations among different variables. I cannot use

pairs(data)

,因为那会给我400多个地块.但是,我特别感兴趣的是一个响应变量y.因此,我想针对所有变量绘制y,这将使绘制数从n ^ 2减少到n.你能告诉我怎么做吗?谢谢

, because that would give me 400+ plots. However, there's one response variable y I'm particularly interested in. Thus, I'd like to plot y against all variables, which would reduce the number of plots from n^2 to n. Can you show me how to do it? Thanks

编辑:为清楚起见,我添加了一个示例.假设我有数据框

I add an example for the sake of clarity. Let's say I have the dataframe

foo=data.frame(x1=1:10,x2=seq(0.1,1,0.1),x3=-7:2,x4=runif(10,0,1))

,我的响应变量是x3.然后,我想生成连续排列的四个图,分别为x1 vs x3,x2 vs x3,x3的直方图,最后是x4 vs x3.我知道如何制作每个情节

and my response variable is x3. Then I'd like to generate four plots arranged in a row, respectively x1 vs x3, x2 vs x3, an histogram of x3 and finally x4 vs x3. I know how to make each plot

plot(foo$x1,foo$x3)
plot(foo$x2,foo$x3)
hist(foo$x3)
plot(foo$x4,foo$x3)

但是我不知道如何将它们连续排列.同样,如果有一种方法可以自动绘制所有n个图,而不必每次都调用命令图(或历史记录),那将是很好的.当n = 4时,这不是什么大问题,但是我通常会处理n = 20 +个变量,因此可能会很麻烦.

However I have no idea how to arrange them in a row. Also, it would be great if there was a way to automatically make all the n plots, without having to call the command plot (or hist) each time. When n=4, it's not that big of an issue, but I usually deal with n=20+ variables, so it can be a drag.

推荐答案

可以执行reshape2/ggplot2/gridExtra软件包组合.这样,您无需指定地块数量.这段代码无需修改即可在任意数量的解释变量上工作

Could do reshape2/ggplot2/gridExtra packages combination. This way you don't need to specify the number of plots. This code will work on any number of explaining variables without any modifications

foo <- data.frame(x1=1:10,x2=seq(0.1,1,0.1),x3=-7:2,x4=runif(10,0,1))
library(reshape2)
foo2 <- melt(foo, "x3")
library(ggplot2)
p1 <- ggplot(foo2, aes(value, x3)) +  geom_point() + facet_grid(.~variable)
p2 <- ggplot(foo, aes(x = x3)) + geom_histogram()
library(gridExtra)
grid.arrange(p1, p2, ncol=2)

这篇关于对n个图中的n个数字变量绘制一个数字变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆