3

I have a dataframe with my measurements separated by days of the month, and I would like to build a correlation matrix (heatmap) between the months like the example below: enter image description here

Thank you for the help! The head of my data is:

> head(month)
          Jan         Feb          Mar          Apr          May
1 18.06474972   11.399055 -31.03085327  127.2022133 -3460.850586
2 51.03550959 461.5850525  95.19575119  243.0290451  -2083.35199
3 47.54027748 110.2497387 -74.04249191  546.8690033 -1276.771118
4 340.8937378 282.1457062  74.03128743  322.6983643 -203.3483124
5 247.5398369 30.00830674  370.5808411  416.0554199  121.7065811
6 399.5107269 922.6151428  439.3453064 -35.20425129 -1469.418518
           Jun         Jul         Aug          Sep          Oct
1  39.49685574 2622.950073 1649.752869   1238.56366  500.0726166
2  380.0546722 2552.876099 2572.219482  525.0535278  1092.828491
3  278.1513977 1118.665558  2370.05896  561.7017517  880.8520813
4  1334.320251 1658.281433 588.7766266  316.3756104   331.518219
5  681.0978546 1830.663025 436.6187897  188.4522657 -106.0038891
6 -69.24006653 1645.978577 2079.013062 -116.1453476  292.2966232
           Nov          Dec
1 -251.1712646  196.3089867
2 -2490.553711  37.59177971
3 -1460.746155  251.9935303
4  425.8812321  345.0469666
5  253.5491486 -150.1276989
6  2654.713806 -1673.095764

2 Answers 2

4

You can try something like below:

library(pheatmap)
month = data.frame(matrix(runif(120),ncol=12))
colnames(month) = format(ISOdate(2004,1:12,1),"%B")

pheatmap(cor(month),cluster_rows = FALSE, cluster_cols =FALSE)

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

While plotting correlations please don't forget about the color scale. It can get confusing to interpret the results. library(RColorBrewer); pheatmap(cor(month),cluster_rows = FALSE, cluster_cols=FALSE, color=colorRampPalette(rev(brewer.pal(n = 7, name = "RdYlBu")))(length(seq(-1, 1, 0.01))), breaks=seq(-1, 1, 0.01))
1

The corrplot package has numerous formats for displaying correlation plots. Here are a few. example(corrplot) and example(corrplot.mixed) show other possibilities.

As in the question, the second plot:

  1. uses the color maroon for +1 and a light blue for -1 using the pals package for its palettes (RColorBrewer is another possibility), and
  2. places the numeric value of the correlation on each tile

but you can change the color scheme and/or format as desired.

library(corrplot)
library(pals)

opar = par(mfcol = 1:2)
wat <- adjustcolor(watlington(16)[11:3], alpha = 0.7)
corrplot.mixed(cor(month), lower = "number", upper = "shade", upper.col = wat)
corrplot(cor(month), col = wat, cl.length = 21, 
  addCoef.col = "black", method = "shade")
par(opar)

screenshot

Note

We have used the first 5 months since the data was not provided in an easily usable form in the question but it should work the same with all 12 months.

Lines <- "          Jan         Feb          Mar          Apr          May
1 18.06474972   11.399055 -31.03085327  127.2022133 -3460.850586
2 51.03550959 461.5850525  95.19575119  243.0290451  -2083.35199
3 47.54027748 110.2497387 -74.04249191  546.8690033 -1276.771118
4 340.8937378 282.1457062  74.03128743  322.6983643 -203.3483124
5 247.5398369 30.00830674  370.5808411  416.0554199  121.7065811
6 399.5107269 922.6151428  439.3453064 -35.20425129 -1469.418518"
month <- read.table(text = Lines)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.