Announcement

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

  • Catplot sort

    Using Stata 11, catplot(SSC)

    I have a binary variable (var1) and a categorical variable (var2) with ~30 categories. I am plotting frequency of var2 over var1 using catplot:

    catplot var1 var2, recast(bar) var2opts(sort(2) descending) asyvars

    This is almost exactly what I want, except that the sorting is only applying to var1==1, but not the var2 categories that var1==0. See below (please excuse the x-axis labels).

    Any ideas would be much appreciated!

    Thanks,
    Katrina

  • #2
    Your attachment isn't readable (by me at least).

    Comment


    • #3
      A reproducible example here is

      Code:
      sysuse auto, clear 
      catplot foreign rep78, recast(bar) var2opts(sort(2) descending) asyvars
      If your graph does not look similar, then you need to tell us more about your var2. Alternatively, tell us what the graph is like, not that it isn't what you want.

      By the way, asking for a vertical bar chart with 30 categories is almost surely doomed to failure. There isn't room to show the category labels intelligibly. That's one of the reasons why catplot defaults to hbar.

      Comment


      • #4
        Thanks for the reply, Nick, and apologies for the attachment issue. I have re-attached in .jpg format, which I hope will work.

        An additional note about my variables is that each var2 category is either var1==1 or var1==0. Every individual in a var2 category has the same var1 value.

        The code is the same as I posted previously,

        catplot var1 var2, recast(bar) var2opts(sort(2) descending) asyvars

        This code is sorting var2 in descending frequency when var1==1, but not for the var2 categories for which var1==0. Several attempts at altering the code have been fruitless. Any ideas?

        Thanks for any help, and apologies again.

        Comment


        • #5
          The FAQ does have advice here. The FAQ is not some arbitrary set of rules to trap newbies; it is packed with advice to help you get answers, not more questions.

          You can attach Stata graphs or other images. Note, however, that Stata graphs and other images are highly readable when inserted as .png file attachments (start with the Clipboard icon) and far less readable if inserted as photos (using the Camera icon).
          Sorry, I still can't read your axis labels. My main comment remains the same: you need to tell us more about var2. Specifically, show us the result of

          Code:
          describe var2
          tab var2
          tab var2, nola
          Please copy and paste the results from Stata so that they are readable, namely between CODE delimiters. If you are unclear on what that means, the FAQ explains.

          Comment


          • #6
            Hi Nick,

            My var2 is called serotype2 in my dataset, and is described below. It is very large with many categories, but my catplots are of subgroups of my data, and will be limited to far fewer categories than all those actually contained within serotype2.

            These catplots are not for publication purposes, and are only being used as communication between coauthors to describe the data, hence my lack of concern about my current axis labels, which I realize still need work.

            Click image for larger version

Name:	Graphpng.png
Views:	1
Size:	32.3 KB
ID:	312163



            Code:
             describe serotype2
            
                          storage  display     value
            variable name   type   format      label      variable label
            --------------------------------------------------------------------------------
            serotype2       long   %22.0g      serotype2
                                                          SEROTYPE
            
            . tab serotype2
            
                          SEROTYPE |      Freq.     Percent        Cum.
            -----------------------+-----------------------------------
                               11A |          1        0.05        0.05
                               18A |          1        0.05        0.10
                               18C |          2        0.10        0.20
                               19A |          2        0.10        0.30
                               19F |          2        0.10        0.40
                               23F |          4        0.20        0.60
                                6A |          5        0.25        0.85
                                6B |          8        0.40        1.25
                                7F |          1        0.05        1.30
                                9A |          1        0.05        1.35
                                 1 |        196        9.80       11.15
                                10 |          6        0.30       11.45
                               10A |          5        0.25       11.70
                               10B |          3        0.15       11.85
                               11A |          1        0.05       11.90
                                12 |          8        0.40       12.30
                               12A |         10        0.50       12.80
                               12F |         43        2.15       14.95
                                13 |          3        0.15       15.10
                                14 |        260       13.00       28.10
                                15 |          4        0.20       28.30
                               15A |          1        0.05       28.35
                               15B |          3        0.15       28.50
                               15C |          3        0.15       28.65
                               15F |          1        0.05       28.70
                                16 |          5        0.25       28.95
                               16F |          3        0.15       29.10
                               17F |          6        0.30       29.40
                                18 |          1        0.05       29.45
                               18A |          4        0.20       29.65
                             18A/B |          1        0.05       29.70
                               18B |          1        0.05       29.75
                               18C |         54        2.70       32.45
                               18F |          5        0.25       32.70
                                19 |          6        0.30       33.00
                               19A |        116        5.80       38.80
                               19F |         84        4.20       43.00
                             19F/A |          3        0.15       43.15
                            19NonF |          1        0.05       43.20
                                 2 |         63        3.15       46.35
                                20 |          3        0.15       46.50
                                21 |          2        0.10       46.60
                                22 |          1        0.05       46.65
                               22A |          1        0.05       46.70
                                23 |          7        0.35       47.05
                               23A |          4        0.20       47.25
                               23B |          8        0.40       47.65
                               23F |        131        6.55       54.20
                                24 |          2        0.10       54.30
                                27 |          3        0.15       54.45
                                29 |          6        0.30       54.75
                                 3 |         12        0.60       55.35
                                31 |          3        0.15       55.50
                                33 |         16        0.80       56.30
                               33A |          1        0.05       56.35
                               33F |          4        0.20       56.55
                                34 |          2        0.10       56.65
                                35 |          1        0.05       56.70
                               35B |          3        0.15       56.85
                               35F |          1        0.05       56.90
                                38 |          3        0.15       57.05
                                 4 |         33        1.65       58.70
                                45 |          1        0.05       58.75
                                46 |          4        0.20       58.95
                                 5 |        278       13.90       72.85
                                 6 |          2        0.10       72.95
                                6A |        144        7.20       80.15
                                6B |        157        7.85       88.00
                                7A |          1        0.05       88.05
                              7B/C |          1        0.05       88.10
                                7F |         24        1.20       89.30
                                 8 |         12        0.60       89.90
                                 9 |          4        0.20       90.10
                                9A |         11        0.55       90.65
                                9L |         10        0.50       91.15
                                9N |          6        0.30       91.45
                                9V |         68        3.40       94.85
                                NT |         78        3.90       98.75
                          OMNI NEG |          3        0.15       98.90
                            Pool P |          1        0.05       98.95
                            Pool C |          2        0.10       99.05
                            Pool E |          4        0.20       99.25
                            Pool G |          3        0.15       99.40
                            Pool H |          5        0.25       99.65
                            Pool I |          2        0.10       99.75
                            Pool Q |          1        0.05       99.80
                            Pool R |          1        0.05       99.85
            [25/38/43/44/45/46/48] |          3        0.15      100.00
            -----------------------+-----------------------------------
                             Total |      2,000      100.00
            
            . tab serotype2, nola
            
               SEROTYPE |      Freq.     Percent        Cum.
            ------------+-----------------------------------
                      1 |          1        0.05        0.05
                      2 |          1        0.05        0.10
                      3 |          2        0.10        0.20
                      4 |          2        0.10        0.30
                      5 |          2        0.10        0.40
                      6 |          4        0.20        0.60
                      7 |          5        0.25        0.85
                      8 |          8        0.40        1.25
                      9 |          1        0.05        1.30
                     10 |          1        0.05        1.35
                     11 |        196        9.80       11.15
                     12 |          6        0.30       11.45
                     13 |          5        0.25       11.70
                     14 |          3        0.15       11.85
                     17 |          1        0.05       11.90
                     18 |          8        0.40       12.30
                     19 |         10        0.50       12.80
                     21 |         43        2.15       14.95
                     22 |          3        0.15       15.10
                     23 |        260       13.00       28.10
                     25 |          4        0.20       28.30
                     26 |          1        0.05       28.35
                     28 |          3        0.15       28.50
                     30 |          3        0.15       28.65
                     31 |          1        0.05       28.70
                     32 |          5        0.25       28.95
                     33 |          3        0.15       29.10
                     35 |          6        0.30       29.40
                     36 |          1        0.05       29.45
                     37 |          4        0.20       29.65
                     38 |          1        0.05       29.70
                     39 |          1        0.05       29.75
                     40 |         54        2.70       32.45
                     42 |          5        0.25       32.70
                     43 |          6        0.30       33.00
                     44 |        116        5.80       38.80
                     46 |         84        4.20       43.00
                     48 |          3        0.15       43.15
                     49 |          1        0.05       43.20
                     50 |         63        3.15       46.35
                     51 |          3        0.15       46.50
                     52 |          2        0.10       46.60
                     53 |          1        0.05       46.65
                     54 |          1        0.05       46.70
                     55 |          7        0.35       47.05
                     56 |          4        0.20       47.25
                     57 |          8        0.40       47.65
                     58 |        131        6.55       54.20
                     61 |          2        0.10       54.30
                     64 |          3        0.15       54.45
                     66 |          6        0.30       54.75
                     67 |         12        0.60       55.35
                     68 |          3        0.15       55.50
                     69 |         16        0.80       56.30
                     70 |          1        0.05       56.35
                     73 |          4        0.20       56.55
                     74 |          2        0.10       56.65
                     75 |          1        0.05       56.70
                     76 |          3        0.15       56.85
                     77 |          1        0.05       56.90
                     79 |          3        0.15       57.05
                     81 |         33        1.65       58.70
                     83 |          1        0.05       58.75
                     84 |          4        0.20       58.95
                     86 |        278       13.90       72.85
                     87 |          2        0.10       72.95
                     88 |        144        7.20       80.15
                     90 |        157        7.85       88.00
                     93 |          1        0.05       88.05
                     94 |          1        0.05       88.10
                     96 |         24        1.20       89.30
                     97 |         12        0.60       89.90
                     98 |          4        0.20       90.10
                     99 |         11        0.55       90.65
                    100 |         10        0.50       91.15
                    101 |          6        0.30       91.45
                    102 |         68        3.40       94.85
                    103 |         78        3.90       98.75
                    104 |          3        0.15       98.90
                    106 |          1        0.05       98.95
                    107 |          2        0.10       99.05
                    108 |          4        0.20       99.25
                    109 |          3        0.15       99.40
                    110 |          5        0.25       99.65
                    111 |          2        0.10       99.75
                    112 |          1        0.05       99.80
                    113 |          1        0.05       99.85
                    114 |          3        0.15      100.00
            ------------+-----------------------------------
                  Total |      2,000      100.00
            
            .

            Comment


            • #7
              Thanks; we're making progress. We'll leave which orientation to use and questions about legibility of axis labels on one side.

              At least part of your problem is now evident if you look at the structure of your variable.

              The default tabulation shows the value labels and that which requests no labels shows the underlying values.

              What they show is this, or so I guess. Your variable is the result of an encode on a messy string variable. You have, for example, two labels that look like "11A", but they can't both be "11A", because otherwise they would not be two labels. There are other similar problems. The more general tell-tale sign is that your value labels do not appear to be in alphanumeric order. Note for example you have "1", "10", "10A", "10B" which are not, it seems, in their correct position within alphanumeric order. Stata doesn't make mistakes of that kind. The most likely explanation is that you have leading spaces or other characters not visible in your string variable, so the first thing to do is to remove the spaces from the original string variable and try again. If that's not available to you you should decode the variable and remove spaces from that.

              The more general point is this: catplot (which is mine, but just a wrapper) and graph bar and its siblings (which do all the heavy lifting) just go by the numeric order of the variable as defined in your data, when you have a numeric variable (as you do); and tabulate shows you what that order is. So essentially if catplot is not producing the graph you want, you need to fix that variable.

              There may be other problems but this is the one to work on first.

              Comment


              • #8
                Thanks again for your feedback. I have done as you suggested, and made corrections to my variable, and have now made the graph with the original string variable, with the same result, and the sort issue I described in my original post.
                Code:
                . tab serotype
                
                              SEROTYPE |      Freq.     Percent        Cum.
                -----------------------+-----------------------------------
                                     1 |        543        6.52        6.52
                                    10 |         40        0.48        7.00
                                   10A |         25        0.30        7.31
                                   10B |          4        0.05        7.35
                                   10F |          1        0.01        7.37
                                    11 |         11        0.13        7.50
                                   11A |          9        0.11        7.61
                                    12 |         14        0.17        7.77
                                   12A |         10        0.12        7.89
                                   12B |          3        0.04        7.93
                                   12F |         98        1.18        9.11
                                    13 |         56        0.67        9.78
                                    14 |      1,198       14.39       24.17
                                 14/6A |          1        0.01       24.19
                                    15 |         30        0.36       24.55
                                   15A |         25        0.30       24.85
                               15A/19A |          1        0.01       24.86
                                   15B |         68        0.82       25.68
                               15B/19F |          1        0.01       25.69
                                   15C |         12        0.14       25.83
                                   15F |          1        0.01       25.84
                                    16 |         49        0.59       26.43
                                   16F |          3        0.04       26.47
                                    17 |         24        0.29       26.76
                                   17F |         13        0.16       26.91
                                    18 |          1        0.01       26.93
                                   18A |         17        0.20       27.13
                                 18A/B |          1        0.01       27.14
                                   18B |          7        0.08       27.23
                                   18C |        189        2.27       29.50
                                18C/14 |          1        0.01       29.51
                                   18F |         15        0.18       29.69
                                    19 |          6        0.07       29.76
                                   19A |        596        7.16       36.92
                                   19B |          5        0.06       36.98
                                   19F |        723        8.69       45.67
                                19F/6B |          1        0.01       45.68
                                 19F/A |          3        0.04       45.72
                                19NonF |          1        0.01       45.73
                                     2 |         64        0.77       46.50
                                    20 |          6        0.07       46.57
                                    21 |         17        0.20       46.77
                                    22 |         21        0.25       47.03
                                   22A |          1        0.01       47.04
                                    23 |          7        0.08       47.12
                                   23A |         29        0.35       47.47
                                   23B |         22        0.26       47.74
                                   23F |        817        9.82       57.55
                                23F/22 |          1        0.01       57.56
                                 23F/8 |          1        0.01       57.58
                                    24 |          6        0.07       57.65
                                    25 |          3        0.04       57.68
                                    26 |          1        0.01       57.70
                                    27 |          6        0.07       57.77
                                    28 |          2        0.02       57.79
                                    29 |         40        0.48       58.27
                                     3 |         90        1.08       59.35
                                    31 |          9        0.11       59.46
                                    33 |         24        0.29       59.75
                                   33A |          4        0.05       59.80
                                   33B |          1        0.01       59.81
                                   33D |          3        0.04       59.85
                                   33F |          6        0.07       59.92
                                    34 |         37        0.44       60.36
                                    35 |          7        0.08       60.45
                                   35B |         12        0.14       60.59
                                   35F |          3        0.04       60.63
                                    37 |          1        0.01       60.64
                                    38 |         13        0.16       60.80
                                    39 |          1        0.01       60.81
                                     4 |        176        2.11       62.92
                                    42 |          1        0.01       62.93
                                    45 |          1        0.01       62.95
                                    46 |          4        0.05       62.99
                                    47 |          1        0.01       63.01
                                     5 |        380        4.57       67.57
                                     6 |          2        0.02       67.60
                                    6A |      1,019       12.24       79.84
                                6A/19F |          1        0.01       79.85
                                    6B |        970       11.65       91.51
                                 6B/9V |          1        0.01       91.52
                                     7 |         42        0.50       92.02
                                    7A |          1        0.01       92.03
                                  7B/C |          1        0.01       92.05
                                    7C |         14        0.17       92.21
                                    7F |         32        0.38       92.60
                                     8 |        113        1.36       93.96
                                     9 |          4        0.05       94.00
                                    9A |         27        0.32       94.33
                                    9L |         22        0.26       94.59
                                    9N |         53        0.64       95.23
                                    9V |        284        3.41       98.64
                                    NT |         88        1.06       99.70
                              OMNI NEG |          3        0.04       99.74
                                Pool C |          2        0.02       99.76
                                Pool E |          4        0.05       99.81
                                Pool G |          3        0.04       99.84
                                Pool H |          5        0.06       99.90
                                Pool I |          2        0.02       99.93
                                Pool P |          1        0.01       99.94
                                Pool Q |          1        0.01       99.95
                                Pool R |          1        0.01       99.96
                [25/38/43/44/45/46/48] |          3        0.04      100.00
                -----------------------+-----------------------------------
                                 Total |      8,323      100.00
                
                . describe serotype
                
                              storage  display     value
                variable name   type   format      label      variable label
                --------------------------------------------------------------------------------
                serotype        str22  %22s                   SEROTYPE
                
                .
                Click image for larger version

Name:	Graph1.png
Views:	2
Size:	32.4 KB
ID:	326289
                Attached Files

                Comment


                • #9
                  Please post your dataset in a public place, or email me it privately. Email addresses for me are pretty visible in various places.

                  Also, please show me the exact catplot command you used to get this graph.

                  Comment


                  • #10
                    Meanwhile, here is a reproducible example showing what I think you want:

                    Code:
                     
                    set scheme s1color 
                    sysuse auto, clear 
                    gen m = substr(make, 1, 1)
                    catplot m foreign, var1opts(sort(1) descending) nofill
                    Click image for larger version

Name:	catplot.png
Views:	1
Size:	27.7 KB
ID:	327351

                    Comment


                    • #11
                      Hi all,

                      FYI, if you want to sort by ascending rather than descending order, you can simply use the -sort- option without specifying descending option.

                      Adam
                      Last edited by Adam Markovitz; 01 May 2017, 13:47.

                      Comment


                      • #12
                        Originally posted by Nick Cox View Post
                        A reproducible example here is

                        Code:
                        sysuse auto, clear
                        catplot foreign rep78, recast(bar) var2opts(sort(2) descending) asyvars
                        If your graph does not look similar, then you need to tell us more about your var2. Alternatively, tell us what the graph is like, not that it isn't what you want.

                        By the way, asking for a vertical bar chart with 30 categories is almost surely doomed to failure. There isn't room to show the category labels intelligibly. That's one of the reasons why catplot defaults to hbar.

                        Hi Nick,

                        I am making a stacked plot, and trying to sort the bars by the % of "Very serious threat" in descending order. Basically, I want to show the blue bars on the left in descending order. I tried the following, but it's not working:

                        Code:
                        catplot  L5, over(countrynew) asyvars stack percent(countrynew) recast(bar) var2opts(sort(2) descending)
                        Here is the dataex:


                        . dataex

                        ----------------------- copy starting from the next line -----------------------
                        Code:
                        * Example generated by -dataex-. For more info, type help dataex
                        clear
                        input str20 countrynew byte L5
                        "Palestine"           3
                        "Botswana"           98
                        "Tajikistan"          1
                        "United Kingdom"      2
                        "Taiwan"              1
                        "China"              98
                        "Laos"               98
                        "United Kingdom"      1
                        "Bangladesh"          1
                        "Malta"               3
                        "United Kingdom"      1
                        "Honduras"            1
                        "India"               1
                        "Kuwait"              3
                        "Kazakhstan"          1
                        "Gambia"              1
                        "Hong Kong"           2
                        "Senegal"             1
                        "Croatia"             1
                        "El Salvador"        98
                        "Kenya"               1
                        "Latvia"              1
                        "Nepal"               2
                        "Chile"               1
                        "Russia"             98
                        "Turkey"              3
                        "Russia"              1
                        "India"               3
                        "France"              1
                        "New Zealand"         1
                        "Colombia"            2
                        "Ghana"              98
                        "Morocco"             1
                        "Vietnam"             1
                        "Spain"               3
                        "Austria"             1
                        "Mongolia"            1
                        "Gambia"              1
                        "Georgia"             1
                        "Iraq"                3
                        "Hungary"             1
                        "Saudi Arabia"        3
                        "Turkey"              1
                        "Bangladesh"          2
                        "Turkmenistan"        1
                        "Italy"               1
                        "Georgia"             2
                        "Poland"              3
                        "Japan"               1
                        "Bulgaria"            1
                        "Mozambique"         98
                        "Saudi Arabia"        3
                        "Kazakhstan"          1
                        "Nigeria"             3
                        "Cambodia"            1
                        "Kazakhstan"         98
                        "Kyrgyzstan"          1
                        "Croatia"             1
                        "New Zealand"         2
                        "Latvia"              1
                        "Mauritius"           2
                        "Niger"               1
                        "Cambodia"            3
                        "Albania"             2
                        "India"              98
                        "Saudi Arabia"        2
                        "Yemen"               2
                        "Italy"               1
                        "Yemen"              98
                        "Gabon"               3
                        "India"               3
                        "Norway"              2
                        "Albania"             3
                        "Nepal"              98
                        "Lithuania"           3
                        "Latvia"              1
                        "Israel"              1
                        "Panama"              2
                        "Niger"               1
                        "Botswana"            1
                        "Turkey"              1
                        "Slovenia"            3
                        "North Macedonia"    98
                        "Mauritania"          3
                        "Kuwait"              1
                        "Sweden"              1
                        "Armenia"             3
                        "Kazakhstan"          2
                        "Myanmar"             2
                        "Lithuania"           1
                        "Senegal"            98
                        "Madagascar"          1
                        "Japan"               2
                        "Montenegro"          1
                        "Indonesia"           2
                        "Hong Kong"           1
                        "Hong Kong"           1
                        "Dominican Republic"  1
                        "Kenya"               1
                        "United States"      98
                        end
                        label values L5 labels17
                        label def labels17 1 "Very serious threat", modify
                        label def labels17 2 "Somewhat serious threat", modify
                        label def labels17 3 "Not a threat at all", modify
                        label def labels17 98 "(DK)", modify

                        Click image for larger version

Name:	Screenshot 2024-07-30 at 12.47.31.png
Views:	1
Size:	1.61 MB
ID:	1760194

                        Comment


                        • #13
                          Sonnen Blume I have some difficulty with grasping what you are trying to do.

                          How many countries are there in the full dataset? Can you be confident of avoiding a complete mess?

                          Your code yields vertical bars, but your example graph is horizontal.

                          No values of 98 or Don't know show up in your example graph.

                          Comment

                          Working...
                          X