Announcement

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

  • #16
    Sorry about your difficulties -- Statalist administrators (not me!) needed to rejig some forum settings

    But when I look at #18 you are basically saying "Yes, I know this has chunks of code that don't fit with the dataset or anything I've defined". And when I see that sometimes I can grasp what the question is, despite the noise, and sometimes I think "No, please clean up and try again". Here it's the latter.

    It's fine to start with a Stata dataset but your own code must run -- or clearly show with an error or error message what your problem is.

    Comment


    • #17
      Hi Nick,
      Codes in #18 works perfectly to produce a graph. There is no error as such when running #18. I need help adding "%" at the end of barlabels as "0.013%".

      Here is revised code with noise deleted:

      Code:
      
      sysuse census, clear
      generate mar_prop = marriage/pop
      generate div_prop = -1*(divorce/pop)
      
      capture program drop program_graph_figure3
      program define program_graph_figure3
      syntax, ///
              year(integer) age(integer) prop(string) ///
              legend_label1(string) legend_label2(string) ///
              xlabel_age(string) xlabel_year(string) ///
              fignum(string) subtitle(string)  //
      
      
      graph hbar  mar_prop ///
                  div_prop, ///
                                      nofill ///
                                      over(state, sort(state) label(labsize(1.5)) gap(100)) ///
                                      bar(1, color("250 191 65")) bar(2, color("0 66 131"))  ///
                                      legend(label(1 "`legend_label1'") label(2 "`legend_label2'")   ///
                                              row(1) size(1.5) pos(6) symxsize(1.1)  symysize(1.1)) ///
                                      ylabel(,labsize(1.5) format(%9.3f)) ///
                                      blabel(bar, size(1.0) format(%9.3f) position(outside)) ///
                                      ytitle("Savings as percentage among `xlabel_age' years from `xlabel_year'", size(1.5)) ///
                                      xsize(4) ysize(6) ///
                                      subtitle("`subtitle'", size(1.5)) ///
                                      name(figure`fignum'_table3_`year'_`age', replace)
      
      end
      
      program_graph_figure3, ///
              year(2130) age(30) prop(prop_exp) ///
              legend_label1(Prevention) legend_label2(Treatment) ///
              xlabel_age("30+") xlabel_year("2022-2030") ///
              fignum("3a") subtitle("Total exp")
      Attached Files
      Last edited by bibha dhungel; 03 Jun 2025, 07:34.

      Comment


      • #18
        That's a bit clearer, so thanks, but in essence you've not even tried in your code to add %.

        The drift of this thread should be clearer if you look through all the posts from 2014 at once.

        1. Adding % labels in graph bar or graph hbar isn't trivial, but people have suggested using Graph Editor calls (#2, #7) or there's Joseph Luchman 's trickier code (#13), which you would need to adapt.

        2. It's much easier in twoway bar (and by extension in other similar commands such as twoway histogram).

        I am very happy to use graph bar or graph hbar when it's needed but I don't see anything in your example that implies you need it.

        A y axis title (here horizontal) in your example that has nothing to do with the data you use is a distraction. So, I will just focus on twoway code that does something closer to what I think you want. I appreciate that you're working towards a personal program to be used for quite different data but I've left that on one side.

        Notes:

        1. labmask is from the Stata Journal.

        2. If your real problem is for 50 states, or for a dataset as large or larger than that, then getting a legible result may be hard.

        3. I am presuming that alphabetical order really isn't ideal. That could be wrong for you.

        Code:
        sysuse census, clear
        
        generate mar_prop = 100 * marriage/pop
        generate div_prop = -100*(divorce/pop)
        
        sort mar_prop
        gen order = _n
        labmask order, values(state)
        
        preserve
        
        keep in -25/L
        
        gen mar_show = strofreal(mar_prop, "%2.1f") + "%"
        gen div_show = strofreal(-div_prop, "%2.1f") + "%"
        
        
        twoway bar mar_prop order, barw(0.6) horizontal color("250 191 65") ///
        || scatter order mar_prop, ms(none) mlabc(black) mla(mar_show) ///
        || bar div_prop order, barw(0.6) horizontal color("0 66 131") ///
        || scatter order div_prop, ms(none) mlabc(black) mla(div_show) mlabpos(9) ///
        legend(order(3 "Divorce rate" 1 "Marriage rate" ) pos(12) row(1)) ///
        yla(26/50, valuelabel tlc(none) labsize(small)) ysize(7) ytitle("") xsc(off) xsc(r(. 16))
        
        restore

        Comment


        • #19
          Thank you so much Nick. Using twoway produces both bars on same horizontal line. Thus, bars would stack if numbers are in the same direction. Is there a way to have blue and yellow bars lying side by side (just as in #24)?

          Code:
          sysuse census, clear
          
          generate mar_prop = 100 * marriage/pop
          generate div_prop = -100*(divorce/pop)
          replace div_prop = -1 * div_prop if state == "Nevada"
          sort mar_prop
          gen order = _n
          labmask order, values(state)
          
          
          preserve
          
          keep in -25/L
          
          gen mar_show = strofreal(mar_prop, "%2.1f") + "%"
          gen div_show = strofreal(-div_prop, "%2.1f") + "%"
          
          
          twoway bar mar_prop order, barw(0.6) horizontal color("250 191 65") ///
          || scatter order mar_prop, ms(none) mlabc(black) mla(mar_show) ///
          || bar div_prop order, barw(0.6) horizontal color("0 66 131") ///
          || scatter order div_prop, ms(none) mlabc(black) mla(div_show) mlabpos(9) ///
          legend(order(3 "Divorce rate" 1 "Marriage rate" ) pos(12) row(1)) ///
          yla(26/50, valuelabel tlc(none) labsize(small)) ysize(7) ytitle("") xsc(off) xsc(r(. 16)) ///
          xscale(range(-3 14))
          
          restore


          I tried code in #13. I cannot manipulate size of state names on the left, get rid of the % values on the left (except setting the size to 0) and reduce gap between state names and the graph box line. And how to increase number of decimal places?
          Code:
          blabel(group, size(0.9) format(%9.2f))
          is not working.


          Code:
          sysuse census, clear
          generate mar_prop = 100 * marriage/pop
          generate div_prop = -100*(divorce/pop)
          replace div_prop = -1 * div_prop if state == "Nevada"
          generate vallab = string(round(mar_prop)) + "%"
          
          graph hbar mar_prop div_prop, ///
                  over(vallab, axis(off) gap(0)) ///
                  over(state, label(labsize(0.9))) /// these are the percentages on left
                  blabel(group, size(0.9) format(%9.2f)) nofill asyvars ///
                  ytitle("Percentage", size(0.9)) ///
                  legend(label (1 "Marriage") label (2 "Divorce") pos(6) ring(1) row(1) size(1) symxsize(1) symysize(1)) ///
                  ylabel(, labsize(1)) // 0,5,10,15
          Click image for larger version

Name:	fig5.png
Views:	1
Size:	435.6 KB
ID:	1778455

          Attached Files
          Last edited by bibha dhungel; 03 Jun 2025, 18:55.

          Comment


          • #20
            Using twoway produces both bars on same horizontal line. Thus, bars would stack if numbers are in the same direction.
            Not so. Bars would be superimposed. They would only stack with different code.

            Is there a way to have blue and yellow bars lying side by side (just as in #24)?
            Yes, That could be done. See https://journals.sagepub.com/doi/pdf...867X0700700112 for one main idea.

            Otherwise I think your question is about implementing Joseph Luchman's approach, which I don't understand, so for that reason alone I can't help there. The root problem seems to be trying to squeeze 50 names and 100 bars into a small space. I can't understand your preference for graph hbar here.
            Last edited by sladmin; 04 Jun 2025, 11:37. Reason: fix quote bracket

            Comment

            Working...
            X