Announcement

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

  • Bar graph with subgroups within each bar

    First of all, sorry if this is the wrong section, please let me know

    I am trying to create a bar graph of 5 different binary variables, in which each bar of the 5 contains the total number of observations that have 0 or 1 for that specific variable, stacked on y, with different hues of the same colour.

    For example, I want to graph the number of students attending specific schools, at 5 different towns, towns A to E. I want the 5 bars to be the 5 towns, the height of each bar to be the total number of students attending schools, and each bar to be divided depending on the students attending the different schools (A1 or A2).

    Is this possible within the STATA 13 environment?

    Thanks

  • #2
    The auto data have similar variables. Two possibilities are

    1. catplot (SSC), which here is just a wrapper for graph bar. You can do pretty much the same with graph bar directly in recent Stata, but with different syntax.

    2. tabplot (SSC), which calls up twoway rbar. Working up the equivalent syntax would be harder for non-programmers.

    Here are two simple examples. Much more could be done in each case. Note that I don't stack in the second case. Stacking is conventional, but it often makes structure harder to see.

    Code:
     
    sysuse auto, clear
    
    catplot foreign rep78, recast(bar) stack asyvars bar(1, bcolor(blue*0.2)) bar(2, bcolor(blue*0.6))
    
    tabplot foreign rep78 , separate(foreign) bar1(bfcolor(blue*0.2) blcolor(blue)) bar2(bcolor(blue*0.6) blcolor(blue)) showval
    Click image for larger version

Name:	vasiilis1.png
Views:	1
Size:	10.5 KB
ID:	1334242


    Click image for larger version

Name:	vasilis2.png
Views:	1
Size:	9.6 KB
ID:	1334243

    Comment


    • #3
      Vasilis Tentolouris I would definitely recommend going with Nick Cox's second suggestion (e.g., tabplot) since it will generalize better if your work expands. Depending on the enrollment rules/regs where you are it may be more beneficial to show the towns from which students come within schools (e.g., in Minneapolis there are > 80 schools and the state of Minnesota allows students from anywhere in the state to enroll in any school, so a graph like this could easily get out of hand). I'd also recommend against using colors that vary by hue only as it is a visual encoding of an ordinal scale (e.g., the more/less intense colors end up conveying information about order/rank/magnitude vs conveying information relative to a nominal scale). If you did want to use a gradient across schools/towns, the program brewterpolate in the brewscheme package will allow you to interpolate colors given a starting/ending color value:

      Code:
      . brewterpolate, sc("197 115 47") ec("5, 37, 249") c(4)
      
      . ret li
      
      macros:
              r(colorsdelim) : "197 115 47", "159 99 87", "120 84 128", "82 68 168", "43 53 209", "5 37 249"
              r(colorstring) : "197 115 47" "159 99 87" "120 84 128" "82 68 168" "43 53 209" "5 37 249"
                r(interpend) : "5"
              r(interpstart) : "2"
              r(totalcolors) : "6"
                      r(end) : "5 37 249"
                    r(start) : "197 115 47"
               r(terpcolor6) : "5 37 249"
               r(terpcolor5) : "43 53 209"
               r(terpcolor4) : "82 68 168"
               r(terpcolor3) : "120 84 128"
               r(terpcolor2) : "159 99 87"
               r(terpcolor1) : "197 115 47"
      There are also tools within brewscheme to create a color palette using these colors and to subsequently use them to generate customized scheme files that you can use with your graphs.

      Comment


      • #4
        Thank you both, great answers. The school thing was just an example, I think Nick's method 1 will work great for what I want to show. Thanks again!

        Edit: trying it now. My STATA doesn't support catplot at all, and when I am trying your code with graph bar, it doesn't let me use recast. I have managed to get the desired effect when plotting a single bar, but it goes wrong when I try to do 2 or more. I am now trying to save each single bar graph in memory, and then use graph combine, but I can't get the bars to appear within the same x axis.

        Here is what I have been trying:

        graph bar (rawsum) A B C D E, stack asybars over (group)

        A B C D and E are non-exclusive binary variables, inputted as 0 and 1. Group is a the grouping binary variable of 0 and 1.
        I want 5 bars, each bar representing A to E, and each bar being split into two. The two parts represent the rawsum of A in group=0 and in group=1

        The piece of code that I wrote gives me two bars of group=0 and group=1, that are stacked into five pieces, of rawsums of A to E, which is the opposite of what I want. I have the feeling that I need to use an if (or many ifs) somewhere, but I am not sure how.
        Last edited by Vasilis Tentolouris; 06 Apr 2016, 14:32.

        Comment


        • #5
          catplot was flagged in #2 as coming from SSC. FAQ #12, which you were asked to read before posting, explains why I did that:

          If you are using user-written commands, explain that and say where where they came from: the Stata Journal, SSC, or other archives. This helps (often crucially) in explaining your precise problem, and it alerts readers to commands that may be interesting or useful to them.
          and later in the same section there is an example of how to install a package from SSC. So, typing in Stata

          Code:
          ssc inst catplot
          will add catplot to your installation.

          The recast() option of catplot is indeed not one you will find in graph bar.

          Sorry, but you don't give example data and you don't show your graph, so it's too much of a puzzle for me to work out what your precise problem is. But catplot is accessible to you, so working out to do things with graph bar is not the only option.

          The FAQ Advice http://www.statalist.org/forums/help is not intended as arbitrary rules to catch neophytes; it is crammed with advice aimed to help posters help themselves by asking questions that can be answered easily.

          graph combine never superimposes graphs; it juxtaposes only. asybars is a typo for asyvars

          Comment


          • #6
            Originally posted by Nick Cox View Post
            catplot was flagged in #2 as coming from SSC. FAQ #12, which you were asked to read before posting, explains why I did that:



            and later in the same section there is an example of how to install a package from SSC. So, typing in Stata

            Code:
            ssc inst catplot
            will add catplot to your installation.

            The recast() option of catplot is indeed not one you will find in graph bar.

            Sorry, but you don't give example data and you don't show your graph, so it's too much of a puzzle for me to work out what your precise problem is. But catplot is accessible to you, so working out to do things with graph bar is not the only option.

            The FAQ Advice http://www.statalist.org/forums/help is not intended as arbitrary rules to catch neophytes; it is crammed with advice aimed to help posters help themselves by asking questions that can be answered easily.

            graph combine never superimposes graphs; it juxtaposes only. asybars is a typo for asyvars

            You are right, I'm sorry. Thanks for all the help.

            Comment

            Working...
            X