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


                • #9
                  New MCI/ADRD dx per 100 is the number of individuals per 100 enrollees in an insurance plan who receive a new diagnosis of mild cognitive impairment or Alzheimer's disease and related dementias. Sorry if that was not your question, and thank you again for your generous help.

                  Comment


                  • #10
                    I apologize for this follow up. But I wanted to extend the helpful graph coded by Dr. Cox is #4 to add 95%CI bands. Here is the dataex for the updated data:

                    Code:
                    * Example generated by -dataex-. For more info, type help dataex
                    clear
                    input byte counter str8 race float(disp0 lci_disp0 uci_disp0 disp1 lci_disp1 uci_disp1) long type byte treatment
                    1 "Asian"     .746593 .6949265 .7982594        .        .        . 1 0
                    1 "Asian"           .        .        . .7912914 .5957354 .9868475 1 1
                    1 "Black"     1.07944 1.037142 1.121739        .        .        . 2 0
                    1 "Black"           .        .        . 1.034599 .8178473 1.251351 2 1
                    1 "Hispanic" 1.063857 1.041929 1.085785        .        .        . 3 0
                    1 "Hispanic"        .        .        . 1.043762 .8152252 1.272299 3 1
                    end
                    label values type type
                    label def type 1 "Asian", modify
                    label def type 2 "Black", modify
                    label def type 3 "Hispanic", modify
                    label values treatment treatment
                    label def treatment 0 `""Without" "COVID-19""', modify
                    label def treatment 1 `""With" "COVID-19""', modify
                    Is there a straightforward way to adapt the following code to add the 95%CI of disp0 (lci_disp0 uci_disp0) and disp1 (lci_disp1 uci_disp1):

                    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("") xtitle("") xlabel() ylabel(.7(.1)1.2, labsize(2.5)) yline(1, lcolor(cranberry))

                    Grateful for your help.
                    Sumedha

                    Comment


                    • #11
                      Code:
                      * Example generated by -dataex-. For more info, type help dataex
                      clear
                      input byte counter str8 race float(disp0 lci_disp0 uci_disp0 disp1 lci_disp1 uci_disp1) long type byte treatment
                      1 "Asian"     .746593 .6949265 .7982594        .        .        . 1 0
                      1 "Asian"           .        .        . .7912914 .5957354 .9868475 1 1
                      1 "Black"     1.07944 1.037142 1.121739        .        .        . 2 0
                      1 "Black"           .        .        . 1.034599 .8178473 1.251351 2 1
                      1 "Hispanic" 1.063857 1.041929 1.085785        .        .        . 3 0
                      1 "Hispanic"        .        .        . 1.043762 .8152252 1.272299 3 1
                      end
                      label values type type
                      label def type 1 "Asian", modify
                      label def type 2 "Black", modify
                      label def type 3 "Hispanic", modify
                      label values treatment treatment
                      label def treatment 0 `""Without" "COVID-19""', modify
                      label def treatment 1 `""With" "COVID-19""', modify
                      
                      
                      twoway rcap *_disp0 treatment, lcolor(khaki) ///
                      || rcap *_disp1 treatment, lcolor(navy) ///
                      || 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("") xtitle("") xlabel() ylabel(.7(.1)1.2, labsize(2.5)) yline(1, lcolor(cranberry))
                      5, 6. or 7 decimal places seem over the top to me.

                      Comment


                      • #12
                        More tweaks. Stronger lines, weaker grid lines, 3 d.p.

                        I find the khaki colour too pale compared with navy.

                        Not clear why you need to repeat "COVID-19", which could be explained once in an axis title.

                        Code:
                        * Example generated by -dataex-. For more info, type help dataex
                        clear
                        input byte counter str8 race float(disp0 lci_disp0 uci_disp0 disp1 lci_disp1 uci_disp1) long type byte treatment
                        1 "Asian"     .746593 .6949265 .7982594        .        .        . 1 0
                        1 "Asian"           .        .        . .7912914 .5957354 .9868475 1 1
                        1 "Black"     1.07944 1.037142 1.121739        .        .        . 2 0
                        1 "Black"           .        .        . 1.034599 .8178473 1.251351 2 1
                        1 "Hispanic" 1.063857 1.041929 1.085785        .        .        . 3 0
                        1 "Hispanic"        .        .        . 1.043762 .8152252 1.272299 3 1
                        end
                        label values type type
                        label def type 1 "Asian", modify
                        label def type 2 "Black", modify
                        label def type 3 "Hispanic", modify
                        label values treatment treatment
                        label def treatment 0 `""Without" "COVID-19""', modify
                        label def treatment 1 `""With" "COVID-19""', modify
                        
                        
                        twoway rcap *_disp0 treatment, lcolor(khaki) lw(*2) msize(large) ///
                        || rcap *_disp1 treatment, lcolor(navy) lw(*2) msize(large) ///
                        || scatter disp? treatment, xla(0 1, glp(solid) glw(vthin) glc(gs12) 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) mlabformat(%4.3f %4.3f) ///
                        by(type, row(1) note("") legend(off) ) ///
                        ytitle("Better text needed here") xtitle("") xlabel() ylabel(.7(.1)1.2, labsize(2.5)) yline(1, lcolor(cranberry))

                        Click image for larger version

Name:	withwithout.png
Views:	1
Size:	43.5 KB
ID:	1777274

                        Comment

                        Working...
                        X