Announcement

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

  • Configure legend in axis labels in a bar chart

    Dear All,

    I am looking for a way to control the legend and axis labels to result in a particular illustration. Consider the following example.

    Code:
    clear all
    
    input day m9 m12 m17
    20001 10 20 5
    20002 10 15 7
    20003 12 18 0
    end
    
    format day %td
    
    label variable m9 "Damaged beyond restoration"
    label variable m12 "Lost or stolen"
    label variable m17 "Status unknown"
    
    // ---- end of data setup -----------------------------------------------------
    
    graph bar m*, over(day) stack legend(rows(1)) xsize(3) ysize(2) name(G1) title(G1)
    graph bar m*, over(day) stack legend(rows(1)) xsize(3) ysize(2) name(G2) title(G2) nolabel
    
    graph bar m*, over(day, label(format("%td"))) stack legend(rows(1)) xsize(3) ysize(2) title(G3) ///
      legend( label(1 "`:variable label m9'") ///
      label(2 "`:variable label m12'") ///
      label(3 "`:variable label m17'")) name(G3)
    
    generate date=string(day,"%td") // workaround to force a particular label
    
    graph bar m*, over(date) stack legend(rows(1)) xsize(3) ysize(2) title(G4) ///
      legend( label(1 "`:variable label m9'") ///
      label(2 "`:variable label m12'") ///
      label(3 "`:variable label m17'")) name(G4)
    
    graph combine G1 G2 G3 G4, cols(2) ysize(6) xsize(8) scale(0.5)
    
    // ---- end of example --------------------------------------------------------
    Click image for larger version

Name:	outflows.png
Views:	1
Size:	60.6 KB
ID:	1427321



    The number and numbering of m* variables can vary (I can't assume they always start from 1, or that there are no gaps in numbering, etc). If that matters, they are resulting from a reshape command.

    The graph that I want is shown in G4. A straightforward graphing (panel G1) has two problems:
    1) legend entries are not variable labels.
    2) axis values do not obey the format of the variable.

    (G2 and G3 show some intermediate steps)

    Following the [excellent] book of Mitchell (ISBN 9781881228851) , I have tried the nolabel option (panel G2) to avoid the "means of .." text. Unfortunately, there seems to be no option to force the variable labels to be put into the legend (which seems to be the most natural way, and all the other modes can be implemented from there, assigning e.g. variable names as the variable labels). I would like to avoid code-generating code (code that expands m* into a variable list, then inspects variables one after another constructing the options list, which are then passed to graph bar). This is quite similar to the recently discussed issue , but not exactly the same, as this current issue is probably simpler, and I have a feeling that there's got to be a standard option for it. Panel G3 shows the result of what would be the result of that self-assembled code. My feeling is that the graph bar uses collapse inside, which destroys the original variable labels with some statistics, hence no option will resurrect them, but wanted to ask just in case.

    Finally the panel G4 tries to compensate for ignoring the format of the value of the axis. Again, I believe that the format of the label should coincide to the original format of the variable, but no, it is ignored and some default format is used. I have tried to specify the format as part of the over option in panel G3, but it is ignored without any effect or error message. G4 relies on a totally reliable, but imho totally unnecessary workaround with generating a string variable utilizing the format of the variable, and supplying the string as the category instead.

    I am looking for a simpler way to build G4.

    Thank you, Sergiy Radyakin

  • #2
    I have the same issue. Did you figure it out?

    Comment


    • #3
      Hello Benjamin,

      no, as you can see there were no responses to this specific question, but do visit the link to a different issue mentioned in above as it contains a related discussion.

      Best, Sergiy

      Comment


      • #4
        A related post has a workaround involving using collapse first. I used preserve, then collapse, then re-assigned the labels, and finally it looks fine. Surely there's something more efficient but this seems to work!

        Comment


        • #5
          I missed this at the time, or at least did not comment. My answer -- which is either no answer at all or better -- is why use legends any way?

          A legend is at best a necessary evil, and the evil is the necessary back and forth between legend and plot. A personal slogan is Lose the legend! Kill the key! (if you can). That means using direct labelling (text on the graph next to graph elements) or axis labels.

          This uses tabplot from the Stata Journal and scheme stcolor from Stata 18. The latter is naturally marginal to the question.


          Code:
          clear all
          
          input day m9 m12 m17
          20001 10 20 5
          20002 10 15 7
          20003 12 18 0
          end
          
          format day %td
          
          label variable m9 "Damaged beyond restoration"
          label variable m12 "Lost or stolen"
          label variable m17 "Status unknown"
          
          frame put *, into(sandbox) 
          
          frame sandbox { 
              foreach v in 9 12 17 {
                  label define which `v' "`: var label m`v''", modify 
              }
              reshape long m, i(day) j(which) 
              label val which which 
              
              tabplot which day [iw=m] , xtitle("") ytitle("") subtitle("") separate(which) showval scheme(stcolor) xasis xla(, format(%td))
          }
          If this was really my problem, I would use a different date display format.
          Click image for larger version

Name:	sergiy.png
Views:	1
Size:	25.3 KB
ID:	1717615


          Comment

          Working...
          X