Announcement

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

  • Bar plot for ranking products across countries

    Dear All

    I am trying to visualise my dataset in such a way that I can show the ranking of production quantities of different fruits and vegetables across EU Member States.
    In my database I have 18 different fruit and vegetable products for 27 MS. What I would like is, for each country, to highlight the top 5 products by production quantity.

    I have tried the following code:

    Code:
    tabplot product MS [aw= productionqty ] if rank<=5, title("Top 5 Products by Country (2023)")
    However, the output does not show the actual production quantities, so it is not very informative.

    Something similar to what Nick Cox proposed in post #6 here would be ideal.
    Any other idea would be appreciated.

    Many thanks in advance for your help.

    Best regards,
    Federica

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long(MS product) float(productionqty rank)
    1 11      0 18
    1 16      0 17
    1  3      0 16
    1 13      0 15
    1  7      0 14
    1 10      0 13
    1 12      0 12
    1  9      0 11
    1  8      0 10
    1  5      0  9
    1 14   1.21  8
    1  6   1.24  7
    1 18   2.26  6
    1  2   4.67  5
    1  4   7.28  4
    1 15  30.57  3
    1 17  56.95  2
    1  1 191.91  1
    2  9      0 18
    2 13      0 17
    2 10      0 16
    2  5      0 15
    2 11      0 14
    2  2      0 13
    2  8      0 12
    2 18      0 11
    2 16      0 10
    2 12      0  9
    2 14      0  8
    2  3      0  7
    2  7      0  6
    2  6  12.28  5
    2  4 141.62  4
    2  1 203.01  3
    2 17  289.7  2
    2 15 381.31  1
    3  9      0 18
    3 13      0 17
    3  3      0 16
    3 16      0 15
    3 10      0 14
    3  5      0 13
    3  8    .25 12
    3 12    .36 11
    3  4   2.13 10
    3 15   2.51  9
    3  6  10.24  8
    3 14  10.44  7
    3  2  10.84  6
    3  7  12.32  5
    3 11  29.49  4
    3  1  34.93  3
    3 18  80.13  2
    3 17  85.05  1
    4 16      0 18
    4  3      0 17
    4  5      0 16
    4  8      0 15
    4  2    .27 14
    4 13    .31 13
    4  9    .37 12
    4 15    .66 11
    4 12   1.14 10
    4 14   1.57  9
    4  6   1.68  8
    4  7   1.75  7
    4  4   2.12  6
    4 11   4.98  5
    4 17   17.9  4
    4 18  26.44  3
    4 10  43.56  2
    4  1  61.53  1
    5 16      0 18
    5  8    .15 17
    5 15    .46 16
    5 12    .73 15
    5  2    .93 14
    5  4   1.21 13
    5  6   1.46 12
    5 14   1.56 11
    5  5   1.98 10
    5  1    2.9  9
    5  7    3.1  8
    5  9   4.15  7
    5 11    5.5  6
    5  3   6.01  5
    5 17  12.97  4
    5 18  13.06  3
    5 10  15.59  2
    5 13  15.62  1
    6 12      0 18
    6  3      0 17
    6 10      0 16
    6  9      0 15
    6  8      0 14
    6 11      0 13
    6 16      0 12
    6  5      0 11
    6  6      0 10
    6 13      0  9
    end
    label values MS MS
    label def MS 1 "Austria", modify
    label def MS 2 "Belgium", modify
    label def MS 3 "Bulgaria", modify
    label def MS 4 "Croatia", modify
    label def MS 5 "Cyprus", modify
    label def MS 6 "Czechia", modify
    label values product product
    label def product 1 "Apples", modify
    label def product 2 "Apricots", modify
    label def product 3 "Bananas", modify
    label def product 4 "Cauliflower and broccoli", modify
    label def product 5 "Clementines", modify
    label def product 6 "Eggplants", modify
    label def product 7 "Grapes for table use", modify
    label def product 8 "Kiwis", modify
    label def product 9 "Lemons", modify
    label def product 10 "Mandarins", modify
    label def product 11 "Muskmelons", modify
    label def product 12 "Nectarines", modify
    label def product 13 "Oranges", modify
    label def product 14 "Peaches", modify
    label def product 15 "Pears", modify
    label def product 16 "Satsumas", modify
    label def product 17 "Tomatoes for fresh consumption", modify
    label def product 18 "Watermelons", modify

  • #2
    tabplot is from the Stata Journal.

    To see quantities too, you could add an option such as

    Code:
    showval(format(%2.1f))


    I got as far as this

    Code:
    label def product 17 "Tomatoes", modify
    graph hbar (asis) productionqty if rank <= 5, over(product, sort(rank)) over(MS) nofill


    while worrying how messy that would get with all 26 states.
    Click image for larger version

Name:	fruits.png
Views:	1
Size:	45.9 KB
ID:	1781700


    Comment


    • #3
      Alphabetical ordering of countries isn't obviously essential here. For a longer riff, see e.g. https://journals.sagepub.com/doi/pdf...6867X211045582

      Oddly enough, Austria first! was mocked by Howard Wainer as a graphical choice back in 1984.

      Elsewhere he changed the example to Alabama first! A Scottish version might be Aberdeen first!
      Last edited by Nick Cox; 09 Sep 2025, 11:01.

      Comment


      • #4
        https://www.jstor.org/stable/2683253 is a link for Wainer.

        Comment


        • #5
          FWIW, Austria First must arise from a rather constricted view of the world, as, at least currently, there are 9 countries that precede it alphabetically: Afghanistan, Albania, Algeria, Andorra, Angola, Antigua and Barbuda, Argentina, Armenia, and Australia. My first impulse was to think it was Eurocentric, but both Andorra and Albania are in Europe. Perhaps the EU? (I did not read the Wainer article, which, perhaps, discloses its universe of discourse.)

          Comment


          • #6
            Wainer was just using some example, and flexible enough to change the example later. If we move beyond countries and states (US sense) we would need to mention Aalborg too and I doubt that is the end of it (or the beginning?).

            Comment


            • #7
              Dear Nick,

              Thank you very much for your help.

              I also experimented with hbar, but the resulting graph turned out too crowded to be easily readableGraph.gph. I then tried splitting the countries into groups of 10 (using MS_gr) and plotting them separately, but even then the figures were not very clear.Bar_quantity_byMS_gr.gph

              To be honest, I am starting to wonder whether this information can really be represented effectively in a single graph.

              And apologies for not being more explicit earlier about the data source — @Clyde is correct, the database includes only EU Member States.

              Thanks
              Federica

              Comment


              • #8
                26 x 5 will be a struggle without some compromise.

                Comment

                Working...
                X