[英]R - Using corrplot with different variables on x and y axes
As you all probably know, corrplot can be used to create beautiful plots that visualize the strength of the relationship amongst a set of variables, with the same variables on the x-axis as on the y-axis (usually symmetrical, unless you have a different metric on the upper diagonal than the lower diagonal). 众所周知,corrplot可用于创建漂亮的图,以可视化一组变量之间的关系强度,x轴上的变量与y轴上的变量相同(通常是对称的,除非您有上对角线与下对角线的度量不同) I want to use corrplot in a slightly different way.
我想以稍微不同的方式使用corrplot。
Suppose I have a dataframe that look like this: 假设我有一个像这样的数据框:
var1 var2 beta se pvalue
X a .01 .01 .35
X b -.02 .02 .45
X c .04 .01 .55
X d .04 .01 .55
Y a .06 .01 .01
Y b -.02 .02 .25
Y c .04 .01 .55
Y d .02 .01 .55
Z a .04 .01 .01
Z b -.01 .01 .45
Z c .03 .02 .02
Z d .04 .02 .02
Can I use corrplot to create a figure with variables X,Y, and Z on the x-axis and a, b, c, and d on the Y-axis? 我可以使用corrplot创建在x轴上带有变量X,Y和Z以及在Y轴上带有a,b,c和d的图形吗? If yes, how?
如果是,怎么办?
PS. PS。 The value I want to plot is beta.
我要绘制的值是beta。
EDIT: I edited the example file to be non-symmetric with respect to the nr of variables on x and y 编辑:我将示例文件编辑为相对于x和y变量的nr不对称
You can reshape your data into a square matrix and just feed it into corrplot
. 您可以将数据重塑为方矩阵,然后将其输入
corrplot
。 This works even if the resulting matrix is not square. 即使结果矩阵不是正方形,此方法也有效。
## Your new data
Dat = read.table(text="var1 var2 beta se pvalue
X a .01 .01 .35
X b -.02 .02 .45
X c .04 .01 .55
X d .04 .01 .55
Y a .06 .01 .01
Y b -.02 .02 .25
Y c .04 .01 .55
Y d .02 .01 .55
Z a .04 .01 .01
Z b -.01 .01 .45
Z c .03 .02 .02
Z d .04 .02 .02",
header=TRUE)
## Now reshape the data (same as before)
wide = reshape(Dat[,1:3], idvar = c("var1"),
timevar="var2", direction = "wide")
rownames(wide) = wide$var1
wide = wide[,-1]
colnames(wide) = sub("beta.", "", colnames(wide))
## Pass it to corrplot
library(corrplot)
corrplot(as.matrix(wide), is.corr=FALSE, tl.srt=0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.