Announcement

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

  • Changing bar labels on hbar

    I want to change the labels on my bars to match the variable labels rather than the variable name (i.e., agree_1). How can I do this without manually editing it on the pop up window?

    My code: graph hbar (mean) agree_1 (mean) agree_2 (mean) agree_3 (mean) agree_4 (mean) agree_5 (mean) agree_6 (mean) agree_7

  • #2
    No data example here, so I invented one.

    I may well be missing something simple, or something subtle but documented, but

    1. statplot from SSC will do this, at the cost of a one-colour display.

    2. Otherwise this was the best I could think up:


    Code:
    clear
    set obs 100
    set seed 314159 
    forval j = 1/3  { 
        gen agree_`j' = runiformint(0, `j' + 2) 
        label var agree_`j' "text for `j'"
    }
    
    * you start here, changing 3 to 7
    
    gen which = _n in 1/3 
    
    forval j = 1/3 { 
        su agree_`j', meanonly 
        gen mean`j' = r(mean) in `j'
        label def which `j' "`: var label agree_`j''", modify 
    }
    
    label val which which 
    
    graph hbar (asis) mean?, over(which) nofill legend(off)

    Click image for larger version

Name:	hbarwithvarlabels.png
Views:	1
Size:	20.6 KB
ID:	1761530

    Comment


    • #3
      Thank Nick.

      However, neither of these solutions work because I do not have an "over". As I specified in my example code, there are 7 different variables (dummy 0/1). That is, I am plotting on 1 bar graph 7 different variables and I want to change their labels.

      Comment


      • #4
        Not so; the point of the longer code in #2 is that you create a which -- a new variable which does what you want it to do.

        Also, as said, the principle holds for 7 as easily as 3.

        Did you see the comment

        * you start here, changing 3 to 7 ?

        Also, as said, it may be easier to convince you if you give a real or at least realistic data example.

        The variable labels shown in the graphs in #1 are just so ridiculously long for graphical purposes that at some point you will need to type in shorter labels yourself. The only alternative -- which surely does not appeal -- is to remove the variable labels altogether.
        Last edited by Nick Cox; 16 Aug 2024, 03:13.

        Comment


        • #5
          Also, you evidently didn't try statplot, which doesn't require any extra variables, just that it be installed.

          Comment


          • #6
            Sorry about this comment

            The variable labels shown in the graphs in #1 are just so ridiculously long for graphical purposes that at some point you will need to type in shorter labels yourself. The only alternative -- which surely does not appeal -- is to remove the variable labels altogether.
            which is utterly irrelevant to this thread, but was a thought about another question from someone else.

            Comment

            Working...
            X