I want to create a stacked bar graph, where the bars equal 100%, representing the percentage of income owned by White, Black, Hispanic, and other race. I would like to do by year, but for now, I just want one bar, that adds up to 100%, that show how income by race/ethnicity. So if the total amount of income across the data set is $1,000,000, and the sum of white income is $800,000, and black is $100,000 then white = 80%, and black=10%.
Here is my data:
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input long ID byte racecat double income float weight 21 1 38688.48025959978 18952.383 81 1 17430178.47485127 177.03397 141 1 2321308.8155759876 1503.8282 251 4 154753.92103839913 17517.736 261 2 28507.30124391563 39906.88 271 1 96721.20064899945 32063.27 291 1 3044172.525689562 128.49106 311 1 71268.25310978908 23650.52 331 4 66167.48242293131 22853.25 381 2 1018117.9015684153 2250.01 391 2 60068.956192536505 24881.57 411 2 865400.2163331531 682.5664 441 3 73304.48891292591 20644.69 451 3 49887.77717685235 21644.885 501 1 43779.06976744186 21836 531 1 36652.244456462955 25246.375 611 4 42760.951865873445 21519.236 681 1 21380.475932936723 31648.785 691 4 1303190.9140075718 2053.5024 701 1 87558.13953488372 8331.604 751 4 108938.61546782045 26278.193 771 1 230094.64575446188 36962.74 791 1 48869.65927528394 45239.25 811 1 122174.14818820985 38244.098 831 1 50905.89507842077 24710.346 871 1 152717.6852352623 27883.16 911 1 53960.248783126015 34740.195 981 2 127264.73769605192 43310.56 1041 1 70250.13520822066 22185.564 1061 1 9926649.540292049 178.977 1071 1 6689034.613304489 178.6889 1151 2 125228.50189291508 31473.98 1171 4 14253.650621957815 26796.365 1191 1 5090.589507842077 53667.87 1211 1 868454.5700378583 8465.74 1221 2 5090.589507842077 24057.293 1231 2 41742.833964305035 22071.355 1291 1 126246.61979448351 20903.654 1301 3 50905.89507842077 33724.066 1331 1 48869.65927528394 18740.283 1431 1 71268.25310978908 14054.192 1441 1 24434.82963764197 28034.426 1451 1 68213.89940508382 17382.271 1471 2 18326.122228231477 19812.84 1511 4 320707.13899405085 11661.69 1521 1 4153921.0383991348 1739.4486 1551 3 124210.38399134668 8588.906 1581 3 52942.1308815576 48048.66 1591 4 26471.0654407788 33591.95 1621 4 110974.85127095727 21138.53 1631 1 193442.4012979989 31527.064 1711 4 38688.48025959978 22357.574 1721 1 1201379.1238507302 5441.831 1741 1 2138047.5932936724 1868.1577 1771 1 50905.89507842077 14224.52 1791 1 308489.72417522984 9173.526 1801 1 99775.5543537047 21468.1 1821 3 47851.541373715525 19590.03 1841 1 1018117.9015684153 674.5006 1851 1 226022.1741481882 19260.576 1881 1 90612.49323958896 43276.91 1901 1 33597.89075175771 26863.91 1931 3 50905.89507842077 21168.594 1971 3 24434.82963764197 24312.75 2001 2 10181.179015684154 18775.9 2021 1 92648.7290427258 45480.25 2051 1 66177.663601947 16889.625 2071 2 19344.24012979989 35158.99 2081 1 18326.122228231477 23638.85 2131 1 306453.488372093 2766.254 2181 1 1221.7414818820985 22836.17 2191 3 50905.89507842077 5418.684 2231 2 25452.947539210385 20720.39 2311 4 384848.56679286103 15557.152 2321 1 32579.772850189293 30457.99 2371 4 237221.47106544077 46866.91 2401 4 11199.296917252568 17013.354 2441 4 35634.12655489454 22350.28 2451 1 424555.16495402914 30045.66 2461 2 119119.7944835046 42368.89 2471 1 2209315.8464034614 718.1016 2501 1 258601.9469983775 3424.138 2511 1 152717.6852352623 27973.91 2541 1 86540.02163331531 24246.193 2631 1 203623.58031368308 24694.057 2641 1 197514.87290427258 21947.225 2671 4 10181.179015684154 23449.984 2751 1 66177.663601947 16916.422 2781 1 223985.9383450514 46866.91 2811 1 130319.09140075717 25880.9 2841 1 1496633.3153055706 2684.151 2871 4 103848.02595997836 46866.91 2881 1 38688.48025959978 17751.37 2991 1 80431.31422390482 29768.707 3011 4 346160.08653326123 747.1478 3021 1 25452.947539210385 27705.873 3031 4 83485.66792861007 20825.45 3041 3 36652.244456462955 33754.855 3051 4 398084.0995132504 12039.806 3101 4 61087.07409410492 44688.65 end label values racecat rcat label def rcat 1 "1 White", modify label def rcat 2 "2 Black", modify label def rcat 3 "3 Hispanic", modify label def rcat 4 "4 Other race/ethnic", modify
my first attempt was to collapse by racecat, create percentages representing the total, and then graph those percentages
collapse (sum) income [w=weight], by(racecat)
egen totinc = total(income)
gen pctinc = (income/totinc)*100
graph hbar pctinc, over(racecat) stack
But that does not work. I have tried multiple combinations - this seems so simple, and yet I cannot produce a simple bar chart.
I am aware of pshare, catplot, and tabplot, but have not been able to manipulate those commands to produce my simple bar char.
Please note that my data are weighted.
thank you for your help.
0 Response to 100% Stacked Bar Chart
Post a Comment