Announcement

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

  • How to create a stacked bar chart with two wheat types for each year?

    Hello everyone;

    I have the following dataset showing the evolution of the producer base price and the subsidy for two types of wheat (Durum Wheat and Soft Wheat) over the period 2020–2026:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int var1 str30 bldur str10 var3 str30 bltendre str10 var5
       . "Prix ??de base à la production" "Subvention" "Prix ??de base à la production" "Subvention"
    2020 "62"                             "20"         "47"                             "12"        
    2021 "65"                             "22"         "50"                             "17"        
    2022 "90"                             "40"         "70"                             "30"        
    2023 "90"                             "50"         "70"                             "40"        
    2024 "90"                             "50"         "70"                             "40"        
    2025 "100"                            "40"         "80"                             "30"        
    2026 "100"                            "40"         "80"                             "30"        
    end
    I would like to create a stacked bar chart in Stata to illustrate the evolution of the producer base price and the subsidy for each type of wheat over the period 2020–2026. More specifically, I would like the graph to have the following characteristics:
    1. Two bars for each year, one representing Durum Wheat and the other representing Soft Wheat.
    2. Each bar should be divided into two segments with different colours:
      • The first segment represents the producer base price.
      • The second segment represents the subsidy.
    3. The two bars corresponding to each year should be grouped together, with the year displayed on the x-axis and the wheat type (Durum Wheat and Soft Wheat) identified above or below each bar.
    4. The y-axis should represent the values of the producer base price and the subsidy, expressed in Tunisian dinars per quintal (TND/quintal).
    5. A legend should distinguish between the producer base price and the subsidy.
    In other words, I would like to have seven groups of bars (one group for each year), with two stacked bars within each group (Durum Wheat and Soft Wheat).

    Could anyone please suggest how to reshape my dataset, if necessary, and which Stata commands (particularly graph bar) I should use to produce this type of graph?

    Thanks very much for the help!

  • #2
    Before you can make your graph, I think you need to change the way you're reading in your data. I think you want all of the variables to be numeric, don't you? How about something like this?

    Code:
    clear
    input year prix1 subvent1 prix2 subvent2 
    2020 62   20   47   12        
    2021 65   22   50   17        
    2022 90   40   70   30        
    2023 90   50   70   40        
    2024 90   50   70   40        
    2025 100  40   80   30        
    2026 100  40   80   30        
    end
    
    label variable prix1 "Durum prix de base"
    label variable subvent1 "Durum subsidy"
    label variable prix2 "Soft prix de base" 
    label variable subvent2 "Soft subsidy"
    --
    Bruce Weaver
    Email: [email protected]
    Version: Stata/MP 19.5 (Windows)
    Crypticity belongs in crosswords, not code! 🤨

    Comment


    • #3
      I agree with Bruce Weaver.

      graph bar can't help easily here.

      Here is some technique. Naturally you may want or even need to change some details. In particular you would need to change colours from stc1 and stc2 if you are using a version of Stata some versions old.

      Code:
      clear
      input year prix1 subvent1 prix2 subvent2 
      2020 62   20   47   12        
      2021 65   22   50   17        
      2022 90   40   70   30        
      2023 90   50   70   40        
      2024 90   50   70   40        
      2025 100  40   80   30        
      2026 100  40   80   30        
      end
      
      label variable prix1 "Durum prix de base"
      label variable subvent1 "Durum subvention"
      label variable prix2 "Soft prix de base" 
      label variable subvent2 "Soft subvention"
      
      gen total1 = prix1 + subvent1
      gen total2 = prix2 + subvent2 
      
      gen yearL = year - 0.15 
      gen yearR = year + 0.15
      
      twoway bar prix1 yearL, barw(0.3 ) lcolor(stc1) fcolor(stc1) ///
      || rbar prix1 total1 yearL, barw(0.3 ) lcolor(stc1) fcolor(stc1*0.5) ///
      || bar prix2 yearR, barw(0.3 ) lcolor(stc2) fcolor(stc2) ///
      || rbar prix2 total2 yearR, barw(0.3 ) lcolor(stc2) fcolor(stc2*0.5) ///
      legend(order(1 "`: var label prix1'" 2 "`: var label subvent1'" 3 "`: var label prix2'" 4 "`: var label subvent2'" )) xla(2020/2026) ytitle(Quelque chose)
      Putting a legend along the top with say legend(pos(12) row(1)) or legend(pos(12) col(2)) may appeal, as may jiggling the order of elements.

      Click image for larger version

Name:	wheat.png
Views:	1
Size:	50.2 KB
ID:	1788312

      Comment


      • #4
        Nick Cox, I find your comment that "graph bar can't help easily here" interesting, given the solutions you posted here: At first glance, the graph bar solution does not strike me as more abstruse than the twoway bar solution. YMMV, of course! And maybe I'd change my mind after a few more glances.
        --
        Bruce Weaver
        Email: [email protected]
        Version: Stata/MP 19.5 (Windows)
        Crypticity belongs in crosswords, not code! 🤨

        Comment


        • #5
          Bruce Weaver Thanks for reminding me of that thread on Stack Overflow (SO).

          The distinction we both know is that graph bar and twoway bar are different commands, confusing though that can be. Even those familiar with both commands can be taxed to know quickly which is better for which kinds of bar chart.

          This kind of puzzlement is common even for what seem simple and mundane graphical tasks. A user thinks "All I want is a graph of y versus x" -- and they are right -- but to code that we need to know whether y and x are already variables or need extra calculations, as well as whatever extra requirements are needed but not yet stated.

          Now graph bar supports stacking but what the OP is asking for there on SO is four different colours, and that seems to be needed here too, or at a minimum highly desirable to make most sense

          The code I posted on SO uses graph bar to produce stacking with two colours only and then twoway bar to produce stacking with four colours. The graph displayed on SO uses the twoway bar code.

          If anyone has a simple graph bar solution to the problem here in this thread, that would be interesting, and a helpful refutation of my comment.

          #2 was posted at 23:32 local time, but in any case I should have noticed that the bars should start at zero! Here is a version of the code that corrects that and adds some other changes which seem to me minor improvements.

          Code:
          clear
          input year prix1 subvent1 prix2 subvent2
          2020 62   20   47   12        
          2021 65   22   50   17        
          2022 90   40   70   30        
          2023 90   50   70   40        
          2024 90   50   70   40        
          2025 100  40   80   30        
          2026 100  40   80   30        
          end
          
          label variable prix1 "Durum prix de base"
          label variable subvent1 "Durum subvention"
          label variable prix2 "Soft prix de base"
          label variable subvent2 "Soft subvention"
          
          gen total1 = prix1 + subvent1
          gen total2 = prix2 + subvent2
          
          gen yearL = year - 0.15
          gen yearR = year + 0.15
          
          twoway bar prix1 yearL, barw(0.3 ) base(0) lcolor(stc1) fcolor(stc1) ///
          || rbar prix1 total1 yearL, barw(0.3 ) lcolor(stc1) fcolor(stc1*0.5) ///
          || bar prix2 yearR, barw(0.3 ) lcolor(stc2) fcolor(stc2) ///
          || rbar prix2 total2 yearR, barw(0.3 ) lcolor(stc2) fcolor(stc2*0.5) ///
          legend(order(2 "`: var label subvent1'" 4 "`: var label subvent2'" 1 "`: var label prix1'"   3 "`: var label prix2'" )) ///
          legend(pos(12) col(2)) yla(0(20)140) xla(2020/2026, noticks) ytitle(Prix et subventions (TND/quintal))
          Click image for larger version

Name:	wheat2.png
Views:	1
Size:	52.2 KB
ID:	1788327




          Last edited by Nick Cox; Today, 04:29.

          Comment

          Working...
          X