Announcement

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

  • stacked bar graph for different years showing shares of countries

    Dear all,

    I want to produce a graph from a dataset containing the production of aluminum in different years by different countries. I have a long panel format, i.e. each year is repeated for all the countries and the production value is under the variable "value". Is there a way I can do this without having to change the format of my data and for example by generating the values for different countries as new variables?
    Secondly, I need to make another graph showing the total product of all countries for each year. I am new to graphs and would appreciate your help. You could find my dataset attached.

    Best regards,
    Shadi
    Attached Files
    Last edited by Shadi Shad; 08 Oct 2021, 03:53.

  • #2
    You have so many countries, that such a graph would be unreadable. What do you want to show? Maybe we can find a better way to visualize that.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Thank you for your answer. Yes, I suspected that it would look unreadable. So I want to show the annual product of aluminium for each country. Maybe I can do it for country by country and then show a time series of the annual product and then combine the graphs into one graph? Would that work?

      Comment


      • #4
        Here is an option that requires Nick Cox's labmask and fabplot (type in Stata search labmask and search fabplot and follow the instructions to install those packages)

        Code:
        set scheme s1color
        clear
        use "https://www.statalist.org/forums/filedata/fetch?id=1630908"
        bys ccode : egen tot = total(value)
        bys tot ccode (year) : gen order = _n == 1
        replace order = sum(order)
        replace order = -order
        labmask order, value(countryname)
        
        fabplot line value year if value > 0, by(order, compact) yscale(log) ///
           ylab(0.001 0.01 0.1 1 10 100 1000 10000)
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	537.9 KB
ID:	1630928
        ---------------------------------
        Maarten L. Buis
        University of Konstanz
        Department of history and sociology
        box 40
        78457 Konstanz
        Germany
        http://www.maartenbuis.nl
        ---------------------------------

        Comment


        • #5
          Originally posted by Maarten Buis View Post
          Here is an option that requires Nick Cox's labmask and fabplot (type in Stata search labmask and search fabplot and follow the instructions to install those packages)
          Thank you so much, it is great how fast it worked. This is a very nice illustration of the data. But one more question: Only for my learning, is there a way that we can plot the same data as a stacked graph, i.e. all countries are shown in the same graph with different colors for the same time period? Would I have then to create different variables for each country? (this is the only case I could actually find when I search, so no one worked with long panel to derive such a panel in the forums, it's rather always the year variable and then a separate country variable for each country that for example in my case shows the product value) Please let me know if this was not clear.

          Comment


          • #6
            Thanks for Maarten Buis for the sales pitch.

            WIth myaxis from SSC or (now) the Stata Journal you can get the same graph with

            Code:
            set scheme s1color
            clear
            use "https://www.statalist.org/forums/filedata/fetch?id=1630908"
            
            myaxis order=countryname, sort(total value) descending
            
            fabplot line value year if value > 0, by(order, compact) yscale(log) ///
               ylab(0.001 0.01 0.1 1 10 100 1000 10000)
            Sources

            fabplot https://www.stata-journal.com/article.html?article=gr0087

            https://www.statalist.org/forums/for...ailable-on-ssc (skim and skip to later posts)


            myaxis https://www.stata-journal.com/article.html?article=st0654

            https://www.statalist.org/forums/forum/general-stata-discussion/general/270264-subsetplot-available-on-ssc




            Turning to the question in #5

            is there a way that we can plot the same data as a stacked graph, i.e. all countries are shown in the same graph with different colors for the same time period? Would I have then to create different variables for each country? (this is the only case I could actually find when I search, so no one worked with long panel to derive such a panel in the forums, it's rather always the year variable and then a separate country variable for each country that for example in my case shows the product value)
            separate exists to create several variables, so that part is easier than you fear. The bigger deal will be getting different colours and managing a legend bigger than the graph. You have 52 countries here. That can only end disappointingly.

            Even here, do you really need all 52?
            fabplot has an option that lets you highlight a subset individually, say 9 or 12, but have all countries as backdrop.

            Comment


            • #7
              Originally posted by Nick Cox View Post

              separate exists to create several variables, so that part is easier than you fear. The bigger deal will be getting different colours and managing a legend bigger than the graph. You have 52 countries here. That can only end disappointingly.

              Even here, do you really need all 52?
              fabplot has an option that lets you highlight a subset individually, say 9 or 12, but have all countries as backdrop.
              Thank you for your response, I can imagine what you say. But what if I only kept 10 of the countries or 5 of them? My intention to ask was only to learn how to make a stacked bar chart with shares of the countries indicated by color in the given time period, having this format of long panel without ideally having to do any reshape or separating observations. Is there a convenient way to do this say for 5 countries of the list?

              Comment


              • #8
                As implied you can do it. With that many years stacked bars and stacked areas will look very similar. graph bar might help, or possibly you could combine twoway area and twoway rarea.

                My distaste for that design when there is a better one makes me reluctant even to think about code;

                Comment

                Working...
                X