Announcement

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

  • Stacked bar graph

    Hey,

    I would like to create a stacked bar graph. An example would be "mpg" and "trunk" of the auto-dataset.

    I tried to use code from here:
    http://www.surveydesign.com.au/tipsgraphs.html

    Unfortunately the stacked bar graph there does something different (it stacks the bars using the different car types and not different variables).

    Thanks in advance!

    Best,
    Thomas
    Last edited by Thomas Mitterling; 17 Aug 2017, 08:42. Reason: Tags added

  • #2
    I don't get any idea from your first example on what you want. In what sense are mpg and trunk even comparable? What is to be stacked? What defines different bars?

    You then explain that the website cited doesn't do what you want.

    This really isn't clear.

    Comment


    • #3
      Sorry! Unfortunately I'm not able to get the direct link to the copy, but I want something that looks like this graph (row 4, column 2 of the link above):
      http://www.surveydesign.com.au/tips/..._bar_graph.png

      mpg and trunk are only comparable in the sense that they are both integers. I want the same graph as in the link, but with mpg and trunk instead of Domestic and Foreign.

      The size of the bar is defined by the values of mpg and trunk for the different cars. On the x-axis I want the different values of make (i.e. the different cars).

      Is this clear now?

      Comment


      • #4
        Sorry, still not very clear to me, but superimposing bars (which now seems to be what you want, not stacking at all) is a poor design for variables on different scales.

        Perhaps https://www.statalist.org/forums/for...ailable-on-ssc can help.

        Comment


        • #5
          As far as I can see from the data the bars in the linked graph are stacked, not superimposed.

          And yes, I'm aware of that, I just chose those variables because they're integer, "my variables" are on the same scale.

          edit:
          I found a better example. In the pop2000 dataset there is the population and its subgroups. I would like to plot the number of people (black and white stacked) for each age category. Is this example clear?
          Last edited by Thomas Mitterling; 17 Aug 2017, 12:44. Reason: Better example found

          Comment


          • #6
            OK. To stack two groups, a simple recipe is to plot the histogram of both groups combined and then on top the histogram of one group. Then the difference is the other group. For this to work you must use frequencies (which are additive) and consistent starts and widths.

            Code:
            sysuse auto, clear
            set scheme s1color 
            twoway histogram mpg, start(10) width(2) bc(red) freq || histogram mpg if foreign, start(10) width(2) bc(blue) freq legend(order(1 "Domestic" 2 "Foreign"))

            Comment


            • #7
              Thanks for the example, but unfortunately my dataset is different. I have two separate variables that I want to plot stacked, not the whole sample + a subsample. How do I adjust your code for that (it looks exactly like in the second example with the population)?

              Comment


              • #8
                Code:
                stack mpg trunk, into(combined) clear
                Then you have a single response and a binary variable and it's the same problem.

                Comment


                • #9
                  Sorry, not clear enough again. I wanted mpg and trunk for the different cars (the cars are the variables on the x-axis). So actually I wanted a histogram with mpg and trunk stacked for each car, not the frequency of each mpg. Do you know what I mean?

                  I think my "new" example with the population is better, since it's basically the same problem (and makes far more sense than using the auto dataset).

                  Comment


                  • #10
                    We may finally have converged. Modulo some measure of stupidity on my part, a good data example from you using your real data early in the thread would have speeded up the discussion. I don't know why you are coy about giving data examples, here and elsewhere. They really help.

                    Consider this:

                    Code:
                    sysuse pop2000, clear 
                    set scheme s1color 
                    gen both = black + white 
                    
                    * ssc inst mylabels 
                    mylabels 0(5)85, myscale(1/2 + @/5) local(xla)
                    mylabels 0(5)20, myscale(@ * 1e6) local(yla) 
                    
                    twoway bar both agegrp, base(0) bcolor(gs2) ///
                    || bar white agegrp ,  bcolor(gs14) ///
                    xla(`xla') xtitle(Age (years)) /// 
                    ytitle(Population (millions)) yla(`yla', ang(h)) /// 
                    legend(order(1 "Black" 2 "White"))
                    Click image for larger version

Name:	age_pop.png
Views:	1
Size:	26.2 KB
ID:	1406947


                    The main idea is just that explained in #6 with a histogram example.

                    1. Plot frequencies for both categories.

                    2. On top plot frequencies for one category.

                    3. The difference must be the frequencies for the other category, which can be labelled as such.

                    In this case, the application is easier because the frequencies are already given. Hence we can just use twoway bar directly, although histograms with frequencies as weights would be possible too.

                    In this case, to get halfway-civilised labels on both axes you have to do some extra work, for which I use mylabels from SSC, which must be installed before you can use it.

                    Comment


                    • #11
                      Thanks a lot Nick! I was able to adjust the code so it works (and looks beautiful), even without mylabels.

                      I'm not sure why I didn't think of just attaching my data (and yes, would've been far easier).

                      Comment

                      Working...
                      X