Announcement

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

  • Two-way bar chart: how to plot in ascending (or descending) order of the y-values

    Greetings,

    I'm running Stata 15.1 on a Mac. I'm working with data from the 2019 American Community Survey. I'd like to create a bar graph showing median personal incomes (and their 95% CIs)by ethnicity/nationality. However, I'd like the plots to be ordered from the highest to the lowest values of income. The following command gives me a bar chart whose plots are ordered according to the (arbitrary) values of the grouping variable (ANCEST):

    Code:
    graph twoway (bar inctot_median ANCEST if low > 0) (rcap high low ANCEST if low > 0), xlabel(0(1)157, angle(90) valuelabel labsize(vsmall)) ylabel(, angle(h)) xsize(9)
    However, as mentioned, I'd like the groups on the x-axis--and thus their respective bars--to be ordered in terms of income (highest to lowest). I'm not sure how to go about this, though. I know the 'graph bar' command has 'descending/ascending' options, but it does not allow the addition of CIs. Any input you can provide would be much appreciated.

    Here is my data:

    Code:
    * Example generated by -dataex-. To install: ssc install    dataex
    clear
    input float ANCEST double inctot_median float(high low)
    1 42000  45176.39  38823.613
    2 36000  39485.54   32514.46
    3 35000 71372.086 -1372.0865
    4 35000 35351.066  34648.934
    5 36000  37744.58   34255.42
    6 32500  33502.95   31497.05
    7 34000  35907.26   32092.74
    8 30400   31069.4    29730.6
    9 34800 35058.105  34541.895
    10 35000 36920.953  33079.047
    11 40000  50560.23  29439.764
    12 34000  34330.82   33669.18
    13 36100 36549.117  35650.883
    14 39400  45533.84   33266.16
    15 38700  50280.49   27119.51
    16 37350  38148.09   36551.91
    17 34800 36345.965  33254.035
    18 36400  37028.23   35771.77
    19 35000 36005.668  33994.332
    20 37000 39058.188  34941.813
    21 38000  39610.55   36389.45
    22 35600 37451.133  33748.867
    23 34000 38187.703  29812.297
    24 45000  55276.86  34723.145
    25 35000  40146.48  29853.516
    26 15000  27173.93    2826.07
    27 40000  43228.95  36771.055
    28 37740  39134.31  36345.688
    29 43000  54063.64  31936.354
    30 20000 37365.293  2634.7056
    31 20800  27411.63   14188.37
    32 35000 36695.746  33304.254
    33 42000  48981.22  35018.777
    34 40000  43011.62   36988.38
    35 34800  48424.41   21175.59
    36 35800 36414.055  35185.945
    37 33000  36212.32   29787.68
    38 36000  49365.23   22634.77
    39 38100  39886.01  36313.992
    40 31000 34502.633  27497.365
    41 37000  39209.25  34790.746
    42 42310  47058.13   37561.87
    43 30000  43965.82  16034.176
    44 35000 37127.844  32872.156
    45 35000  40850.49  29149.506
    46 37000  42244.17   31755.83
    47 50000 67033.875  32966.125
    48 25000  25981.47   24018.53
    49 30500  40251.29  20748.717
    50 50000  84633.53  15366.472
    51 33600   36391.9    30808.1
    52 35000  36054.47   33945.53
    53 30200  34319.83   26080.17
    54 34900   35987.9    33812.1
    55 38000  39690.63  36309.367
    56 28000  29676.31   26323.69
    57 48830  50688.59   46971.41
    58 22000  25629.52  18370.482
    59 35000  39068.88   30931.12
    60 37800  46297.66  29302.346
    61 37000  40972.31   33027.69
    62 35000 38706.176  31293.824
    63 34060  40014.51  28105.494
    64 28000  32634.03   23365.97
    65 13200 19976.377   6423.622
    66 40000     50322  29677.994
    67 30000     46833  13167.005
    68 20000   20230.3    19769.7
    69 24000 28465.225  19534.775
    70 20000  21213.91   18786.09
    71 17900 20478.014  15321.986
    72 25000  27284.32   22715.68
    73 25500 28430.734  22569.266
    74 22900 23905.844  21894.156
    75 28800 34716.273  22883.727
    76 24000   28317.9    19682.1
    77 30000 34603.395  25396.605
    78 30000  31651.65   28348.35
    79 26000  28095.06   23904.94
    80 21900 34599.504   9200.498
    81 25220 27138.807  23301.193
    82 31900 38693.617   25106.38
    83 28500  32044.33   24955.67
    84 20000  20614.64  19385.357
    85 22810 24061.967  21558.033
    86 20000  20979.72  19020.277
    87 25800   31228.6    20371.4
    88 30000 36345.016  23654.984
    89 26000 33497.918  18502.084
    90 30000 31375.057  28624.943
    91 21800  28621.76  14978.243
    92 31000  34766.48   27233.52
    93 24000 33909.043  14090.955
    94 38400  46555.81   30244.19
    95 42000  58275.68  25724.316
    96 30000 32937.168   27062.83
    97 24000  25345.57   22654.43
    98 28400 32007.945  24792.055
    99 26200 29103.326  23296.674
    100 25000  33162.06  16837.941
    101 22000  28018.84  15981.162
    102 40000  48299.37  31700.635
    103 15700  22112.37   9287.631
    104 33800  51205.75   16394.25
    105  9600  58097.97  -38897.97
    106 30000  36603.38   23396.62
    107 30500 33363.004  27636.996
    108  9800 15956.345   3643.655
    109 12100  31807.29  -7607.289
    110 20000  25761.11  14238.888
    111 40000  49045.85   30954.15
    112 25000   30219.4    19780.6
    113 28000  35501.05   20498.95
    114 26000 29286.227  22713.773
    115 20000  25976.26   14023.74
    116 30000  40994.92  19005.078
    117 30000 34343.832  25656.166
    118 33300  46671.82  19928.186
    119 34000  40481.04  27518.955
    120 25000  29158.65   20841.35
    121 40000  52427.95  27572.057
    122 25600 36621.086  14578.915
    123 12000 14378.752   9621.248
    124 42010  53777.77   30242.23
    125 17500  24562.17   10437.83
    126 40000  49908.97   30091.03
    127 31200  41316.52   21083.48
    128 38050  56410.04  19689.965
    129 23000 23902.115  22097.885
    130 20000 23129.336  16870.664
    131  9300 17275.918  1324.0823
    132 28000 33806.492   22193.51
    133 25800 35395.035  16204.965
    134 21800  27662.34   15937.66
    135 25000  26881.46   23118.54
    136 30000 38682.355   21317.64
    137 16000  28399.42    3600.58
    138 25000  31488.36   18511.64
    139 32500  33276.39   31723.61
    140 24000 31028.217  16971.783
    141 25000 27332.807  22667.193
    142 25000 27013.104  22986.896
    143 25000  28383.69   21616.31
    144 22900  24023.73   21776.27
    145 26400  28437.89   24362.11
    146 15500  24917.81   6082.191
    147 24000  27611.73   20388.27
    148 26400 31263.984  21536.016
    149 24000  35495.15   12504.85
    150 26900  30871.92   22928.08
    151 29000  33708.96   24291.04
    152 25000  36903.74  13096.264
    153  5000 14634.072 -4634.0728
    154 35000  42476.57  27523.436
    155 20000 20187.494  19812.506
    156 19000 19399.252  18600.748
    157 13100 15169.773  11030.227
    . 25000  25124.36   24875.64
    end
    label values ANCEST ANCEST
    label def ANCEST 1 "Austrian", modify
    label def ANCEST 2 "Belgian", modify
    label def ANCEST 3 "Flemish", modify
    label def ANCEST 4 "British/English", modify
    label def ANCEST 5 "Danish", modify
    label def ANCEST 6 "Dutch", modify
    label def ANCEST 7 "Finnish", modify
    label def ANCEST 8 "French", modify
    label def ANCEST 9 "German", modify
    label def ANCEST 10 "Greek", modify
    label def ANCEST 11 "Icelander", modify
    label def ANCEST 12 "Irish", modify
    label def ANCEST 13 "Italian", modify
    label def ANCEST 14 "Luxemburger", modify
    label def ANCEST 15 "Maltese", modify
    label def ANCEST 16 "Norwegian", modify
    label def ANCEST 17 "Portuguese", modify
    label def ANCEST 18 "Scottish", modify
    label def ANCEST 19 "Swedish", modify
    label def ANCEST 20 "Swiss", modify
    label def ANCEST 21 "Welsh", modify
    label def ANCEST 22 "Scandinavian/Nordic", modify
    label def ANCEST 23 "Albanian", modify
    label def ANCEST 24 "Belarusian", modify
    label def ANCEST 25 "Bulgarian", modify
    label def ANCEST 26 "Cossack", modify
    label def ANCEST 27 "Croatian", modify
    label def ANCEST 28 "Czech", modify
    label def ANCEST 29 "Estonian", modify
    label def ANCEST 30 "Georgian", modify
    label def ANCEST 31 "Roma", modify
    label def ANCEST 32 "Hungarian", modify
    label def ANCEST 33 "Latvian", modify
    label def ANCEST 34 "Lithuanian", modify
    label def ANCEST 35 "Macedonian", modify
    label def ANCEST 36 "Polish", modify
    label def ANCEST 37 "Romanian", modify
    label def ANCEST 38 "Moldavian", modify
    label def ANCEST 39 "Russian", modify
    label def ANCEST 40 "Serbian", modify
    label def ANCEST 41 "Slovak", modify
    label def ANCEST 42 "Slovene", modify
    label def ANCEST 43 "Uzbek", modify
    label def ANCEST 44 "Ukranian", modify
    label def ANCEST 45 "Yugoslavian", modify
    label def ANCEST 46 "Slav", modify
    label def ANCEST 47 "Slavonian", modify
    label def ANCEST 48 "Spanish", modify
    label def ANCEST 49 "Australian", modify
    label def ANCEST 50 "New Zealander", modify
    label def ANCEST 51 "Canadian", modify
    label def ANCEST 52 "French Canadian", modify
    label def ANCEST 53 "Acadian", modify
    label def ANCEST 54 "Chinese", modify
    label def ANCEST 55 "Japanese", modify
    label def ANCEST 56 "Korean", modify
    label def ANCEST 57 "Indian", modify
    label def ANCEST 58 "Pakistani", modify
    label def ANCEST 59 "Nigerian", modify
    label def ANCEST 60 "Basque", modify
    label def ANCEST 61 "Iranian", modify
    label def ANCEST 62 "Lebanese", modify
    label def ANCEST 63 "Turkish", modify
    label def ANCEST 64 "Egyptian", modify
    label def ANCEST 65 "Iraqi", modify
    label def ANCEST 66 "Sri Lankan", modify
    label def ANCEST 67 "Malaysian", modify
    label def ANCEST 68 "Mexican", modify
    label def ANCEST 69 "Costa Rican", modify
    label def ANCEST 70 "Guatemalan", modify
    label def ANCEST 71 "Honduran", modify
    label def ANCEST 72 "Nicaraguan", modify
    label def ANCEST 73 "Panamanian", modify
    label def ANCEST 74 "Salvadoran", modify
    label def ANCEST 75 "Agentinean", modify
    label def ANCEST 76 "Bolivian", modify
    label def ANCEST 77 "Chilean", modify
    label def ANCEST 78 "Colombian", modify
    label def ANCEST 79 "Ecuadorian", modify
    label def ANCEST 80 "Paraguayan", modify
    label def ANCEST 81 "Peruvian", modify
    label def ANCEST 82 "Uruguayan", modify
    label def ANCEST 83 "Venezuelan", modify
    label def ANCEST 84 "Puerto Rican", modify
    label def ANCEST 85 "Cuban", modify
    label def ANCEST 86 "Dominican", modify
    label def ANCEST 87 "Bahamian", modify
    label def ANCEST 88 "Barbadian", modify
    label def ANCEST 89 "Belizean", modify
    label def ANCEST 90 "Jamaican", modify
    label def ANCEST 91 "Dutch West Indian", modify
    label def ANCEST 92 "Trinidadian/Tobagonian", modify
    label def ANCEST 93 "British Virgin Islander", modify
    label def ANCEST 94 "British West Indian", modify
    label def ANCEST 95 "Grenadian", modify
    label def ANCEST 96 "West Indian", modify
    label def ANCEST 97 "Haitian", modify
    label def ANCEST 98 "Brazilian", modify
    label def ANCEST 99 "Guyanese", modify
    label def ANCEST 100 "Algerian", modify
    label def ANCEST 101 "Moroccan", modify
    label def ANCEST 102 "Israeli", modify
    label def ANCEST 103 "Jordanian", modify
    label def ANCEST 104 "Kuwaiti", modify
    label def ANCEST 105 "Saudi", modify
    label def ANCEST 106 "Syrian", modify
    label def ANCEST 107 "Armenian", modify
    label def ANCEST 108 "Yemeni", modify
    label def ANCEST 109 "Kurdish", modify
    label def ANCEST 110 "Palestinian", modify
    label def ANCEST 111 "Cameroonian", modify
    label def ANCEST 112 "Cape Verdean", modify
    label def ANCEST 113 "Congolese", modify
    label def ANCEST 114 "Ethiopian", modify
    label def ANCEST 115 "Eritrean", modify
    label def ANCEST 116 "Gambian", modify
    label def ANCEST 117 "Ghanian", modify
    label def ANCEST 118 "Guinean", modify
    label def ANCEST 119 "Kenyan", modify
    label def ANCEST 120 "Liberian", modify
    label def ANCEST 121 "Senegalese", modify
    label def ANCEST 122 "Sierra Leonean", modify
    label def ANCEST 123 "Somali", modify
    label def ANCEST 124 "South African", modify
    label def ANCEST 125 "Sudanese", modify
    label def ANCEST 126 "Togo", modify
    label def ANCEST 127 "Ugandan", modify
    label def ANCEST 128 "Zimbabwean", modify
    label def ANCEST 129 "Afghan", modify
    label def ANCEST 130 "Bengali", modify
    label def ANCEST 131 "Bhutanese", modify
    label def ANCEST 132 "Nepali", modify
    label def ANCEST 133 "Punjabi", modify
    label def ANCEST 134 "Burmese", modify
    label def ANCEST 135 "Cambodian", modify
    label def ANCEST 136 "Cantonese", modify
    label def ANCEST 137 "Mongolian", modify
    label def ANCEST 138 "Tibetan", modify
    label def ANCEST 139 "Filipino", modify
    label def ANCEST 140 "Indonesian", modify
    label def ANCEST 141 "Laotian", modify
    label def ANCEST 142 "Hmong", modify
    label def ANCEST 143 "Thai", modify
    label def ANCEST 144 "Vietnamese", modify
    label def ANCEST 145 "Hawaiian", modify
    label def ANCEST 146 "Polynesian", modify
    label def ANCEST 147 "Samoan", modify
    label def ANCEST 148 "Tongan", modify
    label def ANCEST 149 "Micronesian", modify
    label def ANCEST 150 "Guamanian", modify
    label def ANCEST 151 "Chamorro", modify
    label def ANCEST 152 "Palauan", modify
    label def ANCEST 153 "Marshall Islander", modify
    label def ANCEST 154 "Fijian", modify
    label def ANCEST 155 "African American", modify
    label def ANCEST 156 "Native American", modify
    label def ANCEST 157 "Eskimo", modify
    Thanks in advance!


  • #2
    The user written (ok, I am getting old. It should now be "community contributed") command labmask was written for that purpose. It is discussed with many examples in this article by Nick Cox: https://www.stata-journal.com/articl...article=gr0034 . To get labmask it is probably easiest to type in Stata search labmask
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      See also now https://www.statalist.org/forums/for...e-or-graph-use

      Code:
      myaxis toshow=ANCEST, sort(mean inctot_median)
      
      twoway (bar inctot_median toshow if low > 0) (rcap high low toshow if low > 0), xlabel(0(1)157, angle(90) valuelabel labsize(vsmall)) ylabel(, angle(h)) xsize(9)

      Comment

      Working...
      X