Announcement

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

  • Graph counts of categorical variable by year

    Hello,

    I have the counts of a categorical variable by year. Please suggest a good way to graph such data. An example:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte finyear str39 workcategoryname int _freq
    10 "Anganwadi/Other Rural Infrastructure"    2
    11 "Anganwadi/Other Rural Infrastructure"   14
    12 "Anganwadi/Other Rural Infrastructure"    7
    13 "Anganwadi/Other Rural Infrastructure"    6
    14 "Anganwadi/Other Rural Infrastructure"   22
    15 "Anganwadi/Other Rural Infrastructure"    5
    16 "Anganwadi/Other Rural Infrastructure"   60
    17 "Anganwadi/Other Rural Infrastructure"  105
    18 "Anganwadi/Other Rural Infrastructure"  152
    19 "Anganwadi/Other Rural Infrastructure"  190
    20 "Anganwadi/Other Rural Infrastructure" 2518
    21 "Anganwadi/Other Rural Infrastructure"  764
    22 "Anganwadi/Other Rural Infrastructure"  505
    23 "Anganwadi/Other Rural Infrastructure"  213
    24 "Anganwadi/Other Rural Infrastructure"  526
    25 "Anganwadi/Other Rural Infrastructure"  112
    10 "Bharat Nirman Sewa Kendra"              86
    11 "Bharat Nirman Sewa Kendra"             271
    12 "Bharat Nirman Sewa Kendra"             345
    13 "Bharat Nirman Sewa Kendra"             162
    14 "Bharat Nirman Sewa Kendra"             235
    15 "Bharat Nirman Sewa Kendra"              31
    16 "Bharat Nirman Sewa Kendra"              51
    17 "Bharat Nirman Sewa Kendra"              22
    18 "Bharat Nirman Sewa Kendra"              19
    19 "Bharat Nirman Sewa Kendra"               2
    13 "Coastal Areas"                          13
    14 "Coastal Areas"                           4
    16 "Coastal Areas"                           2
    17 "Coastal Areas"                           3
    19 "Coastal Areas"                           4
    20 "Coastal Areas"                           2
    22 "Coastal Areas"                           1
    24 "Coastal Areas"                           1
    10 "Drought Proofing"                      406
    11 "Drought Proofing"                      478
    12 "Drought Proofing"                      153
    13 "Drought Proofing"                      163
    14 "Drought Proofing"                      420
    15 "Drought Proofing"                      299
    16 "Drought Proofing"                      463
    17 "Drought Proofing"                      867
    18 "Drought Proofing"                     1062
    19 "Drought Proofing"                      585
    20 "Drought Proofing"                     4292
    21 "Drought Proofing"                     2040
    22 "Drought Proofing"                     2526
    23 "Drought Proofing"                     1405
    24 "Drought Proofing"                     5624
    25 "Drought Proofing"                      437
    end
    Thank you.

  • #2
    Without know just what questions you hope to answer with this data, I cannot suggest a "best" way of doing this. But here is one way that puts all of the data in a single graph, readably, and enables you to see the variation in each category.

    Code:
    encode workcategoryname, gen(category)
    
    xtset category finyear
    
    xtline _freq, yscale(log) ylabel(1 10 100 1000 10000) overlay
    The log scale on the vertical axis was chosen because with an ordinary linear scale, the small values for Coastal Areas and Bharat Nirman Sewa Kendra get squashed to the point that their variation over time cannot be seen.

    But depending on your goals, there are many other ways to display this data that might be more suitable.

    Comment


    • #3
      Clyde Schechter is as usual entirely correct, especially that we need more information to give good advice.

      An easy guess is that you have about 16 different years. It's harder to guess how many categories you have. It's all too easy to get to the point where a large fraction of a display is taken up by a legend.

      Comment


      • #4
        I have 16 categories.

        Comment


        • #5
          With as many as 16 categories, graphs made in the style of #2 might get a tad messy. Perhaps try sparkline or multiline, both contributed by Nick Cox and available via ssc install?

          With your example data,
          Code:
          sparkline _freq finyear, over(workcategoryname)
          produces
          Click image for larger version

Name:	Screenshot 2025-09-27 at 1.59.15 AM.png
Views:	1
Size:	411.9 KB
ID:	1782089

          Unlike sparkline, multiline does not have an over() option, so we need to first create the requisite variables, and then graph them:
          Code:
          separate _freq, by(workcategoryname) veryshortlabel
          multiline _freq? finyear, missing
          to get
          Click image for larger version

Name:	Screenshot 2025-09-27 at 2.13.17 AM.png
Views:	1
Size:	453.1 KB
ID:	1782090

          Last edited by Hemanshu Kumar; 26 Sep 2025, 14:43.

          Comment


          • #6
            Thank you, Hemanshu.

            Comment

            Working...
            X