R: using corrplot to visualize two variables (e.g., correlation and p-value) using the size and colour of the circles

ε祈祈猫儿з 提交于 2021-02-19 02:24:28

问题


I am trying to recreate someone's image using corrplot. This is the original image I am trying to re-create:

I use the following R-code:

corrplot(as.matrix(rgs), 
         method="circle", 
         type="upper", 
         col=brewer.pal(n=8, name="PuOr"), 
         tl.col="black", 
         tl.srt=45, 
         p.mat = as.matrix(pvalues), 
         sig.level = 0.05, 
         insig = "blank")

Which gives me this:

The problem I have is that the colour as well as the size of the circles in my plot are based on the correlations, but in the original image above the colour of the circles is based on the correlation while the size of the circles is based on the p-values. I have the p-values in a different data frame called pvalues (I actually use that in the code above to determine which circles should be shown and which shouldn't in the bottom 3 lines). My question is: how can I make the colour and size be dependent on two different variables like they did in the original image? Is that even possible using corrplot?


回答1:


What you want does not seem to be possible with corrplot, unless you hack it a bit. I simply added a new parameter size_vector which is used when drawing the circles. See https://github.com/johannes-titz/corrplot/commit/9362f6a7c2fda794b5ef8895b77f0b2ff979092a for the changed lines.

# install the hacked version
devtools::install_github("johannes-titz/corrplot@size_parameter")
library(corrplot)
data(mtcars)
M <- cor(mtcars)
# get p values
p_vals_mat <- cor.mtest(mtcars)$p
corrplot(M, size_vector = 1-as.numeric(p_vals_mat))

Note that I used 1-p for the size (small p-values produce large circles). You can use any value between 0 and 1 for the size.

Further note that in the original figure, the relationship between the p-value and the circle size is non-linear. So you might want to use some transformation that comes close to this relationship.

In any case I would actually advise to not use such figures. p-values are problematic on their own, but plotting them with some kind of transformation does not make much sense to me. The size of the correlation is likely the most important information and the plot does not reflect this. This has some potential for confusions.

PS: I did not bother to add a legend, but this should not be too difficult to do with legend.

A small update: The transformation of the p-values might be something like this:

transform_p <- function(x) {
  y <- 0.91 - (0.82) * (1 - exp(-3.82 * x))
  y
}

Which will slightly change the size of the circles:

corrplot(M, size_vector = as.numeric(transform_p((p_vals_mat))))

Again, I do not recommend it, but it should be a bit closer to the original figure.

If you just want the upper triangular, only pass the p-values of the upper triangular:

upper_tri <- p_vals_mat[upper.tri(p_vals_mat, diag = T)]
corrplot(M, size_vector = transform_p(upper_tri), type = "upper")




回答2:


Maybe this person used ggcor, which is a package under development. If you do:

install.packages("devtools")
devtools::install_github("houyunhuang/ggcor")

library(ggplot2)
library(ggcor)

quickcor(fortify_cor(mtcars,cor.test = T), type = "upper") + 
  geom_circle2(aes(fill = r,r0 = p.value))

You can have the size varying with the p value

fortify_cor(mtcars,cor.test = T)

creates a table of correlation, with the p values

# A tibble: 121 x 8
   .row.names .col.names      r  p.value upper.ci lower.ci .row.id .col.id
 * <chr>      <chr>       <dbl>    <dbl>    <dbl>    <dbl>   <int>   <int>
 1 mpg        mpg         1     0.          1       1           11       1
 2 cyl        mpg        -0.852 6.11e-10   -0.716  -0.926       10       1
 3 disp       mpg        -0.848 9.38e-10   -0.708  -0.923        9       1
 4 hp         mpg        -0.776 1.79e- 7   -0.586  -0.885        8       1
 5 drat       mpg         0.681 1.78e- 5    0.832   0.436        7       1
 6 wt         mpg        -0.868 1.29e-10   -0.744  -0.934        6       1
 7 qsec       mpg         0.419 1.71e- 2    0.670   0.0820       5       1
 8 vs         mpg         0.664 3.42e- 5    0.822   0.410        4       1
 9 am         mpg         0.600 2.85e- 4    0.784   0.318        3       1
10 gear       mpg         0.480 5.40e- 3    0.710   0.158        2       1


来源:https://stackoverflow.com/questions/60410366/r-using-corrplot-to-visualize-two-variables-e-g-correlation-and-p-value-usi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!