Announcement

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

  • Bar Graph Options in Combination Graphs

    Hi all,

    Suppose I'm creating a graph like this:

    Code:
    sysuse uslifeexp, clear
    
    twoway (bar le le_b     , over(year) bar(1,color(gray)) bar(1,color(black))) ///
     (line le_female year         , sort(year) lcolor(red)) ///
     (line le_male year         , sort(year) lcolor(yellow)) ///
     (line le_bfemale year         , sort(year) lcolor(green)) ///
     (line le_bmale year         , sort(year) lcolor(blue))
    To summarize, I'd like to underlay the bar graphs of le and le_b, and then overlay the female/male lines.

    Now, Stata will not allow me to use the "over" option or the "bar" option for the bar graph. Can anyone recommend a solution?

  • #2
    That's mushing together syntax for graph twoway bar and for graph bar and it can't be done in that way. They're quite different commands. (Don't shoot the messenger!)

    Working backwards,

    1. If you want some line elements, that requires twoway line. So far, so good.

    2. If you want bars too, that means twoway bar in combination and not graph bar (nor any of its options not documented under the same names).

    3. If you want two bars side by side for the same year, or something similar then with twoway you'll have to arrange that yourself using offsets. See for example

    SJ-7-1 gr0026 . . . . Stata tip 42: The overlay problem: Offset for clarity
    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . J. Cui
    Q1/07 SJ 7(1):141--142 (no commands)
    tip for graphing several quantities on a continuous axis

    http://www.stata-journal.com/sjpdf.h...iclenum=gr0026

    I feel queasy at a distance about mixing line and bar elements capriciously but at the same time it's evident that this is just a proxy for your real graph for which that presumably makes full sense.

    Comment


    • #3
      Thanks, Nick. It turns out "That's mushing together syntax for graph twoway bar and for graph bar and it can't be done in that way" was enough of a clue for me (I can't believe I didn't notice that myself!), and that helped me sort out my code, which is now:

      Code:
      sysuse uslifeexp, clear
      
      twoway (bar le year, color(gray))     ///
          (bar le_b year, fcolor(none))    ///
          (line le_female year         , sort(year) lcolor(red)) ///
          (line le_male year         , sort(year) lcolor(yellow)) ///
          (line le_bfemale year         , sort(year) lcolor(green)) ///
          (line le_bmale year         , sort(year) lcolor(blue))
      (Also you're right, this is a proxy for my real graph, where I want to show something as a sort of "area" and some other things as trends.)
      Last edited by Meg Steinheil; 29 Jan 2018, 09:22.

      Comment


      • #4
        Good that you made real progress, but my Sheldon side makes me underline that red and green together are problematic for many readers.

        See http://colorbrewer2.org for colour schemes that are friendly to as many people as possible.

        Comment


        • #5
          Oh that's a really good point. Thanks for the link.

          Comment


          • #6
            hi Nick,

            I want to create a combine hbar graph with two vars inside per each: shares and values per 2008 and 2018 by sector. is it possible to get the combine graph with bars ordered by the sorted share in 2018. thanks

            Comment

            Working...
            X