Announcement

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

  • Bar graph: switching grouping category

    Hi everyone,

    Seems like a simple thing to do, but I can't figure it out.

    Code:
    graph bar innovation_faith innovation_reforms innovation_critiques, over(gender)
    I get this, but I want the bars to be grouped where men and women bars are next to each other for easy comparison, for each outcome variable. Using
    Code:
    by(gender)
    or
    Code:
    asyvars
    don't work either.

    Best,
    Jason

    Click image for larger version

Name:	Graph.png
Views:	1
Size:	47.1 KB
ID:	1683028




  • #2
    One way to do this is using statplot from SSC, which must be installed before you can use.it.

    #1 does not provide example data, so here some are faked.


    Code:
    clear
    set obs 100
    set seed 2803
    gen woman = _n > 50
    label define woman 0 man 1 woman
    label val woman woman
    
    foreach v in faith critiques reforms {
        gen `v' = runiformint(4,7)
    }
    
    set scheme s1color
    
    graph bar faith critiques reforms, over(woman) name(G1, replace)
    
    ssc install statplot
    
    statplot faith critiques reforms, over(woman) recast(bar) asyvars name(G2, replace)
    Something like

    Code:
    statplot faith critiques reforms, over(woman) recast(dot) asyvars  exclude0
    is probably a more effective use of space. I am guess that it is the comparison of the variables and genders that is of interest, not comparisons with zero.
    Last edited by Nick Cox; 22 Sep 2022, 17:41.

    Comment


    • #3
      Ah perfect thank you! Exactly what I was looking for. For others with this problem, I just used this code:

      Code:
      statplot innovation_faith innovation_reforms innovation_critiques, over(gender) recast(bar) asyvars
      And got this output:
      Click image for larger version

Name:	Graph.png
Views:	1
Size:	45.7 KB
ID:	1683035

      Comment


      • #4
        Good that it worked for you, but that;s rather a lot of ink to show 6 numbers. Depending on how each variable was measured (opinion scale?) you could show more detail on each distribution. Also, readers deserve variable labels that are less ugly than variable names.

        Comment


        • #5
          That's fair. The variables are composite measures, 1-8 scale, so it might be difficult to show more detail. Agreed on improving the quality of the graph. I do have a follow up question, can you add CIs with statplot?

          Comment


          • #6
            statplot does not support confidence intervals. See my paper on statsby for a way forward.

            Comment


            • #7
              Note that instruction with an example of how to use Stata's twoway command to create 'bars with confidence intervals' is available at this UCLA webpage.
              The user community contributed package cibar from Alexander Staudt took that webpage as an inspiration as to create bars with confidence intervals over groups.
              Following Nick's example in #2:
              Code:
              * Preliminaries
              ssc install cibar , replace
              
              *Example using #2
              clear
              set obs 100
              set seed 2803
              gen woman = _n > 50
              label define woman 0 man 1 woman
              label val woman woman
              
              foreach v in faith critiques reforms {
                  gen `v' = runiformint(4,7)
              }
              set scheme economist
              cibar faith, over(woman) ciopts(lc(gs4) lw(*2))
              graph export "Example_Economist_Bars_CI.png", width(500) height(500) replace
              Which produces this result:
              Click image for larger version

Name:	Example_Economist_Bars_CI.png
Views:	1
Size:	6.9 KB
ID:	1683057



              Note that cibar does support using weights but that it is limited to visualizing only one variable while it has an option to use over multiple times for categorical or binary variables, as such an interesting option to use.

              However, I agree with Nick (see #4 at this page) that this manner of data visualization might better be replaced by this type of visualization:
              Click image for larger version

Name:	Example_ciplot_CI.png
Views:	1
Size:	13.5 KB
ID:	1683058


              which is the result of this code that uses ciplot, another user community contributed package by Nick:
              Code:
              * Preliminaries
              ssc install ciplot , replace
              * Code
              set scheme s1color
              ciplot faith critiques reforms, by(woman) legend(rows(1))
              For those interested in more advanced (sorted) visualizations of confidence intervals around (geometric) means, do visit this page with other examples of ciplot.
              http://publicationslist.org/eric.melse

              Comment


              • #8
                Oh this is very helpful, thank you. I really appreciate the resources, I find graphing in Stata rather non-intuitive.

                Comment

                Working...
                X