Announcement

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

  • (h)bar plots with ranges, colors and extra information (label) on the right

    1) Is it possible to recreate a graph like this
    http://
    junkcharts.typepad.com/.shared/image.html?/photos/uncategorized/ec_smoke.gif

    in Stata?
    I.e., a bar plot that has ranges, and the ranges colors are defined by a third variable.

    The references I found to this all point to range plots (twowayrbar y1var y2var xvar), but that is not the case here because (1) the xvar is categorical, (2) I want to distinguish the bars by a third variable

    2) what about this graph
    http://theeconomist.tumblr.com/post/...iggest-defence
    ?


    can I do a hbar plot adding information from a trid variable on the right hand side of the graph? Would this be some sort of label, and if so, how to put it on the right?
    can this be combined with the graph above (1)

    3) what about this graph?
    http://junkcharts.typepad.com/.shared/image.html?/photos/uncategorized/2008/04/18/econ_anglosaxon.gif

    I.e., for each category it has ranges over a second category. Can it be done in Stata?

    regards
    Lucas


  • #2
    Yes, you can do them all. 3) would be most work, but it is doable.

    1) Compare

    Code:
    sysuse auto, clear 
    local xtitle : var label mpg 
    collapse (min) min=mpg (max) max=mpg, by(foreign rep78) 
    gen rep78_1 = rep78 - 0.1 if foreign 
    gen rep78_2 = rep78 + 0.1 if !foreign 
    twoway rcap min max rep78_1 , horiz || rcap min max rep78_2, horiz  ///
    yla(1/5, ang(h)) ytitle("`: var label rep78'") ///
    xtitle("`xtitle'") legend(order(1 "Foreign" 2 "Domestic"))
    2) Compare

    Code:
    sysuse auto
    graph hbar (mean) mpg, over(foreign) over(rep78) asyvars
    Last edited by Nick Cox; 14 Apr 2014, 09:08.

    Comment


    • #3
      tks, Nick,
      Last edited by Lucas Mation; 14 Apr 2014, 09:21.

      Comment


      • #4
        Nick, on item 2), of the graph:
        http://theeconomist.tumblr.com/post/3748598014/daily-chart-the-worlds-biggest-defence

        notice the white numbers on the right, for "military personnel". They Adding another layer of information not related to the y axis but to the categories of the bar graph.

        How to recreate a graph with that?

        Comment


        • #5
          In principle you can add any text you like as axis labels for an extra axis.

          Code:
          sysuse auto, clear
          set scheme s1color
          local xtitle: var label mpg
          collapse mpg price, by(foreign rep78)
          gen rep78_1 = rep78 - 0.12 if foreign
          gen rep78_2 = rep78 + 0.12 if !foreign
          
          forval j = 1/5 {
              su price if rep78 == `j' & foreign == 0
              local lbl : di %4.0f r(mean)
              local J = `j' + 0.12
              local labels0 `labels0' `J' "`lbl'"
          
              su price if rep78 == `j' & foreign == 1
              local lbl : di %4.0f r(mean)
              local J = `j' - 0.12
              local labels1 `labels1' `J' "`lbl'"
          }
          
          twoway bar mpg rep78_1 , horiz barw(0.2) || bar mpg rep78_2, horiz barw(0.2) ///
          yla(1/5, ang(h)) ytitle("`: var label rep78'", axis(2)) ///
          xtitle("`xtitle'") legend(order(1 "Foreign" 2 "Domestic")) ///
          yaxis(1 2) yla(`labels0' `labels1', axis(1) ang(h) noticks) ///
          ytitle(Price (USD), axis(1))


          Attached Files

          Comment


          • #6
            great, tks

            Comment


            • #7
              The bars would be better starting at 0. That's incidental to the main point here, but base(0)appears necessary. That's supposed to be the default.....

              Comment


              • #8
                HI ,

                I understood most of the code. Can you explain this part please.

                local labels0 `labels0' `J' "`lbl'"


                Sorry, it might be a very basic question for you.

                Thanks
                Anwar

                Comment


                • #9

                  It's in a loop. If you want a discursive tutorial on loops, including material on local macros, see http://www.stata-journal.com/sjpdf.h...iclenum=pr0005 (I love the numerology that this column was (2002) 2(2): 202-222, none of which was contrived.)

                  The second half of the loop contents is just a minor variation on the first, so won't need the same kind of explanation.

                  Code:
                   
                  forval j = 1/5 { 
                       su price if rep78 == `j' & foreign == 0 
                       local lbl : di %4.0f r(mean) 
                       local J = `j' + 0.12 
                       local labels0 `labels0' `J'  "`lbl'" 
                  
                       * second half a variation on the same, but for differently offset axis labels. 
                  
                  }
                  Here is the code again, but first time round, so I replace all references to local macro j with 1. That done, I can replace all references to local macro J too.

                  Code:
                   
                       su price if rep78 == 1 & foreign == 0 
                       local lbl : di %4.0f r(mean) 
                       local J = 1 + 0.12 
                       local labels0 `labels0'  1.12  "`lbl'"
                  The summarize statement yields among other results the mean, stored as r(mean). I format that result to the nearest integer and it becomes in this example 4565. The local macro labels0 doesn't exist yet, so the reference to it is treated as an empty string. So labels0 is born at this point containing the text

                  Code:
                   
                  1.12 "4565"
                  which is the first detail for axis labels, namely part of what will be a specification of y axis labels.

                  Every time I go round the loop I work out positions for axis labels -- when I am done I have 1.12 0.88 2.12 1.88 3.12 2.88 4.12 3.88 5.12 4.88 -- and the text (numeric results) that will go at those positions. The axis labels are designed to align with the bars which are centred at exactly those positions and have widths 0.2, so they don't touch but are close. Second and later times round the loop, each new detail is added to the existing local macro labels0 or labels1. (There is actually no reason to keep those macros separate.)

                  In real coding, the actual magnitudes of offsets are the results of trial and error (what looks good to me?) and I develop code in a do-file editor window.



                  Comment


                  • #10
                    Hi,
                    Thank you Nick. It will take some time for me to digest it .
                    Thanks again for taking time for such a detailed post
                    Sincerely
                    Anwar

                    Comment

                    Working...
                    X