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). I want to use corrplot in a slightly different way.
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? If yes, how?
PS. The value I want to plot is beta.
EDIT: I edited the example file to be non-symmetric with respect to the nr of variables on x and y
You can reshape your data into a square matrix and just feed it into 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)