Announcement

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

  • Reordering sub-bars in stacked bar charts

    For the purpose of clarity, I'll be using the auto.dta dataset included with Stata as an example in my query.
    Code:
    sysuse auto.dta, clear
    graph bar (sum) gear_ratio mpg, over(foreign) stack
    I would like to customize the order of sub-bars for specific bars of my charts. In this example, I would like to switch the order of the sub-bars for the "Foreign" bar only, so that the blue sub-bar appears on top of the red one. I'm currently using Stata 18. Any insight on this query would be greatly appreciated!

    Thank you all for your time.

  • #2
    You need to be able to define each bar separately in graph bar to achieve that. Here is one way.

    Code:
    sysuse auto.dta, clear
    set scheme s1color
    graph bar (sum) gear_ratio mpg, over(foreign) stack ytitle("") ///
    bar(1, color(red)) bar(2, color(blue)) leg(order(1 "gear ratio" 2 "mpg")) ///
    saving(gr1, replace)
    separate gear_ratio, by(foreign)
    separate mpg, by(foreign)
    rename (gear_ratio0 mpg0 gear_ratio1 mpg1) (a0 a1 a3 a2)
    reshape long a, i(make) j(which)
    graph bar (sum) a, over(which) over(foreign) stack bar(1, color(red)) ///
    bar(2, color(blue)) bar(3, color(blue)) bar(4, color(red)) asyvars ///
    leg(order(1 "gear ratio" 2 "mpg")) ytitle("") saving(gr2, replace)
    gr combine gr1.gph gr2.gph
    Res.:
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	17.8 KB
ID:	1722581

    Comment


    • #3
      I would be interested to know why you want to do this. Clearly the example is contrived and just uses a mutually accessible dataset (thanks!) but what is the rationale for your real application?

      Comment


      • #4
        Andrew: Thank you very much for your code. Given that I have a substantial number of variables and bars to represent, this will certainly take some time to achieve what I want

        Nick: I'm interested in visually representing the number of variables for each section of a survey with different waves (corresponding to different bars). I believe that the order in which the questions are asked to the respondent holds valuable information, so I wanted to create a chart with sub-bars in a specific order that matches the questionnaire's sequence. I'm open to any suggestions for better ways to present this information!

        Comment


        • #5
          Thanks for the extra details, but I don't follow why this implies inconsistent orders unless different people are asked questions in a different order. How many people?

          Comment


          • #6
            Nick: The questionnaire has undergone changes across the different waves of the survey. Certain questions were asked at the beginning in year 1, while others were asked at the end of the questionnaire in year 2. For a specific year all respondents answered the questions in the same order. So every wave of the survey has its own questionnaire with some slight changes between them.
            Last edited by Yanis Rahmouni; 02 Aug 2023, 05:56.

            Comment


            • #7
              Thanks for the extra details (not yet the number of people in the survey, which may or not be relevant).

              I see in principle. In practice I doubt whether this device will help to see any structure in the data arising from question order effects.

              Comment

              Working...
              X