How to create a simple heatmap in R

admin

Administrator
Staff member
I cannot for the life of me figure out how to do this, but it seems like it should be pretty simple.

I have the following data:

Code:
A   B   C

5   2   0.714972409
5   4   0.74183882
5   6   0.762162036
5   8   0.813707009
5   10  0.850703046
5   12  0.857035156
5   14  0.872640528
5   16  0.973139763
5   18  0.978478338
7   2   0.684788926
7   4   0.711897491
7   6   0.735098842
7   8   0.762857648
7   10  0.83291045
7   12  0.844075508
7   14  0.884207251
7   16  0.94815451
7   18  0.950971496
9   2   0.659268067
9   4   0.694214759
9   6   0.718271051
9   8   0.771301281
9   10  0.773598055
9   12  0.836032827
9   14  0.870630437
9   16  0.877580987
9   18  0.929563158
11  2   0.662650697
11  4   0.670933715
11  6   0.689340083
11  8   0.693320771
11  10  0.750268847
11  12  0.786759112
11  14  0.827402884
11  16  0.87493501
11  18  0.909695854
13  2   0.626547156
13  4   0.633140699
13  6   0.682464618
13  8   0.702283534
13  10  0.756709989
13  12  0.768034027
13  14  0.815414298
13  16  0.868304111
13  18  0.857143204
15  2   0.642907947
15  4   0.664392516
15  6   0.683305068
15  8   0.706409063
15  10  0.729998833
15  12  0.752115586
15  14  0.775292453
15  16  0.801494475
15  18  0.883794201
17  2   0.605458303
17  4   0.621560397
17  6   0.648538727
17  8   0.665378929
17  10  0.695163292
17  12  0.713840128
17  14  0.762558649
17  16  0.835615372
17  18  0.845147592
19  2   0.573326068
19  4   0.599060774
19  6   0.615440659
19  8   0.649463687
19  10  0.674024337
19  12  0.72427247
19  14  0.747894393
19  16  0.77787273
19  18  0.796066305
21  2   0.576442958
21  4   0.565737836
21  6   0.602169915
21  8   0.642909046
21  10  0.645458282
21  12  0.691301146
21  14  0.727324617
21  16  0.775556108
21  18  0.817496666
23  2   0.533171689
23  4   0.565956771
23  6   0.620844768
23  8   0.616694919
23  10  0.633368894
23  12  0.669562089
23  14  0.680912205
23  16  0.722211534
23  18  0.765015031
25  2   0.52308274
25  4   0.563778286
25  6   0.564340285
25  8   0.610685424
25  10  0.600061306
25  12  0.655410805
25  14  0.684528361
25  16  0.71358848
25  18  0.740716841
27  2   0.520854189
27  4   0.538261912
27  6   0.560022949
27  8   0.592117652
27  10  0.59707951
27  12  0.630212317
27  14  0.653396447
27  16  0.689859539
27  18  0.719597316
29  2   0.504173709
29  4   0.529294427
29  6   0.553966747
29  8   0.561650807
29  10  0.609853393
29  12  0.597217807
29  14  0.631877662
29  16  0.675602795
29  18  0.676468158
31  2   0.488265133
31  4   0.495376458
31  6   0.507751424
31  8   0.528459137
31  10  0.546474587
31  12  0.599001653
31  14  0.620699739
31  16  0.621521091
31  18  0.664893468
33  2   0.464415592
33  4   0.50818927
33  6   0.485845473
33  8   0.506436638
33  10  0.529943976
33  12  0.568978775
33  14  0.5816666
33  16  0.599533467
33  18  0.628195086

And I want to create a heatmap where A is the x-axis, B is the y-axis, and the color is dependent on C. I also want to be able to set the color scale (preferably shades of green) and create a legend.

I used <a href="https://martinsbioblogg.wordpress.com/2013/03/21/using-r-correlation-heatmap-with-ggplot2/" rel="nofollow">this post</a> to make a heatmap that looked very similar to the one shown there, but I don't know how to recolor it or give the legend a different title.

Thanks!