我有一些相关值的矩阵。现在,我想在一个看起来或多或少像这样的图形中绘制该图形:
我该如何实现?
我有一些相关值的矩阵。现在,我想在一个看起来或多或少像这样的图形中绘制该图形:
我该如何实现?
Answers:
快速,肮脏并且在球场上:
library(lattice)
#Build the horizontal and vertical axis information
hor <- c("214", "215", "216", "224", "211", "212", "213", "223", "226", "225")
ver <- paste("DM1-", hor, sep="")
#Build the fake correlation matrix
nrowcol <- length(ver)
cor <- matrix(runif(nrowcol*nrowcol, min=0.4), nrow=nrowcol, ncol=nrowcol, dimnames = list(hor, ver))
for (i in 1:nrowcol) cor[i,i] = 1
#Build the plot
rgb.palette <- colorRampPalette(c("blue", "yellow"), space = "rgb")
levelplot(cor, main="stage 12-14 array correlation matrix", xlab="", ylab="", col.regions=rgb.palette(120), cuts=100, at=seq(0,1,0.01))
ellipse:plotcorr
。
ggplot2库可以使用来处理此问题geom_tile()
。由于没有任何负相关,因此在上面的图中似乎已进行了一些缩放,因此请考虑到您的数据。使用mtcars
数据集:
library(ggplot2)
library(reshape)
z <- cor(mtcars)
z.m <- melt(z)
ggplot(z.m, aes(X1, X2, fill = value)) + geom_tile() +
scale_fill_gradient(low = "blue", high = "yellow")
编辑:
ggplot(z.m, aes(X1, X2, fill = value)) + geom_tile() +
scale_fill_gradient2(low = "blue", high = "yellow")
c(-1, -0.6, -0.3, 0, 0.3, 0.6, 1)
)与"white"
在中间,让颜色反映相关系数的对称性。
scale_fill_gradient2()
可以实现您自动描述的功能。我不知道那个存在。
p <- ggplot(.....) + ... + ....; library(plotly); ggplotly(p)
将使其互动
X1
使用:z.m$X1 <- factor(z.m$X1, levels = rev(levels( z.m$X1 )))
使用corrplot软件包:
library(corrplot)
data(mtcars)
M <- cor(mtcars)
## different color series
col1 <- colorRampPalette(c("#7F0000","red","#FF7F00","yellow","white",
"cyan", "#007FFF", "blue","#00007F"))
col2 <- colorRampPalette(c("#67001F", "#B2182B", "#D6604D", "#F4A582", "#FDDBC7",
"#FFFFFF", "#D1E5F0", "#92C5DE", "#4393C3", "#2166AC", "#053061"))
col3 <- colorRampPalette(c("red", "white", "blue"))
col4 <- colorRampPalette(c("#7F0000","red","#FF7F00","yellow","#7FFF7F",
"cyan", "#007FFF", "blue","#00007F"))
wb <- c("white","black")
par(ask = TRUE)
## different color scale and methods to display corr-matrix
corrplot(M, method="number", col="black", addcolorlabel="no")
corrplot(M, method="number")
corrplot(M)
corrplot(M, order ="AOE")
corrplot(M, order ="AOE", addCoef.col="grey")
corrplot(M, order="AOE", col=col1(20), cl.length=21,addCoef.col="grey")
corrplot(M, order="AOE", col=col1(10),addCoef.col="grey")
corrplot(M, order="AOE", col=col2(200))
corrplot(M, order="AOE", col=col2(200),addCoef.col="grey")
corrplot(M, order="AOE", col=col2(20), cl.length=21,addCoef.col="grey")
corrplot(M, order="AOE", col=col2(10),addCoef.col="grey")
corrplot(M, order="AOE", col=col3(100))
corrplot(M, order="AOE", col=col3(10))
corrplot(M, method="color", col=col1(20), cl.length=21,order = "AOE", addCoef.col="grey")
if(TRUE){
corrplot(M, method="square", col=col2(200),order = "AOE")
corrplot(M, method="ellipse", col=col1(200),order = "AOE")
corrplot(M, method="shade", col=col3(20),order = "AOE")
corrplot(M, method="pie", order = "AOE")
## col=wb
corrplot(M, col = wb, order="AOE", outline=TRUE, addcolorlabel="no")
## like Chinese wiqi, suit for either on screen or white-black print.
corrplot(M, col = wb, bg="gold2", order="AOE", addcolorlabel="no")
}
例如:
相当优雅的IMO
该图的类型在其他术语中称为“热图”。一旦有了相关矩阵,就可以使用各种教程之一对其进行绘制。
使用基本图形:http : //flowingdata.com/2010/01/21/how-to-make-a-heatmap-a-quick-and-easy-solution/
使用ggplot2:http ://learnr.wordpress.com/2010/01/26/ggplot2-quick-heatmap-plotting/
我一直在进行类似于@daroczig发布的可视化的工作,并使用软件包的plotcorr()
功能使用@Ulrik发布的代码ellipse
。我喜欢用椭圆表示相关性,喜欢用颜色表示负相关和正相关。但是,我希望醒目的颜色在接近1和-1的相关性中脱颖而出,而不是接近0的相关性。
我创建了一种替代方法,其中将白色椭圆形覆盖在彩色圆圈上。调整每个白色椭圆的大小,以使其后面可见的彩色圆的比例等于平方的相关性。当相关性接近1和-1时,白色椭圆很小,并且许多彩色圆圈可见。当相关性接近0时,白色椭圆变大,几乎看不到彩色圆圈。
plotcor()
可以在https://github.com/JVAdams/jvamisc/blob/master/R/plotcor.r中使用该功能。
mtcars
下面显示了使用数据集生成的绘图的示例。
library(plotrix)
library(seriation)
library(MASS)
plotcor(cor(mtcars), mar=c(0.1, 4, 4, 0.1))
我意识到已经有一段时间了,但是新读者可能会对软件包(https://cran.rstudio.com/web/packages/corrr/index.html)感兴趣,rplot()
该corrr
软件包可以生成各种@daroczig提及的图,但设计了一种数据管道方法:
install.packages("corrr")
library(corrr)
mtcars %>% correlate() %>% rplot()
mtcars %>% correlate() %>% rearrange() %>% rplot()
mtcars %>% correlate() %>% rearrange() %>% rplot(shape = 15)
mtcars %>% correlate() %>% rearrange() %>% shave() %>% rplot(shape = 15)
mtcars %>% correlate() %>% rearrange(absolute = FALSE) %>% rplot(shape = 15)
corrplot R包中的corrplot()函数也可用于绘制相关图。
library(corrplot)
M<-cor(mtcars) # compute correlation matrix
corrplot(M, method="circle")
这里发布了几篇描述如何计算和可视化相关矩阵的文章:
由于我无法发表评论,因此我必须给daroczig答案以2c的评分。
椭圆散点图确实来自椭圆包,并使用以下命令生成:
corr.mtcars <- cor(mtcars)
ord <- order(corr.mtcars[1,])
xc <- corr.mtcars[ord, ord]
colors <- c("#A50F15","#DE2D26","#FB6A4A","#FCAE91","#FEE5D9","white",
"#EFF3FF","#BDD7E7","#6BAED6","#3182BD","#08519C")
plotcorr(xc, col=colors[5*xc + 6])
(来自手册页)
根据建议,corrplot程序包也可能对此处找到的漂亮图像很有用