Announcement

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

  • How to Create A Self-Adjusting 'Others' Category

    Hello Statalist Community,

    I am working on creating a bar graph in Stata, where I want to display the top 5 countries (sorted in descending order by the size of their observations), and also aggregate the counts of the remaining categories to create a new "Others" category. I
    I would love to have the "Other" category stacked and self-adjusting, like in the example below.

    So far, I've ranked the variable and retained the top 5 (using the command myaxis countries_top = countries, sort(count) descending keep if countries_top <= 5). However, I need assistance in adding the "Other" category to my graph and stacking only within this category to show the sizes of the other countries.

    Can you help me with how to make these adjustments in my Stata graph, specifically focusing on stacking within the "Other" category?

    Thank you in advance for your assistance!

    Kind regards
    Franciska Strittmatter










    In this post, you will learn how to create a chart like this where the category 'Other' is dynamically self-adjusting itself based on the data. Data for this post can be downloaded from here .

  • #2
    This may help, although there are difficulties in understanding exactly you want.

    You don't give a data example: please see FAQ Advice #12.

    Sorry, but I have never used Tableau and won't read your attachment to see if it explains more. It's a better use of my time to show some Stata technique.

    The graph shown is about different kinds of data, and more crucially it is hard to see what the rules would be for your graph, as the bars in the displayed graph are ordered by one variable (and so not the other), and how would that carry over to your data? I don't see the logic of including others in the middle of the first 5. Some of the others seem to be larger in value than those displayed separately.

    Here I don't subdivide the others, which would be harder work but perhaps not impossible.

    But there is some technique here that may carry over, noting that -200 is purely empirical for the example. This code would work well only for say two- or three-letter country codes.

    Code:
    webuse grunfeld, clear 
    
    preserve 
    keep if year <= 1938 
    keep if mvalue < . 
    
    bysort year (mvalue) : gen rank = _N - _n + 1 
    by year : egen toshow = total((rank > 5) * mvalue) 
    replace toshow = mvalue if rank <= 5 
    
    gen x = -200 
    
    twoway bar toshow rank if rank <= 5, ///
    barw(0.9) yla(1/5 6 "others", tlc(none)) ysc(reverse) horizontal by(year, title(Market value) legend(off) note("")) ///
    || bar toshow rank if rank == 6, barw(0.9) ysc(reverse) horizontal ///
    || scatter rank x if rank <= 5, ms(none) mla(company) mlabsize(medlarge) mlabpos(0) 
    
    
    list year company toshow rank if rank <= 6  , sepby(year)
    
         +--------------------------------+
         | year   company   toshow   rank |
         |--------------------------------|
      5. | 1935         6   755.11      6 |
      6. | 1935         9    290.6      5 |
      7. | 1935         4    417.5      4 |
      8. | 1935         3   1170.6      3 |
      9. | 1935         2   1362.4      2 |
     10. | 1935         1   3078.5      1 |
         |--------------------------------|
     15. | 1936         9   957.34      6 |
     16. | 1936         8      516      5 |
     17. | 1936         4    837.8      4 |
     18. | 1936         2   1807.1      3 |
     19. | 1936         3   2015.8      2 |
     20. | 1936         1   4661.7      1 |
         |--------------------------------|
     25. | 1937         9   1043.3      6 |
     26. | 1937         8      729      5 |
     27. | 1937         4    883.9      4 |
     28. | 1937         2   2676.3      3 |
     29. | 1937         3   2803.3      2 |
     30. | 1937         1   5387.1      1 |
         |--------------------------------|
     35. | 1938         9   839.32      6 |
     36. | 1938         4    437.9      5 |
     37. | 1938         8    560.4      4 |
     38. | 1938         2   1801.9      3 |
     39. | 1938         3   2039.7      2 |
     40. | 1938         1   2792.2      1 |
         +--------------------------------+

    Click image for larger version

Name:	top6.png
Views:	1
Size:	33.5 KB
ID:	1730685

    Comment

    Working...
    X