问题描述
我想创建一个相关矩阵图,即每个变量相对于其他变量在散点图中绘制的图,例如 pairs()
或 splom()
。我想用ggplot2做到这一点。 。该链接提到了某人在ggplot2中为此编写的一些代码,但是,它已经过时且不再起作用(即使在交换掉不推荐使用的部分之后也是如此)。
I want to create a correlation matrix plot, i.e. a plot where each variable is plotted in a scatterplot against each other variable like with pairs()
or splom()
. I want to do this with ggplot2. See here for examples. The link mentions some code someone wrote for doing this in ggplot2, however, it is outdated and no longer works (even after you swap out the deprecated parts).
循环执行此操作,然后循环执行 multiplot()
,但必须有更好的方法。我尝试将数据集融化成长时间,然后复制值和变量变量,然后使用构面。
One could do this with a loop in a loop and then multiplot()
, but there must be a better way. I tried melting the dataset to long, and copying the value and variable variables and then using facets. This almost gives you something correct.
d = data.frame(x1=rnorm(100),
x2=rnorm(100),
x3=rnorm(100),
x4=rnorm(100),
x5=rnorm(100))
library(reshape2)
d = melt(d)
d$value2 = d$value
d$variable2 = d$variable
library(ggplot2)
ggplot(data=d, aes(x=value, y=value2)) +
geom_point() +
facet_grid(variable ~ variable2)
这使一般结构正确,但是仅适用于针对每个变量绘制自身。
This gets the general structure right, but only works for the plotting each variable against itself. Is there some more clever way of doing this without resorting to 2 loops?
推荐答案
library(GGally)
set.seed(42)
d = data.frame(x1=rnorm(100),
x2=rnorm(100),
x3=rnorm(100),
x4=rnorm(100),
x5=rnorm(100))
# estimated density in diagonal
ggpairs(d)
# blank
ggpairs(d, diag = list("continuous"="blank")
这篇关于ggplot2的相关矩阵图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!