Announcement

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

  • Doubt with heatplot (oder yaxis)

    Greetings,

    Good start of the year colleagues. I am trying to make a heat map. I have 4 variables: 1 string that has the days of the week, 1 categorical that I use to sort the days of the week from Monday to Sunday, another one that where I have the hours of the day and the last one is variable of my interest to graph.

    I am using the following code for graphing

    #delimit ;
    heatplot atropellamiento dia hora,
    yscale(noline reverse)
    ytitle("")
    xtitle("")
    ylabel(, nogrid labsize(*0.9) angle(0))
    xlabel(0(1)24, labsize(*0.6) nogrid)
    color(inferno, reverse)
    p(lcolor(black%10) lwidth(*0.15))
    cuts(0(10)100)
    leg(off)
    ;
    #delimit cr

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str10 dia byte hora int choquecarro float dia_num
    "Lunes"       0  767 1
    "Lunes"       1  473 1
    "Lunes"       2  365 1
    "Lunes"       3  258 1
    "Lunes"       4  221 1
    "Lunes"       5  244 1
    "Lunes"       6  564 1
    "Lunes"       7 1698 1
    "Lunes"       8 2333 1
    "Lunes"       9 2111 1
    "Lunes"      10 1819 1
    "Lunes"      11 1690 1
    "Lunes"      12 1930 1
    "Lunes"      13 1960 1
    "Lunes"      14 2473 1
    "Lunes"      15 2351 1
    "Lunes"      16 2250 1
    "Lunes"      17 2150 1
    "Lunes"      18 2092 1
    "Lunes"      19 1946 1
    "Lunes"      20 1639 1
    "Lunes"      21 1100 1
    "Lunes"      22  784 1
    "Lunes"      23  585 1
    "Martes"      0  453 2
    "Martes"      1  260 2
    "Martes"      2  164 2
    "Martes"      3  127 2
    "Martes"      4  119 2
    "Martes"      5  156 2
    "Martes"      6  573 2
    "Martes"      7 1646 2
    "Martes"      8 2641 2
    "Martes"      9 2387 2
    "Martes"     10 1972 2
    "Martes"     11 1813 2
    "Martes"     12 1943 2
    "Martes"     13 1939 2
    "Martes"     14 2435 2
    "Martes"     15 2301 2
    "Martes"     16 2158 2
    "Martes"     17 2093 2
    "Martes"     18 2230 2
    "Martes"     19 2024 2
    "Martes"     20 1657 2
    "Martes"     21 1121 2
    "Martes"     22  784 2
    "Martes"     23  551 2
    "Miércoles"  0  598 3
    "Miércoles"  1  291 3
    "Miércoles"  2  188 3
    "Miércoles"  3  156 3
    "Miércoles"  4  144 3
    "Miércoles"  5  167 3
    "Miércoles"  6  552 3
    "Miércoles"  7 1775 3
    "Miércoles"  8 2615 3
    "Miércoles"  9 2086 3
    "Miércoles" 10 1902 3
    "Miércoles" 11 1710 3
    "Miércoles" 12 1850 3
    "Miércoles" 13 1990 3
    "Miércoles" 14 2333 3
    "Miércoles" 15 2323 3
    "Miércoles" 16 2097 3
    "Miércoles" 17 2128 3
    "Miércoles" 18 2171 3
    "Miércoles" 19 2027 3
    "Miércoles" 20 1680 3
    "Miércoles" 21 1119 3
    "Miércoles" 22  827 3
    "Miércoles" 23  609 3
    "Jueves"      0  524 4
    "Jueves"      1  320 4
    "Jueves"      2  245 4
    "Jueves"      3  147 4
    "Jueves"      4  142 4
    "Jueves"      5  200 4
    "Jueves"      6  521 4
    "Jueves"      7 1671 4
    "Jueves"      8 2415 4
    "Jueves"      9 2062 4
    "Jueves"     10 1663 4
    "Jueves"     11 1666 4
    "Jueves"     12 1803 4
    "Jueves"     13 1815 4
    "Jueves"     14 2353 4
    "Jueves"     15 2393 4
    "Jueves"     16 2072 4
    "Jueves"     17 2061 4
    "Jueves"     18 2174 4
    "Jueves"     19 1963 4
    "Jueves"     20 1808 4
    "Jueves"     21 1149 4
    "Jueves"     22  896 4
    "Jueves"     23  729 4
    "Viernes"     0  697 5
    "Viernes"     1  441 5
    "Viernes"     2  342 5
    "Viernes"     3  272 5
    end
    The doubt that I have is how I can force so that in the yaxis and it appears ordered from Monday to Sunday. I tried to use the numeric variable so that it would be ordered and then label it with the names of the days of the week but the graph gets out of order.

    Greetings
    Attached Files

  • #2
    The graph you show is using alphabetical order given a string variable. My guess is that you need to define a numeric variable with value labels.

    Comment


    • #3
      Thanks Nick Cox, I tried to use a numeric variable (dia_num) but the whole graph is out of sync. I don't know if there is a way to force the y-axis so that the days of the week appear as we usually use them.
      Attached Files

      Comment


      • #4
        Code:
        label define yaxis 1 Lunes … 7 Domingo
        (fill in as desired) and then use encode to get a numeric version.

        You may need
        ysc(reverse) too.

        Comment


        • #5
          Thanks Nick Cox, I did what you recommended but I don't get what I want to replicate. My problem is in the y-axis and I don't know if the x-axis will also give me problems. But first I would like to know if there is a way to fix the y-axis.
          Attached Files

          Comment


          • #6
            I don’t follow what you think is still wrong. The days seem to be in correct order now.

            Comment


            • #7
              I apologize, the last graph I am attaching is the one I am trying to replicate and that is the result I want to obtain.

              Comment


              • #8
                I don’t have access to a computer right now. But anyone else interested in the question surely needs to see an update on your code and your data. Otherwise this is a case of my code doesn’t do what I want.

                Comment


                • #9
                  There are problems at various levels here. Let's start with my faults. I was away from my computer when I tried to engage with this and missed one helpful point through inattention. Sorry about that.

                  I don't use heatplots and I think they are way oversold. The idea is easy enough to understand: intensity of colour of each tile encodes magnitude. So far, so good, but the nub of the matter is how easily and effectively can readers decode colour as magnitude, and see general patterns and particular differences fully and correctly. I don't think I am good at that at all, which leaves open entirely other people being excellent at it.

                  It's for that reason that I generally prefer quite different kinds of graph. In some situations in which heatplots are used, it is not clear to me that any kind of graph would work well.

                  If you're working with heatplots in Stata, then Ben Jann's heatplot (from SSC, as you are asked to explain) is to my knowledge certainly the command to use.

                  Getting day of the week in the desired order seems to be a minor problem and I still don't understand why you couldn't manage it. But I missed that you already have a variable with day number, so that is easier than I said.

                  #1 refers to a variable atropellamiento but it is not in the data example, which features choquecarro

                  I don't read Spanish, but I think we are counting traffic accidents of various kinds by day of the week and hour of the day. I used labmask (Stata Journal) to label the numeric day variable and then fabplot (Stata Journal) to draw the line graphs.

                  The point to that is that the four days shown have very similar traces, so we need to check for differences too.

                  Clearly, data for all seven days are more of a challenge, but also likely to be more varied, as weekend patterns could be quite different.

                  Code:
                  * Example generated by -dataex-. For more info, type help dataex
                  clear
                  input str10 dia byte hora int choquecarro float dia_num
                  "Lunes"       0  767 1
                  "Lunes"       1  473 1
                  "Lunes"       2  365 1
                  "Lunes"       3  258 1
                  "Lunes"       4  221 1
                  "Lunes"       5  244 1
                  "Lunes"       6  564 1
                  "Lunes"       7 1698 1
                  "Lunes"       8 2333 1
                  "Lunes"       9 2111 1
                  "Lunes"      10 1819 1
                  "Lunes"      11 1690 1
                  "Lunes"      12 1930 1
                  "Lunes"      13 1960 1
                  "Lunes"      14 2473 1
                  "Lunes"      15 2351 1
                  "Lunes"      16 2250 1
                  "Lunes"      17 2150 1
                  "Lunes"      18 2092 1
                  "Lunes"      19 1946 1
                  "Lunes"      20 1639 1
                  "Lunes"      21 1100 1
                  "Lunes"      22  784 1
                  "Lunes"      23  585 1
                  "Martes"      0  453 2
                  "Martes"      1  260 2
                  "Martes"      2  164 2
                  "Martes"      3  127 2
                  "Martes"      4  119 2
                  "Martes"      5  156 2
                  "Martes"      6  573 2
                  "Martes"      7 1646 2
                  "Martes"      8 2641 2
                  "Martes"      9 2387 2
                  "Martes"     10 1972 2
                  "Martes"     11 1813 2
                  "Martes"     12 1943 2
                  "Martes"     13 1939 2
                  "Martes"     14 2435 2
                  "Martes"     15 2301 2
                  "Martes"     16 2158 2
                  "Martes"     17 2093 2
                  "Martes"     18 2230 2
                  "Martes"     19 2024 2
                  "Martes"     20 1657 2
                  "Martes"     21 1121 2
                  "Martes"     22  784 2
                  "Martes"     23  551 2
                  "Miércoles"  0  598 3
                  "Miércoles"  1  291 3
                  "Miércoles"  2  188 3
                  "Miércoles"  3  156 3
                  "Miércoles"  4  144 3
                  "Miércoles"  5  167 3
                  "Miércoles"  6  552 3
                  "Miércoles"  7 1775 3
                  "Miércoles"  8 2615 3
                  "Miércoles"  9 2086 3
                  "Miércoles" 10 1902 3
                  "Miércoles" 11 1710 3
                  "Miércoles" 12 1850 3
                  "Miércoles" 13 1990 3
                  "Miércoles" 14 2333 3
                  "Miércoles" 15 2323 3
                  "Miércoles" 16 2097 3
                  "Miércoles" 17 2128 3
                  "Miércoles" 18 2171 3
                  "Miércoles" 19 2027 3
                  "Miércoles" 20 1680 3
                  "Miércoles" 21 1119 3
                  "Miércoles" 22  827 3
                  "Miércoles" 23  609 3
                  "Jueves"      0  524 4
                  "Jueves"      1  320 4
                  "Jueves"      2  245 4
                  "Jueves"      3  147 4
                  "Jueves"      4  142 4
                  "Jueves"      5  200 4
                  "Jueves"      6  521 4
                  "Jueves"      7 1671 4
                  "Jueves"      8 2415 4
                  "Jueves"      9 2062 4
                  "Jueves"     10 1663 4
                  "Jueves"     11 1666 4
                  "Jueves"     12 1803 4
                  "Jueves"     13 1815 4
                  "Jueves"     14 2353 4
                  "Jueves"     15 2393 4
                  "Jueves"     16 2072 4
                  "Jueves"     17 2061 4
                  "Jueves"     18 2174 4
                  "Jueves"     19 1963 4
                  "Jueves"     20 1808 4
                  "Jueves"     21 1149 4
                  "Jueves"     22  896 4
                  "Jueves"     23  729 4
                  end
                  
                  labmask dia_num, values(dia)
                  
                  line choquecarro hora, by(dia_num) xla(0(3)21 23)
                  
                  fabplot line choquecarro hora, by(dia_num) xla(0(3)21 23)


                  Click image for larger version

Name:	not_heatplot.png
Views:	1
Size:	109.0 KB
ID:	1770296

                  Last edited by Nick Cox; 05 Jan 2025, 05:23.

                  Comment


                  • #10
                    Thanks Nick Cox , as always you were right. The problem was becoming a problem of my code not doing what I want. Reading all afternoon I found a post by Fahad Mirza (all credit to him) and it just explained what I needed. My problem was on how to shift the x-axis to fit the grid and by adding your last recommendation I was able to replicate that graph. I leave you the code in case someone finds it useful.
                    Thanks a lot for the tips Nick

                    Code:
                     quietly: summarize hora
                     return list
                     local count = `r(max)' + 1
                    
                     
                     local xlab
                        forval i=1/`count'{
                            local xlab "`xlab' `=`i'-0.5' `" "`:lab (hora) `i''" "'"
                        }
                        
                        #delimit ;
                        
                        heatplot atropellamiento i.dia_num i.hora
                        , 
                     
                        xsize(33.87)
                        ysize(19.05)
                        
                        xscale(lc(none))
                     
                        ytitle("")                
                        xtitle("")
                        cuts(@min 30 60 90 120 @max)
                        
                        xlabel(`xlab', 
                         nogrid 
                         labsize(1.75) 
                         labcolor(gs5)
                         tlength(*0.5)
                         tlcolor(black)
                        )
                        
                        ylabel(, 
                         angle(horizontal) 
                         labgap(-0.5)
                         labsize(2) 
                         noticks 
                         labcolor(gs5) 
                         nogrid
                        )
                        
                        xscale(extend)
                        
                        yscale(
                         noline 
                         reverse
                        ) 
                        
                       graphregion(margin(l=1 r=2)) 
                       plotregion(margin(b=0 t=0))
                        
                       p(
                        lcolor(white) 
                        lwidth(0.1) 
                        lalign(center)
                       )
                       
                        color("`colors'") 
                        
                        scheme(plotplain)
                            
                        legend(subtitle("{bf}Atropellos", 
                            span 
                            size(1.75)
                           ) 
                         pos(6) 
                         ring(1) 
                         rows(1) 
                         keygap(0.5) 
                         colgap(2) 
                         size(1.5) 
                         symysize(1) 
                         symxsize(10) 
                         order(1 "{bf:        < 30}" 2 "{bf:       <= 60}" 3 "{bf:       <= 90}" 4 "{bf:       <= 120}" 5 "{bf:        > 120}") 
                         stack
                         
                        )
                        
                        
                        title("{bf}Atropellos por tránsito terrestre durante 2023", 
                         style(large) 
                         size(*1.3) 
                         color(navy)
                         pos(12) 
                         
                        
                        ) 
                        
                        subtitle("(Por día y hora de ocurrencia | Zonas urbanas y suburbanas )", 
                        size(*0.8) 
                        color(gs0%50)
                        pos(12) 
                             
                        )
                        
                        note("Fuente: elaboración propia con datos de INEGI.", 
                        size(*0.5) 
                        pos(5) 
                        color(gs8%100)
                        )
                        
                        addplot(      
                            scatter dia_num hora, 
                            color(%0)
                            xaxis(2)
                            xtitle("", axis(2)) 
                            xscale(lc(none) axis(2))
                            xlabel(
                                1/`count', 
                                valuelabels 
                                 tlength(*0.5)
                                tlcolor(black)
                                labsize(1.75) 
                                labcolor(gs5) 
                                nogrid 
                                axis(2)
                                
                            )
                        )
                            
                        ;
                        
                        #delimit cr

                    Comment


                    • #11
                      Thanks for the partial closure!

                      To help other heatplot enthusiasts further, I suggest that you

                      1. give a link to the post by Fahad Mirza

                      2. make your code a minimal reproducible example: to save beginners any trouble, I note that it wouldn't work with your data example in #1 as the main variable being plotted is not in the data example.

                      Comment


                      • #12
                        I would be interested to see the resulting heat plot and the complete data example.

                        Code:
                        dataex atropellamiento choquecarro dia_num dia hora, count(168)
                        or whatever else yields the complete 7 * 24 observations. Then we could compare the heat plot, a series of line plots and whatever other visualizations people suggest.

                        Comment


                        • #13
                          Originally posted by Nick Cox View Post
                          Thanks for the partial closure!

                          To help other heatplot enthusiasts further, I suggest that you

                          1. give a link to the post by Fahad Mirza
                          It's been a few days since the OP posted, but I think he was referring to the following post on Medium by Fahad Mirza: https://medium.com/the-stata-guide/g...h-c6b2bc65f760. This progressed from this Statalist post: https://www.statalist.org/forums/for...on-replication.

                          Like Nick, I find heat plots challenging to interpret unless there is a clear pattern of increasing or decreasing color intensity. Otherwise, they often appear as a random mix of colors.

                          Comment


                          • #14
                            A while back, graphics discussions on social media often mentioned

                            Aha! [I see structure and pattern and interesting detail now!]

                            versus

                            Wow! [How did you do that? It looks impressive!]

                            Aha! surely beats Wow! every time. If people are admiring your graph as a technical accomplishment, but not appreciating what it shows, that's the wrong way round.

                            No doubt someone did it first, but I often wanted to add

                            Huh? [What I am supposed to see here?]

                            to the miniature taxonomy of responses.

                            Comment

                            Working...
                            X