Announcement

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

  • Bar graph with y-axis determined by different variables

    Dear everyone,

    I am trying to create a bar graph, but have several expenditure variables that I fail to show in a combined graph.

    I am working with panel data and would like to show this table as example for the data structure:
    id year food expenditure clothing expenditure education expenditure ... Management Type
    1 2017 63 11 24 1
    1 2018 62 12 22 1
    1 2019 67 14 21 0
    Now I attempted to create a graph. I will attach a sketch of my idea below the text, but am also going to explain my idea in case the picture can't be shown correctly.

    On the y axis, I want to show the expenditures numbers. I found some advice in this forum regarding the long format and using a stack command, but my experience in using long-form data is extremely limited. The only way I managed to create the long form was connected to also dropping the management variable.

    On the x Axis, I would like to see the names of the expenditure types. Like Food, Clothing, etc.

    The bars above each expenditure type should always be in one color for the mean expenditure of management group 1, and another color for the mean expenditure of management group 0.

    My numerous attempts to create this chart led me to various issues that left me with code that is so far from being helpful here, that I am not attaching any of my tries. But please let me know, if I missed providing information necessary to help me.

    Thank you already for your feedback.


    Here is the sketch:
    Click image for larger version

Name:	Untitled 2.jpeg
Views:	1
Size:	362.2 KB
ID:	1780658



  • #2
    It seems that you want means of three expenditure types, subdivided by management type.

    I use here statplot from SSC.

    Code:
    set scheme stcolor 
    
    clear 
    input id    year    food     clothing education    type
    1    2017    63    11    24        1
    1    2018    62    12    22        1
    1    2019    67    14    21        0
    end 
    
    graph bar (mean) food clothing education, over(type)
    
    statplot food clothing education, over(type) recast(bar) asyvars
    Click image for larger version

Name:	expemditure.png
Views:	1
Size:	29.0 KB
ID:	1780672

    Comment


    • #3
      Here is an alternate way to present this. It differs a bit from what you requested, because reports the management type directly on the axis, rather than using a legend. I tend to prefer this because it reduces the amount of eye movement required of the reader to gather the information needed to understand what each bar represents.

      Code:
      clear 
      input id    year    food     clothing education    type
      1    2017    63    11    24        1
      1    2018    62    12    22        1
      1    2019    67    14    21        0
      end 
      
      rename (food clothing education) expenditure=
      reshape long expenditure, i(id year) j(category) string
      graph bar (mean) expenditure, over(type) over(category)
      It also uses only native Stata commands, though that only matters if you are working in an environment that prohibits, or seriously encumbers, installing user-written commands.

      Comment


      • #4
        Dear Nick Cox, Dear Clyde Schechter,

        Thank you so much for your insights. While I spent hours dismantling my data to arrive at a solution, your codes both immediately worked perfectly and created the picture pretty much exactly like I hoped for it in the sketch.

        Really incredible, made my day.

        Thank you so much an best wishes,
        Björn
        Last edited by Bjorn Becker; 06 Aug 2025, 01:35.

        Comment


        • #5
          Thanks for the nice thanks, but we have better news.

          I was talking to Hua Peng of StataCorp suggesting that there should be a better way of doing it and he had the best answer of all. You can do this in Stata 19, as I should have registered.

          Here is a worked example with some technique. This is written backwards to allow focus on what changes between two versions of the graph. The option choices were worked out slowly from defaults and initial rough guesses.

          Code:
          webuse nlswork, clear
          
          local opts  legend(row(1) pos(12)) ytitle(Proportion) yla(0 "0" 0.1(0.1)0.6, format(%02.1f))
          local opts `opts' blabel(bar, format(%3.2f))
          local which `" 1 "College graduate" 2 "Central city" 3 "South" "'
          
          graph bar collgrad c_city south, over(race) `opts' legend(order(`which')) name(G1, replace)
          
          graph bar collgrad c_city south, over(race, relabel(`which')) `opts' groupyvars name(G2, replace)
          
          graph combine G1 G2
          Click image for larger version

Name:	groupyvars.png
Views:	1
Size:	42.4 KB
ID:	1780754

          Comment


          • #6
            Dear Nick Cox,

            I am really grateful for you bringing this up with Mr. Peng and for this improved version.

            I will soon upgrade to STATA 19 and implement it thereafter.

            Best wishes and thank you again.

            Comment

            Working...
            X