Announcement

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

  • Adding value labels at top of twoway bars.

    Dear All,
    I am creating a bar chart using the following dataset:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long type float(type0 disp0 disp1)
    1  .75  .7627072  .7905607
    2 1.75 1.0727396 1.1185094
    3 2.75 1.0815755 1.0576259
    end
    label values type type
    label def type 1 "Asian", modify
    label def type 2 "Black", modify
    label def type 3 "Hispanic", modify
    Here is the code for the bar chart, which looks as expected and serves the purpose:

    Code:
    twoway     (bar disp0 type0, barwidth(.25) color(khaki)) || ///
        (bar disp1 type, barwidth(.25) color (navy)), ///
        legend(order(1 "New MCI/ADRD dx per 100 without treatment" 2 "New MCI/ADRD dx per 100 with treatment") pos(6)) ///
        xlabel(.88 "Asian" 1.88 "Black" 2.88 "Hispanic" ) ylabel(.7(.1)1.2, valuelabel labsize(2.5)) yline(1, lcolor(cranberry))
    I would like to add value labels (up to 2 decimal places), displaying the height of each bar at the top of each bar. For instance, at the top of disp0 bar for Asian it would say "0.75". Any help with editing to above code to add the value labels on top of each bar would be greatly appreciated.
    Many thanks in advance for your help.
    Gratefully,
    Sumedha

  • #2
    Thanks for a reproducible example. Value labels are a completely different concept in Stata terminology. What you’re asking for are bar labels. You can add these as marker labels on invisible markers using scatterplots in a twoway graph.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long type float(type0 disp0 disp1)
    1  .75  .7627072  .7905607
    2 1.75 1.0727396 1.1185094
    3 2.75 1.0815755 1.0576259
    end
    label values type type
    label def type 1 "Asian", modify
    label def type 2 "Black", modify
    label def type 3 "Hispanic", modify
    format disp? %3.2f  
    
    twoway     (bar disp0 type0, barwidth(.25) color(khaki)) || ///
        (bar disp1 type, barwidth(.25) color (navy)) || ///
        scatter disp0 type0, ms(none) mlab(disp0) mlabpos(12) mlabc(black) || ///
        scatter disp1 type, ms(none) mlab(disp1) mlabpos(12) mlabc(black) ||, ///
    legend(order(1 "New MCI/ADRD dx per 100 without treatment" 2 "New MCI/ADRD dx per 100 with treatment") pos(6)) ///
        xlabel(.88 "Asian" 1.88 "Black" 2.88 "Hispanic" ) ylabel(.7(.1)1.2, valuelabel labsize(2.5)) yline(1, lcolor(cranberry))

    Click image for larger version

Name:	Graph.png
Views:	1
Size:	41.6 KB
ID:	1776635


    Comment


    • #3
      Some extra suggestions building on Andrew Musau's excellent work:

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input long type float(type0 disp0 disp1)
      1  .75  .7627072  .7905607
      2 1.75 1.0727396 1.1185094
      3 2.75 1.0815755 1.0576259
      end
      label values type type  
       
      label def type 1 "Asian", modify
      label def type 2 "Black", modify
      label def type 3 "Hispanic", modify
      format disp? %3.2f  
      
      twoway     (bar disp0 type0, base(0) barwidth(.25) color(khaki)) || ///
          (bar disp1 type, base(0) barwidth(.25) color (navy)) || ///
          scatter disp0 type0, ms(none) mlab(disp0) mlabpos(12) mlabc(black) || ///
          scatter disp1 type, ms(none) mlab(disp1) mlabpos(12) mlabc(black) ||, ///
          subtitle(New MCI/ADRD dx per 100) ///
      legend(order(1 "without treatment" 2 "with treatment") row(1) pos(6)) ///
          xlabel(.88 "Asian" 1.88 "Black" 2.88 "Hispanic", tlc(none) ) ylabel(.7(.1)1.2, labsize(2.5)) yline(1, lcolor(cranberry))
      Click image for larger version

Name:	asian_black_hispanit.png
Views:	0
Size:	0
ID:	1776642
      Last edited by Nick Cox; 27 Apr 2025, 15:02.

      Comment


      • #4
        Here's another take.

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input long type float(type0 disp0 disp1)
        1  .75  .7627072  .7905607
        2 1.75 1.0727396 1.1185094
        3 2.75 1.0815755 1.0576259
        end
        label values type type
        label def type 1 "Asian", modify
        label def type 2 "Black", modify
        label def type 3 "Hispanic", modify
        format disp? %3.2f  
        
        twoway     (bar disp0 type0, base(0) barwidth(.25) color(khaki)) || ///
            (bar disp1 type, base(0) barwidth(.25) color (navy)) || ///
            scatter disp0 type0, ms(none) mlab(disp0) mlabpos(12) mlabc(black) || ///
            scatter disp1 type, ms(none) mlab(disp1) mlabpos(12) mlabc(black) ||, ///
            subtitle(New MCI/ADRD dx per 100) /// 
        legend(order(1 "without treatment" 2 "with treatment") row(1) pos(6)) ///
            xlabel(.88 "Asian" 1.88 "Black" 2.88 "Hispanic", tlc(none) ) ylabel(.7(.1)1.2, labsize(2.5)) yline(1, lcolor(cranberry))
        
        * take 2 
        drop type0 
        
        reshape long disp, i(type) j(treatment)
        label def treatment 0 without 1 with 
        label val treatment treatment 
        
        separate disp, by(treatment) veryshortlabel
        
        scatter disp? treatment, xla(0 1, glp(solid) glw(thin) glc(gs8) valuelabel tlength(0)) ///
        xsc(r(-0.2 1.2)) mcolor(khaki navy) ms(O S) msize(medlarge ..) mla(disp0 disp1) mlabpos(3 3) mlabc(black black) ///
        by(type, row(1) note("") legend(off)  ) ///
        ytitle(New MCI/ADRD dx per 100) ylabel(.7(.1)1.2, labsize(2.5)) yline(1, lcolor(cranberry))
        Click image for larger version

Name:	asian_black_hispanic.png
Views:	1
Size:	64.3 KB
ID:	1776645

        Comment


        • #5
          This is the graph from #3


          Click image for larger version

Name:	asian_black_hispanit.png
Views:	1
Size:	24.0 KB
ID:	1776647

          Comment


          • #6
            I don't know what
            New MCI/ADRD dx per 100
            means or why 1/100 is a special value.

            Comment


            • #7
              Profs. Musau and Cox,
              Thank you so much for your help. Prof. Musau's solution did the trick, but the "take 2" from Prof. Cox in #4 above was aesthetically more pleasing than what I had initially suggested, so going with that one. Very grateful to both of you.
              Sincerely,
              Sumedha

              Comment


              • #8
                Thanks for the closure. What's the story on #6?

                Comment

                Working...
                X