How to build a correlogram in R on top of the correlation matrix?
I followed the instructions on this website from STHDA for plotting correlation matrices and correlograms in R. The website and examples are really good. However, I would like to plot the top of the correlogram over the top of the correlation matrix.
Here's the code:
library(PerformanceAnalytics)
chart.Correlation(mtcars, histogram=TRUE, pch=19)
This should give me a correlation matrix using scatterplots along with a histogram that I would like to maintain. But for the top of the plot, I would like to get the correlogram derived from this code:
library(corrplot)
corrplot(cor(mtcars), type="upper", order="hclust", tl.col="black", tl.srt=45)
The obvious way to do this is to export all the graphics as pdf and then work with Inkscape, but it would be better if I could get this straight from R. Is there any possible way to do this?
Thank!
source to share
The trick for using panel functions pairs
is in help(pairs)
:
The panel function should not try to start a new plot, but simply draw in the given coordinate system: thus, "plot" and "boxplot" are not panel functions.
So, you should use the functions of adding graphic objects, such as points
, lines
, polygon
or perhaps (if available) plot(..., add=TRUE)
, but not a direct schedule. What you suggested in your comment (s SpatialPolygons
) might have worked with some nudge if you were actually trying to build it on the device firmware just returning it from the plotting function.
In my example below, I am indeed "creating a new plot", but I cheat (based on this SO post) by adding a second plot on top of the existing one. I do this to reduce the unnecessary scale / shift, which will still not be perfect since you feel like you want a โperfect circleโ, which can really only be guaranteed with asp=1
(aspect ratio is fixed at 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 - through objective visualization - by playing with the radius. The colors I took directly from the page you linked originally.
Similar functions can be used for your bottom and diagonal panels.
source to share