自定义 R 中输出“corrplot”或“corrgram”的两侧和顶部显示的变量

Customizing what variables are shown on the sides and on the top of the outputs `corrplot` or `corrgram` in R

假设我有以下数据:

> print(data)
                 date          gdp unemp_rate   cpi_index   rpi_index         var1         var2         var3        var4
1  8/31/2009 23:00:00  0.002000575        0.0 0.006539081 0.008466604  0.041601305  0.193230747  0.002260496 0.016428674
2   12/1/2009 0:00:00  0.003890642        0.0 0.007278347 0.011660448  0.012048193  0.022703903  0.003004489 0.015541372
3    3/1/2010 0:00:00  0.005088272        0.2 0.007439852 0.011065007  0.028750000 -0.222946928  0.002789741 0.015225019
4  5/31/2010 23:00:00  0.009771946       -0.1 0.012874890 0.019151847  0.015448707  0.137959240  0.000843318 0.003532365
5  8/31/2010 23:00:00  0.006467518       -0.1 0.002928654 0.004474273  0.016524217  0.119414245  0.001498776 0.006978383
6   12/1/2010 0:00:00  0.000247441        0.1 0.010128833 0.011135857 -0.021860987 -0.098281638 -0.002076772 0.013506623
7    3/1/2011 0:00:00  0.005362386       -0.1 0.014669842 0.017180617 -0.008997135 -0.104039862  0.000737306 0.005057618
8  5/31/2011 23:00:00  0.002284132        0.1 0.015393251 0.017323517 -0.003816573  0.108217236  0.001119267 0.006603190
9  8/31/2011 23:00:00  0.006963250        0.4 0.006089083 0.005534270  0.019330121  0.191878865  0.001567801 0.006242028
10  12/1/2011 0:00:00 -0.000147759        0.1 0.009548705 0.010160881 -0.010990888 -0.079442157  0.002012014 0.010126316
11   3/1/2012 0:00:00  0.000677327       -0.2 0.003463403 0.004191115 -0.000230322 -0.091365159  0.004426378 0.009523975
12 5/31/2012 23:00:00 -0.001779548       -0.2 0.008184866 0.010851419  0.012325059  0.013284528  0.010746497 0.013690636
13 8/31/2012 23:00:00  0.008329224       -0.1 0.002730592 0.003715937  0.017636684  0.072261170  0.001646379 0.018186905
14  12/1/2012 0:00:00 -0.003377040       -0.1 0.012079435 0.011929247 -0.006708783 -0.005000292  0.000966773 0.012595370
15   3/1/2013 0:00:00  0.005957449        0.0 0.004513875 0.005691057 -0.000787978 -0.137470909  0.000978465 0.015526088
16 5/31/2013 23:00:00  0.006427064        0.0 0.007228126 0.009296686  0.018419422  0.225735629  0.001693297 0.014078954
17 8/31/2013 23:00:00  0.007166400       -0.2 0.003024506 0.004805767  0.024889381  0.189354653  0.002163410 0.012134669
18  12/1/2013 0:00:00  0.004061822       -0.4 0.006102001 0.006377043  0.011171074  0.039039948  0.004515371 0.011188655
19   3/1/2014 0:00:00  0.006772674       -0.4 0.000928235 0.005544554  0.022735763 -0.085462281  0.004334033 0.021969445
20 5/31/2014 23:00:00  0.007517419       -0.5 0.007057474 0.008270973  0.039503209  0.093873476  0.004611893 0.015191039
21 8/31/2014 23:00:00  0.006551699       -0.3 0.000405809 0.003515625  0.039508032  0.085234886  0.004022014 0.011791335

我想创建一个相关矩阵/热图,其中左侧有 var1、var2、var3 和 var4,同时有 gdp、unemp_rate、cpi_index 和 rpi_index 在上面。

我已经用 Excel:

勾勒出了我的意思

我曾尝试使用corrplotcorrgram等包来构造cprrelation矩阵,但到目前为止都没有成功。我不需要输出与 Excel 草图完全一样 - 我只需要它以便在左侧有 var1、var2、var3 和 var4,同时具有 gdp、unemp_rate、cpi_index 和 rpi_index 在顶部。它不一定是 corrplotcorrgram - 任何其他可以获得所需输出的包都很好。

如有任何帮助,我们将不胜感激。

提前致谢!

这里是 dput(data) 如果你想把它放在 R 中。

> dput(data)
structure(list(date = structure(c(16L, 1L, 6L, 11L, 17L, 2L, 
7L, 12L, 18L, 3L, 8L, 13L, 19L, 4L, 9L, 14L, 20L, 5L, 10L, 15L, 
21L), .Label = c("12/1/2009 0:00:00", "12/1/2010 0:00:00", "12/1/2011 0:00:00", 
"12/1/2012 0:00:00", "12/1/2013 0:00:00", "3/1/2010 0:00:00", 
"3/1/2011 0:00:00", "3/1/2012 0:00:00", "3/1/2013 0:00:00", "3/1/2014 0:00:00", 
"5/31/2010 23:00:00", "5/31/2011 23:00:00", "5/31/2012 23:00:00", 
"5/31/2013 23:00:00", "5/31/2014 23:00:00", "8/31/2009 23:00:00", 
"8/31/2010 23:00:00", "8/31/2011 23:00:00", "8/31/2012 23:00:00", 
"8/31/2013 23:00:00", "8/31/2014 23:00:00"), class = "factor"), 
    gdp = c(0.002000575, 0.003890642, 0.005088272, 0.009771946, 
    0.006467518, 0.000247441, 0.005362386, 0.002284132, 0.00696325, 
    -0.000147759, 0.000677327, -0.001779548, 0.008329224, -0.00337704, 
    0.005957449, 0.006427064, 0.0071664, 0.004061822, 0.006772674, 
    0.007517419, 0.006551699), unemp_rate = c(0, 0, 0.2, -0.1, 
    -0.1, 0.1, -0.1, 0.1, 0.4, 0.1, -0.2, -0.2, -0.1, -0.1, 0, 
    0, -0.2, -0.4, -0.4, -0.5, -0.3), cpi_index = c(0.006539081, 
    0.007278347, 0.007439852, 0.01287489, 0.002928654, 0.010128833, 
    0.014669842, 0.015393251, 0.006089083, 0.009548705, 0.003463403, 
    0.008184866, 0.002730592, 0.012079435, 0.004513875, 0.007228126, 
    0.003024506, 0.006102001, 0.000928235, 0.007057474, 0.000405809
    ), rpi_index = c(0.008466604, 0.011660448, 0.011065007, 0.019151847, 
    0.004474273, 0.011135857, 0.017180617, 0.017323517, 0.00553427, 
    0.010160881, 0.004191115, 0.010851419, 0.003715937, 0.011929247, 
    0.005691057, 0.009296686, 0.004805767, 0.006377043, 0.005544554, 
    0.008270973, 0.003515625), var1 = c(0.041601305, 0.012048193, 
    0.02875, 0.015448707, 0.016524217, -0.021860987, -0.008997135, 
    -0.003816573, 0.019330121, -0.010990888, -0.000230322, 0.012325059, 
    0.017636684, -0.006708783, -0.000787978, 0.018419422, 0.024889381, 
    0.011171074, 0.022735763, 0.039503209, 0.039508032), var2 = c(0.193230747, 
    0.022703903, -0.222946928, 0.13795924, 0.119414245, -0.098281638, 
    -0.104039862, 0.108217236, 0.191878865, -0.079442157, -0.091365159, 
    0.013284528, 0.07226117, -0.005000292, -0.137470909, 0.225735629, 
    0.189354653, 0.039039948, -0.085462281, 0.093873476, 0.085234886
    ), var3 = c(0.002260496, 0.003004489, 0.002789741, 0.000843318, 
    0.001498776, -0.002076772, 0.000737306, 0.001119267, 0.001567801, 
    0.002012014, 0.004426378, 0.010746497, 0.001646379, 0.000966773, 
    0.000978465, 0.001693297, 0.00216341, 0.004515371, 0.004334033, 
    0.004611893, 0.004022014), var4 = c(0.016428674, 0.015541372, 
    0.015225019, 0.003532365, 0.006978383, 0.013506623, 0.005057618, 
    0.00660319, 0.006242028, 0.010126316, 0.009523975, 0.013690636, 
    0.018186905, 0.01259537, 0.015526088, 0.014078954, 0.012134669, 
    0.011188655, 0.021969445, 0.015191039, 0.011791335)), .Names = c("date", 
"gdp", "unemp_rate", "cpi_index", "rpi_index", "var1", "var2", 
"var3", "var4"), class = "data.frame", row.names = c(NA, -21L
))

您可以尝试 gplots 包中的 heatmap.2 函数,我喜欢它用于热图,它会给出与您想要的图表非常相似的东西(我四舍五入到第二个数字的示例下面。使用任意多的数字):

最初的一些数据操作:

mycor <- cor(df[-1])
mycor <- round(mycor[5:8,1:4], 2)
mycor

#the data to plot
> mycor
       gdp unemp_rate cpi_index rpi_index
var1  0.53      -0.31     -0.54     -0.39
var2  0.33      -0.03     -0.08     -0.10
var3 -0.18      -0.49     -0.31     -0.23
var4 -0.04      -0.29     -0.51     -0.45

对于情节:

#libraries needed
library(gplots)
library(RColorBrewer)
#create the colours you need. In your case red, white and again red. 
#You can specify any combination you want.
#If you want to intensify white try the below with 
#c('c('red','white','white','red') and see what happens 
my_palette <- colorRampPalette(c('red','white','red'))

#use the function below to plot the heatmap according to mycor table
heatmap.2(mycor, cellnote= mycor, main='Correlation', notecol='black', 
          density.info='none', trace='none', col=my_palette, dendrogram='none',
          Colv='NA', margin=c(10,6))

这是一个非常易于使用的功能,您可以轻松指定所需的颜色,并且有很多参数可以用来更改内容,以防您想要以不同的方式进行更改。检查 ?heatmap.2