Announcement

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

  • Highlighting a specific bar and displaying a label directly above highlighted bar

    Hi all,

    I;m using Stata 13.1 and I am trying to generate a series of graphs whereby I highlight a specific (but different) bar in each graph. I have managed to get to the point of highlighting a specific graph, but I'm struggling to be able to add a label or some text above the highlighted bar. Just to take this further, I will highlight each bar in turn and wish the text to move according to which bar is highlighted. The text I wish to add is "your area"


    *EXAMPLE CODE**


    clear
    set obs 21
    g long name =.
    replace name = _n
    g area = name+10


    gen mytext =""
    levelsof name, local(levelstud)
    qui foreach l of local levelstud {
    replace mytext = "Your area" in `l'
    }

    forval i = 1/1 {
    g y = `i'
    **preserve
    separate area, by(name == `i')
    di `i'
    g name2 = "your area" if name==`i'
    graph bar area0 area1, nofill over(name, sort(area) axis(off)) yla("") legend(off) ///
    bar(1, bfcolor(none)) plotregion(color(white)) blabel(name2, position(outside)) ytitle("")

    **restore
    }


    I'm aware that in -blabel- 'name2' is not a names style. When I use just 'name' it adds a label to every single bar.

    Any help appreciated

  • #2
    This seems to work:
    Code:
    clear
    set obs 21
    g long name =.
    replace name = _n
    g area = name+10
    
    forval i = 1/21 {
        preserve
        qui separate area, by(name == `i')
        local a = area1[`i']+2.5
        local b = (`i'/22)*100
    
        disp "`i', `a', `b'"
        graph bar area0 area1, nofill over(name, sort(area)) yla("") legend(off) ///
         bar(1, bfcolor(none)) plotregion(color(white)) ytitle("")  /// 
         text(`a' `b'  "your area") /// 
         name(gr`i',replace)
         
        restore
    }

    Comment


    • #3
      How about this:

      Code:
      graph bar area0 area1, nofill over(name, sort(area) axis(off)) yla("") legend(off) ///
      bar(1, bfcolor(none)) plotregion(color(white))  ytitle("") ///
      text(15 1.5 "your area", place(c) orient(vertical))
      See -help added_text_options-

      Comment


      • #4
        See also

        gr0049 Cox, N. J. 2011. Stata tip 102: Highlighting specific bars. Stata Journal 11( 3): 474-- 477

        http://www.stata-journal.com/article...article=gr0049

        Comment


        • #5
          Thanks for the suggestions. I have one final query:

          I would like to colour the axis lines as white (or no line at all), is there an option to do this?

          Thanks again

          Tim



          clear
          set obs 221
          g long name =.
          replace name = _n
          g area = name+10

          forval i = 221/221 {
          preserve
          qui separate area, by(name == `i')
          local a = area1[`i']+35
          local b = (`i'/222)*100
          disp "`i', `a', `b'"
          qui su area, det
          graph bar area0 area1, nofill over(name, label(nolabel) sort(area) gap(0)) yla("") ysc(range(0 `=r(max)+50')) legend(off) ///
          bar(1, bfcolor("91 53 140") color("91 53 140")) bar(2, bfcolor("251 186 80") color("251 186 80")) plotregion(color(white)) ytitle("") nolabel ///
          text(`a' `b' "your area", orient(vertical)) ///
          name(gr`i',replace)

          restore
          }
          Last edited by Tim Evans; 21 Jun 2015, 17:50.

          Comment


          • #6
            see "help axis_scale_options"

            Comment


            • #7
              Add
              Code:
              axis(lc(none))
              as a suboption to over()

              Comment


              • #8
                Thanks for the replies. I have tried specifying -axis(lc(none))- as a suboption to -over(), and this deals with the x-axis line. However I still have a vertical black line for the y-axis. I have tried specifying -axis(lc(none))- outside of the -over()- part of my code, but Stata cheerfully tells me ;

                clear
                set obs 221
                g long name =.
                replace name = _n
                g area = name+10

                forval i = 221/221 {
                preserve
                qui separate area, by(name == `i')
                local a = area1[`i']+35
                local b = (`i'/222)*100
                disp "`i', `a', `b'"
                qui su area, det
                graph bar area0 area1, axis(lc(none)) nofill over(name, axis(lc(none)) label(nolabel) sort(area) gap(0)) yla("") ysc(range(0 `=r(max)+50')) legend(off) ///
                bar(1, bfcolor("91 53 140") color("91 53 140")) bar(2, bfcolor("251 186 80") color("251 186 80")) plotregion(color(white)) ytitle("") nolabel ///
                text(`a' `b' "your area", orient(vertical)) ///
                name(gr`i',replace)

                restore
                }

                option axis() not allowed
                r(198);

                I read a post from some time ago that also had this problem, but unfortunately the poster never posted the coded solution (http://www.stata.com/statalist/archi.../msg00677.html)


                Thanks
                Tim
                Last edited by Tim Evans; 22 Jun 2015, 03:08.

                Comment


                • #9
                  Thanks for the reproducible examples. The next badge comes for use of CODE delimiters. You need, I think,

                  Code:
                   
                  ysc(lc(none))
                  not

                  Code:
                   
                  axis(lc(none))
                  as the axis you want to modify is the y axis.

                  It's a small deal, but

                  Code:
                   
                  qui su area, det
                  does nothing useful here. In earlier examples you wanted a maximum to use in specifying the graph, but for that

                  Code:
                   
                  su area, meanonly
                  is quite sufficient. See also (e.g.) http://www.stata-journal.com/sjpdf.h...iclenum=st0135

                  Comment


                  • #10
                    Thanks for tip and tidy up suggestion Nick. I'll work on claiming that next badge soon!

                    Comment

                    Working...
                    X