简体   繁体   English

如何在R中绘制相关矩阵之上的相关图?

[英]How to plot, in R, a correlogram on top of a correlation matrix?

I've followed the instructions on this website from STHDA to plot correlation matrices and correlograms in R. The website and examples are really good. 我已按照STHDA在本网站上的说明在R中绘制相关矩阵和相关图。网站和示例非常好。 However, I'd like to plot the upper part of the correlogram over the upper part of the correlation matrix. 但是,我想在相关矩阵的上部绘制相关图的上半部分。

Here's the code: 这是代码:

library(PerformanceAnalytics)
chart.Correlation(mtcars, histogram=TRUE, pch=19)

This should give me the correlation matrix using scatter plots, together with the histogram, which I'd like to maintain. 这应该给我一个使用散点图的相关矩阵,以及我想维持的直方图。 But for the upper part of the plot, I'd like to have the correlogram obtained from this code: 但是对于图的上半部分,我想从这段代码中获得相关图:

library(corrplot)
corrplot(cor(mtcars), type="upper", order="hclust", tl.col="black", tl.srt=45)

The obvious way of doing it is exporting all graphs in pdf and then work with Inkscape , but it would be nicer if I could get this directly from R. Is there any possible way for doing this? 这样做的显而易见的方法是以pdf格式导出所有图形,然后使用Inkscape ,但如果我可以直接从R中获取它会更好。有没有可行的方法来做到这一点?

Thanks. 谢谢。

The trick to using the panel functions within pairs is found in help(pairs) : help(pairs)可以找到pairs使用面板函数的技巧:

A panel function should not attempt to start a new plot, but just plot within a given coordinate system: thus 'plot' and 'boxplot' are not panel functions. 面板功能不应尝试启动新绘图,而只是在给定坐标系内绘图:因此“绘图”和“boxplot”不是面板功能。

So, you should use graphic- adding functions, such as points , lines , polygon , or perhaps (when available) plot(..., add=TRUE) , but not a straight-up plot. 因此,您应该使用图形添加功能,例如pointslinespolygon ,或者可能(如果可用) plot(..., add=TRUE) ,但不能使用直接绘图。 What you were suggesting in your comment (with SpatialPolygons ) might have worked with some prodding if you actually tried to plot it on a device vice just returning it from your plotting function. 你在评论中建议的(使用SpatialPolygons )如果你真的试图将它绘制在一个设备副上,只是从你的绘图函数返回它,那么你可能会使用一些刺激。

In my example below, I actually do "create a new plot", but I cheat (based on this SO post ) by adding a second plot on top of the one already there. 在我下面的例子中,我实际上是“创建一个新的情节”,但我作弊(基于这个SO帖子 )在已经存在的那个上添加第二个情节。 I do this to shortcut an otherwise necessary scale/shift, which would still not be perfect since you appear to want a "perfect circle", something that can really only be guaranteed with asp=1 (aspect ratio fixed at 1:1). 我这样做是为了缩短其他必要的缩放/移位,这仍然不是完美的,因为你似乎想要一个“完美的圆圈”,实际上只能用asp=1 (宽高比固定为1:1)来保证。

colorRange <- c('#69091e', '#e37f65', 'white', '#aed2e6', '#042f60')
## colorRamp() returns a function which takes as an argument a number
## on [0,1] and returns a color in the gradient in colorRange
myColorRampFunc <- colorRamp(colorRange)

panel.cor <- function(w, z, ...) {
    correlation <- cor(w, z)

    ## because the func needs [0,1] and cor gives [-1,1], we need to
    ## shift and scale it
    col <- rgb( myColorRampFunc( (1+correlation)/2 )/255 )

    ## square it to avoid visual bias due to "area vs diameter"
    radius <- sqrt(abs(correlation))
    radians <- seq(0, 2*pi, len=50)     # 50 is arbitrary
    x <- radius * cos(radians)
    y <- radius * sin(radians)
    ## make them full loops
    x <- c(x, tail(x,n=1))
    y <- c(y, tail(y,n=1))

    ## I trick the "don't create a new plot" thing by following the
    ## advice here: http://www.r-bloggers.com/multiple-y-axis-in-a-r-plot/
    ## This allows
    par(new=TRUE)
    plot(0, type='n', xlim=c(-1,1), ylim=c(-1,1), axes=FALSE, asp=1)
    polygon(x, y, border=col, col=col)
}

pairs(mtcars, upper.panel=panel.cor)

在此输入图像描述

You can manipulate the size of the circles -- at the expense of unbiased visualization -- by playing with the radius. 您可以通过使用半径来操纵圆的大小 - 以无偏见的可视化为代价。 The colors I took directly from the page you linked to originally. 我直接从您最初链接的页面中获取的颜色。

Similar functions can be used for your lower and diagonal panels. 类似的功能可用于下部和对角线面板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM