Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • What is the best program to generate heat maps in Stata?

    Dear Statalisters,

    I have tried hmap, heatplot and corrtable. They are all amazing programs, but I could not figure out how to obtain the results we need.

    Let me be more specific:

    I have data like this:


    Code:
     clear all
       set obs 64
       g n=int(uniform()*10)
       g x=1+int((_n-1)/8)
       g y=1+mod((_n-1),8)
       la def xlab 1 "one" 2 "two" 7 "seven" 8 "eight"
       la def ylab 3 "three" 4 "four" 5 "five" 6 "six"
       la val x xlab
       la val y ylab
       ta y x [fw=n]
       label define test 9 "good"  8 "mild"
       label values n test
       hmap x y n
       hmap x y n, sc
    Click image for larger version

Name:	Graphaa.png
Views:	1
Size:	60.6 KB
ID:	1649596





    And I need the following features:
    • Plot labels, not numbers in the variable called "n" (hmap does that)
    • Be able to specify colors. For example, if n > 7 & n <9 the color should be green, if n == 2 the color should be blue, etc.
    • Put the horizontal axis/stick at the top
    Any suggestions or tips?

    All the best,

    Tiago


  • #2
    It may be easier to program this in twoway rather than trying to figure out the options in the community-contributed commands. Here is how I would do it.

    Code:
    clear all
       set obs 64
       g n=int(uniform()*10)
       g x=1+int((_n-1)/8)
       g y=1+mod((_n-1),8)
       la def xlab 1 "one" 2 "two" 7 "seven" 8 "eight"
       la def ylab 3 "three" 4 "four" 5 "five" 6 "six"
       la val x xlab
       la val y ylab
       ta y x [fw=n]
       label define test 9 "good"  8 "mild"
       label values n test
    
    decode n, gen(lab)
    replace lab= "" if !missing(real(lab))
    gen xl= x-0.5
     quietly: summarize y
        local count = `r(max)' 
        local xlab
        forval i=1/`count'{
            local xlab "`xlab' `=`i'-0.5' `" "`:lab (x) `i''" "'"
        } 
    
    gen start= y-1
    tw (rbar start y x if n==2, horiz barw(0.9) lc(white) fc(blue)  xsc(alt) ///
    ylab(1/8, val)  xlab(`xlab')) (rbar start y x if inrange(n, 7, 9), horiz ///
    barw(0.9) lc(white) fc(green)) (rbar start y x if (!inrange(n, 7, 9)|n!=2), ///
    horiz barw(0.9) lc(white) fc(navy%40)) (scatter y xl, mc(none) ///
    mlab(lab) mlabc(black) mlabpos(0) leg(off) scheme(s1mono))
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	38.7 KB
ID:	1649732

    Comment


    • #3
      As always Andrew offers an elegant and useful solution. It is a matter of taste, I suppose, but you might consider adding the option
      Code:
      aspect(1)
      to Andrew's option list. The rectangles will look more like squares with this option.

      Comment


      • #4

        As always, thank you very much, Andrew. Extremely helpful suggestion. Very easy to adapt the final code based on your initial code. Thanks, John, for the question about the option aspect(1.0). It improves the visual appeal of the graph.

        Comment

        Working...
        X