{"id":58,"date":"2011-03-06T22:36:03","date_gmt":"2011-03-06T22:36:03","guid":{"rendered":"http:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/?p=58"},"modified":"2014-04-20T17:06:17","modified_gmt":"2014-04-20T17:06:17","slug":"58","status":"publish","type":"post","link":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/?p=58","title":{"rendered":"Heatmaps in gnuplot and Stata"},"content":{"rendered":"<p>I use <code>gnuplot<\/code> and Stata to generate a heatmap representation of a square matrix containing a measure of closeness between 26 departments in a university. <code>gnuplot<\/code> is a general-purpose plotting program, and can be wheedled into doing a lot of things, but Stata&#8217;s graphics routines are also very general. Given data in <em>i, j, n<\/em> format (in blocks, that is with a blank line inserted before every change of value of <em>i<\/em>), <code>gnuplot<\/code> can generate a heatmap with code like the following:<\/p>\n<p><!--more--><\/p>\n<pre>set view map\r\nset palette rgbformulae 21,22,23\r\nset colorbox\r\nunset border\r\nset size square\r\nset ytics (\"LCS\" 26, \"LAW\" 25, \"SOC\" 24, \"PPA\" 23, \"HIS\" 22, \"ACF\" 21,  MMA\" 20, \"PER\" 19, \"ECO\" 18,  PHI\" 17, \"PS$\r\nset xtics (\"LCS\"  1, \"LAW\"  2, \"SOC\"  3, \"PPA\"  4, \"HIS\"  5, \"ACF\"  6,  MMA\"  7, \"PER\"  8, \"ECO\"  9,  PHI\" 10, \"PS$\r\n\r\nset object rectangle from screen 0,0 to screen 1,1 behind fc rgbcolor \"beige\"                                      \r\n\r\nset cbrange [0:15000]\r\nsplot [-1:27][-1:27]  \"heatmap.dat\" u 2:(27-$1):3 with image t \"\"<\/pre>\n<p>The result looks approximately like this: <img decoding=\"async\" alt=\"gnuplot heatmap\" src=\"http:\/\/teaching.sociology.ul.ie\/bhalpin\/heatmapn.png\" \/><\/p>\n<p>The palette is multi-hued, and this can be defined in a quite general manner (<code>set palette rgbformulae ....<\/code> allows one to set the red\/green\/blue levels across the value range by formulas, including user-written ones).<\/p>\n<p>In some ways it would be preferable to use Stata to generate the heatmap, since that is where I do the data manipulation. Adrian Mander&#8217;s <code>plotmatrix<\/code> add-on provides most of the necessary functionality. We need to put the information into a matrix first, and it helps to be able to define the cut-points between different colours:<\/p>\n<pre>tab row col, matcell(t)\r\nmatrix rownames t = LCS LAW SOC PPA HIS ACF MMA PER ECO PHI PSY NMI PES EPS LSC MST ARC CSI ECE MOE PHY MAE CES MAS ART PLE\r\nmatrix colnames t = LCS LAW SOC PPA HIS ACF MMA PER ECO PHI PSY NMI PES EPS LSC MST ARC CSI ECE MOE PHY MAE CES MAS ART PLE\r\nplotmatrix, mat(t) split(0 2000 4000 6000 8000 10000 12000 14000 9999999) color(emidblue) aspect(1)<\/pre>\n<p>The resulting graph looks like this:<\/p>\n<p><img decoding=\"async\" alt=\"Stata heatmap\" src=\"http:\/\/teaching.sociology.ul.ie\/bhalpin\/stataheatmap.png\" \/><\/p>\n<p>Apart from being monochrome and not having space for all the labels, the result is functionally equivalent. If you have any thoughts on either of these issues, I&#8217;d appreciate an <a href=\"mailto:brendan.halpin@ul.ie\">e-mail<\/a> or comment.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I use gnuplot and Stata to generate a heatmap representation of a square matrix containing a measure of closeness between 26 departments in a university. gnuplot is a general-purpose plotting program, and can be wheedled into doing a lot of things, but Stata&#8217;s graphics routines are also very general. Given data in i, j, n &hellip; <a href=\"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/?p=58\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Heatmaps in gnuplot and Stata<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,6,4,1],"tags":[],"_links":{"self":[{"href":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/58"}],"collection":[{"href":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=58"}],"version-history":[{"count":13,"href":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/58\/revisions"}],"predecessor-version":[{"id":293,"href":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/58\/revisions\/293"}],"wp:attachment":[{"href":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=58"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=58"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=58"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}