Announcement

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

  • Bar and line on same graph

    I am using Stata 13.

    I want to get bar and line on same graph,. I tried following code:
    Code:
     twoway bar grp1 grp2  reportyear, stacj  || line grdnv reportyear
    but it returns the error: option stack not allowed

    Is it possible to use stack or other bar functions when combining line and bar graphs?

  • #2
    Your syntax says stacj which would produce a different error message. Assuming that you really did mean stack, there is still a different and more difficult problem.

    stack is an option of graph bar, which (confusingly or otherwise) is absolutely nothing to do with twoway bar.

    If you want to stack one bar on top of another you will need to set that up differently, something like this:

    Code:
     
    gen bargrp2 = grp1 + grp2 
    label var bargrp2 "`: var label grp2'" 
    
    twoway bar grp1 reportyear || rbar grp1 bargrp2 reportyear || line grdnv reportyear

    Comment


    • #3
      There are many bar graphs that can be combined with other graphs (see help twoway). Any twoway graph can be combined with another twoway graph. However, the commands twoway bar and graph bar are different commands. Stack is an option of graph bar, not twoway (see help graph bar). So graph bar cannot be combined with a twoway line graph.

      That said, Nick Cox has posted (within the last week) several ways to create stacked bar graphs using twoway bar and alternatives to stacked bars that might help you. Search this forum for queries about stacked bar graphs.

      Crossed with Nick Cox in cyber space
      Stata/MP 14.1 (64-bit x86-64)
      Revision 19 May 2016
      Win 8.1

      Comment


      • #4
        Thanks for answer.

        How should I proceed for third variable? I tried:
        Code:
        gen bargrp2 = grp1 + grp2
        label var bargrp2 "`: var label grp2'"
        gen bargrp3 = grp1 + grp2 + grp3
        label var bargrp2 "`: var label grp3'"
        
        twoway bar grp1 reportyear || rbar grp1 bargrp2 reportyear || rbar grp1 grp2 bargrp3 reportyear || line grdnv reportyear
        but it returns error rbar requires 3 variables: grp1 grp2 bargrp3 reportyear

        Comment


        • #5
          I think this doesn't work. I think problem is that I have positive and negative values. For example the graph:
          Graph.gph

          Comment


          • #6
            You can work this out. But first please read http://www.statalist.org/forums/help#stata for advice not to show .gph attachments and what to do instead.

            If values are positive and negative then stacking is not a good design if bars would overlap. To give better advice, we need to know more about your data. Again, please see http://www.statalist.org/forums/help#stata But it seems likely that you need a different design, e.g. using lines for all your variables.

            You'd need

            Code:
              
            gen bargrp2 = grp1 + grp2
            label var bargrp2 "`: var label grp2'"  
            
            gen bargrp3 = grp1 + grp2 + grp3
            * typo fix  
            label var bargrp3 "`: var label grp3'"  
            
            * logic fix  
            twoway bar grp1 reportyear || rbar grp1 bargrp2 reportyear || rbar bargrp2 bargrp3 reportyear || line grdnv reportyear
            except that that won't solve the overlap problem.

            Comment

            Working...
            X