Announcement

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

  • Display variable label in bar chart

    Hello.

    Using the sample data set, I ran the following commands:

    . sysuse auto.dta
    . graph hbar (mean) mpg rep78, ascategory blabel(bar)

    The Y-axis displays ,,mean of varname’’ for mpg and rep78. I however would like to display ,,varlabel’’, so Mileage (mpg) and Repair Record 1978 (possibly without the term ,,mean of").

    Does someone has an answer to that?

    Many thanks,
    Aline

  • #2
    Drop -ascategory- and try the following:

    Code:
    graph hbar (mean) mpg rep78,  blabel(bar, pos(base)) legend(order(1 "`: var label mpg'" 2 "`: var label rep78'"))
    Maybe someone else can help with including -ascategory-

    Comment


    • #3
      Thanks for the reproducible example.

      Code:
      graph hbar (mean) mpg rep78, ascategory blabel(bar) yvar(relabel(1 "`: var label mpg'" 2 "`: var label rep78'"))

      Comment


      • #4
        Thanks Nick!

        Comment


        • #5
          Hello everybody,

          I was wondering whether there is an automated way to obtain the exact same outcome. I have the same problem with graph dot, which has a very similar syntax, and labels Y-axis with "mean of varname" while I´d like to display the variable label. However, I can´t implement the suggestion proposed by Nick since:

          - the number of variable I am working with is very large
          - the graph is inside a loop which produces the dot graph for several groups of variables

          I am also confused by the manual of graph dot; in the "legending_options", the description of "nolabel" states "use yvar names, not labels, in legend" . This seems to imply that labels should be the standard option, which is not the case. The same description is provided for graph bar.

          I hope you can help me!
          I thank you in advance,

          Pietro Sancassani

          Comment


          • #6
            Pietro Sancassani I have read #5 three times but I can't grasp your difficulty without a data example and the code you have tried so far.

            Comment


            • #7
              Hello Nick,
              Thanks for your reply. Here you find a reproducible example. Please note that, as mentioned before, the number of variables in each varlist is much larger in my case. Also (as in the example), the number of variables varies across locals, so I am not sure I can use the option yvar(relabel...)

              Code:
              sysuse auto, clear
              
              local varlist1 price weight
              local varlist2 mpg turn trunk
              local varlist3 rep78 gear_ratio headroom
              
              cd  //set your directory
              
              preserve
                  keep `varlist1'
                  save varlist1, replace
              restore, preserve
                  keep `varlist2'
                  save varlist2, replace
              restore, preserve
                  keep `varlist3'
                  save varlist3, replace
              restore
              
              local data "varlist1 varlist2 varlist3"
              
              foreach set in `data' {
              preserve
                  use `set', clear
                  ds
                  local var `r(varlist)'
                  graph dot `var', ascategory
                  graph save "`set'",replace
              restore
                  }
              Thanks!
              Last edited by Pietro Sancassani; 05 Jul 2019, 03:05.

              Comment


              • #8
                Thanks for your reproducible example. As you say, this is a version of your real problem. I don't understand the dancing with different datasets here. Also, nested macro calls help.

                Your code seems to boil down to


                Code:
                sysuse auto, clear
                
                local varlist1 price weight
                local varlist2 mpg turn trunk
                local varlist3 rep78 gear_ratio headroom
                
                foreach set in varlist1 varlist2 varlist3 {
                    graph dot ``set'', ascategory name(`set', replace)
                }
                although my using name() rather than graph save is to make results easier to see.

                The problem is then shown by a graph like this





                where you would rather see variable labels. I've found it easier to work with statplot (SSC) for problems like this, which restructures the data on the fly. Thus I would rewrite your code as


                Code:
                sysuse auto, clear
                
                local varlist1 price weight
                local varlist2 mpg turn trunk
                local varlist3 rep78 gear_ratio headroom
                
                foreach set in varlist1 varlist2 varlist3 {
                    statplot ``set'', recast(dot) linetype(line) lines(lcolor(gs12) lw(vthin)) name(`set', replace)
                }
                Here's a token result. Nothing to do with your question, but I've found that dotted lines don't reproduce well in some other software and always switch to thin grey continuous lines instead.

                Comment


                • #9
                  Many thanks Nick, this is definitely what I was looking for.

                  Comment


                  • #10
                    Hello everyone,

                    I am using the following code:

                    sysuse auto.dta
                    graph bar (count) rep78 trunk , over ( foreign )

                    I would like to replace the count of rep78 and count of truck with my own labeling, let's say screened participants.
                    What's the code I should use?

                    Many thanks,
                    Marios

                    Comment


                    • #11
                      Code:
                      help legend_options
                      Code:
                      graph bar (count) rep78 trunk , over (foreign) leg(order(1 "My lab 1" 2 "My lab 2"))

                      Comment


                      • #12
                        Thanks Andrew!

                        Comment


                        • #13
                          Originally posted by Nick Cox View Post
                          Thanks for the reproducible example.

                          Code:
                          graph hbar (mean) mpg rep78, ascategory blabel(bar) yvar(relabel(1 "`: var label mpg'" 2 "`: var label rep78'"))
                          Hi all, I am trying to following this advice but I got an error message 'option yvar not allowed'. Here's the code line
                          Code:
                          graph hbar var1 var2 var3, over(year, axis(off)) yvaroptions(sort(1) descending) yscale(reverse) bargap(+30) intensity(30) ascategory blabel(bar, position(inside) format(%9.1f)) yvar(relabel(1 "`: var label var1'" 2 "`: var label var2'"))
                          Any advice?

                          thanks

                          Comment


                          • #14
                            Good to be quoted, but the implied advice behind
                            Thanks for your reproducible example.
                            is not a pattern you followed.

                            After creating a reproducible example, I got this working. The problem appears to be that yvar() and yvaroptions() are one and the same, and should be called just once.

                            Code:
                            clear
                            set obs 100
                            gen year = cond(_n <= 50, 2021, 2022)
                            set seed 2803
                            forval j = 1/3 {
                            gen var`j' = runiformint(1,5)
                            label var var`j' "variable `j'"
                            }
                            
                            graph hbar var1 var2 var3, over(year, axis(off)) yvaroptions(sort(1) descending relabel(1 "`: var label var1'" 2 "`: var label var2'")) ///
                            yscale(reverse) bargap(+30) intensity(30) ascategory blabel(bar, position(inside) format(%9.1f))
                            Last edited by Nick Cox; 07 Mar 2024, 07:16.

                            Comment

                            Working...
                            X