Announcement

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

  • simple stacked bar with two different continous variables on x axis

    Hello and happy Good Friday

    I'm trying to reproduce in STATA a following stacked bar chart (done in Excel). I don't have any code to share because I can't find anything that would be even close to it. I have in total 10 variables: expenditures for food, transport, home, miscellaneous and other and the same for energy footprint (EF). I tried to use combine command but it didn't work with continuous variables like expenditure. Any help would be appreciated

    Click image for larger version

Name:	stacked_bar.JPG
Views:	1
Size:	29.5 KB
ID:	1494113






  • #2
    That's certainly doable in Stata using graph bar and I don't know why you found the code elusive. But it's not very readable: several small categories, a need for back and forth with the legend.

    Here's another suggestion. I had to fake your data because you don't give a data example: tabplot is from the Stata Journal.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(percent which what)
    61 1 1
     8 1 2
     5 1 3
    16 1 4
    10 1 5
     7 2 1
    82 2 2
     6 2 3
     2 2 4
     3 2 5
    end
    label values which which
    label def which 1 "exp", modify
    label def which 2 "EF", modify
    label values what what
    label def what 1 "Food and alcohol", modify
    label def what 2 `" "Housing, water, electricity," "gas, other fuels" "', modify
    label def what 3 "Transport", modify
    label def what 4 `" "Miscellaneous goods" "and services" "', modify
    label def what 5 "Other", modify
    
    set scheme s1color
    
    tabplot what which [iw=percent], horizontal blcolor(eltgreen*2) bfcolor(eltgreen*0.2) showval  ytitle("") xtitle("")
    Click image for larger version

Name:	expenditure.png
Views:	1
Size:	23.5 KB
ID:	1494135


    Comment


    • #3
      Hello Nick,

      Thank you for your answer. Unfortunately, the command tabplot did not work. I got a message saying: 'command tabplot is unrecognized' when I tried to run your code. I'm using stata14 so maybe that is the reason? I wonder if I should update my stata and that might be a reason (I need someone from IT with an administrator password to be able to run the update, so it needs to wait after Easter.
      Can it be another reason for this command not to run?

      Another question: Is it the only way to create this figure - by using input flow? With your method, it requires calculating separately percentages for each category (most likely in excel) to then create new statafile just for creating this figure. Or can I use existing variables to create frequencies (I have over 12 thousand observations) and make bar graph without calculating frequencies by hand?

      I appreciate your help,

      Cheers,

      Marta

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input double(Zam_weights expNOweightCOICOP1 expNOweightCOICOP4 expNOweightCOICOP7 expNOweightCOICOP12 EF_unweight_COICOP1 EF_unweight_COICOP4 EF_unweight_COICOP7 EF_unweight_COICOP12) float(exp_other EF_other)
      1143.83652842272  1843.1585589643 237.740879505956 138.523491257931 55.7963449168595 5.72070771274769 7.34092638009282  1.66543717797937  .518741834582012 29.163885   .13830046
       276.50371902091 130.139748539822 .930820387182132                0                0 .651493133326334 44.4211785757963                 0                 0         0           0
      125.508247568308 3648.59762831689 1501.89356465239 138.523491257931 359.113087426756 11.3239571465603 363.644950807247  2.01076270907684  3.14721795417739  596.3594    3.809426
      234.789736764894  7452.3041461556 99.2844569854745                0  361.41254680399 29.1434421672999 40.9998347857307                 0  3.55396817795311  168.5287   1.5976194
       169.97693344836 483.567848014728  38.124590490028                0                0  1.4743310852601 15.9930827987016                 0                 0 10.412775   .04410137
      374.505727820788 1478.09480809427 171.119768112265                0 56.6618656355992 5.17658300217446 71.9678988636316                 0   .45090788032369 108.50838    .6029801
      37.2536099283971 1260.01477344944 6.51574271027492 55.4093965031724 107.686355046432  4.1410781316054 82.2539518482804  .804305083630736  1.00444003200912  365.1984   4.0378137
      150.661038698079 959.257061198044 11.0456132220032  9.2348994171954 43.4614020596818 3.44793323003663 1.65561727194653  .134050847271789  .430032066943492 20.000914     .180816
      151.935168932051 1355.83334223035 477.144592149029 104.244561010912 620.255138014926 4.45799079499887 63.6248077566487  .174979304166414  6.09292641601499  481.4298    3.707222
      563.274582117364 1820.67553797232 108.592660857296                0  456.12799094034 6.36319788688564 41.0203925232534                 0  4.06142376193863  481.5327     5.60964
      179.777336205452 1426.14773280665 221.819359693608 120.812949520866 172.003525668005 4.57991185199905 7.30706859949065  .531635685022299  1.68963505654197  212.8697   1.5489886
      22.1065638919032 1481.50144527556 151.194253075322                0 159.401176656896 5.17421735668701 97.6022514009105                 0  1.55553187204875 169.53633   1.4732814
      44.0910543367156 905.939116613126 3.72328154872853                0  45.207534178242 2.69956438940631 31.1784507367065                 0  .448217936921547 2.1899157  .011302718
      303.549339855495 513.800897216293  2.7924611615464  4.6174497085977 34.8216252200074 2.02239831804742 23.9896241980524 .0670254236358947  .342564131014454 74.330826    .9290763
      205.936589001103 404.712181775809 44.3660541417288                0 46.9100524549104 1.49975329061977 40.8785441343464                 0  .460809560875083  9.266183   .08534345
      154.961662527192 1921.09212292118 496.382157013584                0 236.287671624734 6.53487199899458 55.5111832970982                 0  2.32111482514857 181.74956   1.6783962
      29.4668412015206 1836.25646588336 84.6991634684589                0 275.245490211016 6.68984403252306 73.5920694653353                 0  2.69793552443539 174.76335    1.787802
      1143.83652842272 2839.29884579984 184.356217254599 69.2617456289655 138.992748014549 9.08275129856108 81.9587607836493  .832718588989685   1.3653616618521 304.07593   2.6749895
      32.7619798229813 10636.1887636042 906.114290382758  260.61140252728 319.820272546952 36.4332930043174 29.2702557352672  14.4484082213428  2.91252679613971   1225.55      3.4348
      265.266134718456 1266.93847091319 73.4567198185096 17.3740935018187 26.1937263320794 4.64899507298676 31.9799982761463 .0291632173610689  .273011819511803 150.91373    .8772343
      152.823297962746 3395.62518213152 111.581310308538 184.697988343908 118.343081523407 10.7174228340918 47.9730810748479  2.68101694543579  1.13315458241546  251.5459   1.9648038
      72.1265426684648 689.297499479841 22.7105529765467                0 117.359833638549 2.21559398811631 19.6248259605897                 0  1.14964859146762  74.05846    .8343076
      155.434939201476 1384.83484839861 425.022311643573 23.0872485429885  130.30570126364  5.0695374957261 14.5730217239358  .335127118179473  1.28002655798634  43.43523     .426541
      67.0551451370883 1650.34754375377  27.924611615464 142.279864012266 104.244561010912 6.06914087593378 43.5664967921741   .95012117043608  1.02402124638907  92.87968    .7508347
      478.189069766949 1100.90257774475 4.65410193591066                0                0 3.37255399976661 39.3094913622223                 0                 0 271.85785   2.4125235
      499.849288823476 393.262468893835 5.65269462884822                0 43.4352337545466 1.30528830198525 2.01110572819537                 0  .426675519328781  60.60796    .5736575
       66.325507158986 827.739544110978 149.705900798566                0 12.1618654512731 2.64166709880965 63.9661638735495                 0  .119469145412059  13.57365   .14337054
      418.224493363922 494.550302632217 .930820387182132 173.740935018187 29.5359589530917 1.85492301902965 27.7162672795034  .291632173610689  .290139353143571  84.54315    .9101365
      166.669956134513 640.833493540172 9.30820387182132 23.0872485429885 45.1726431047285 2.40068779372502 31.9985002399168  .335127118179473  .443742540101932  36.74206    .3769004
       331.62753579493  995.45200551239 147.844260024201                0  78.237350298177 3.27965277871242 63.9620523260449                 0  .768103323413911 102.33336    .7352937
      71.6326194450458  918.96154580507 86.4882052390595 13.8523491257931 182.427981769096 3.42834863681912 32.0087791086782  .201076270907684  1.79203718118088  500.5865     5.21005
      179.598269303928 810.973093291681 46.5410193591066                0 20.8489122021824 2.78889691282644 42.2576154575673                 0  .204804249277815 25.167824   .29820445
      756.895873998996 1426.83476450504  13.962305807732 13.8523491257931 26.9298449278189 4.78265119146919 134.147846413594  .166543717797937  .264538821983844  263.6507    1.961813
      429.152928877365 777.360461832259 59.0485265094063                0 114.433670278384 2.66525474486719 23.9937357455569                 0  1.11485700270434   50.2901    .5046768
      151.935168932051 1019.02730356916 365.421958318184                0 69.5225423124098 3.40063134517432 159.895879833227                 0   .68603737854076 100.47128   1.0563834
        523.1676168906 650.023680123494 56.7763824413666                0 8.68704675090933 2.12707194974743 49.0620649014743                 0 .0853351038657562         0           0
      280.127179238683 3031.60200919665 812.811807799861                0 602.881044513107 9.92455880493971 29.0638124977805                 0  5.92225620828348  2598.901   15.269678
      375.262228846454 1083.54297699813 9.30820387182132  26.061140252728 29.9596886643401 3.48754062562544 8.01504336312128 .0437448260416034  .263115835354386  99.14847    .8777351
      184.421424666843 1081.86389097386 62.0516414981893                0 142.467566714913 3.70899028850842 40.9176038356396                 0   1.3994957033984  63.06353    .5339559
      108.188183895223 381.038020407891  2.7924611615464  9.2348994171954                0 1.53760974994635 17.9937599788535  .111029145198625                 0  25.64388   .20737503
      94.4296855877104 1395.11902259852 140.135846199607                0  149.41720411564 4.45840457668124 32.3977015084538                 0  1.46776378649101  26.05496   .22234194
      28.7901740690675  1448.3080177883 74.8529503992829                0 89.9080178543104 5.14193129858001 31.9830819367748                 0  .879650678794127  171.6956   1.5171344
      11.5338292856969 1008.80833993096 131.305044422214                0 29.5301269178895  3.1977882696541 36.1768871325504                 0  .283004060710673 170.37024    .8988148
        16.68972373296  7068.9659613833 310.072133510608 143.140940966529  288.40995213019 24.1435851222396 161.790342796026  2.07778813271273  2.83312544834311 232.70114   2.3734498
      500.924835378187 718.461520423151 9.30820387182131                0 111.862287535664 2.72148623530117 35.3295305875925                 0  1.09602323103871 119.59167   1.3615987
      11.5338292856969 3475.23010701983 595.550803853102 31.3986580184644 429.889291019621 9.18082320143276 179.402733809218  .423542497821653  4.20964227924493 1741.3447    9.175096
      262.007693033222 896.475754454312 259.985529385718                0 43.4352337545466 2.94753016055267 110.693947503523                 0  .426675519328781  46.33092     .454977
      443.059389991101 753.631883124499 64.9431120987692                0 41.6978244043648 2.47997232561968 27.8006061144409                 0   .40960849855563  43.09894    .1945576
      396.378409638145 2325.49048097848 126.893913569553 87.9661728416654 88.6134733997286 7.71472306718668 101.947446361851  .384754563987854  .703684136249267 1990.0112    9.675438
      453.986371416381 580.997018188505 50.2252555233957                0 19.1546464839949 1.79903342417998 16.0198078574812                 0  .187807139402347  29.57257     .327837
      386.807061761794 1089.20617844484 6.51574271027492                0 47.6511599746581 3.37626337179936 35.3233632663356                 0  .456456835953075 118.52074   1.3834295
      94.4296855877104  1601.5231834049 144.340236069425  46.174497085977 324.967034820192 5.77994165451506 95.4587559950225  .555145725993124  3.13580849998682   520.118   4.1578574
      110.987826433801 628.523757725952 1.86164077436426                0 29.5359589530917 2.10222639780265 15.9930827987016                 0  .290139353143571   56.8825   .53940225
      89.6304244460807 1121.02964321751 61.5611922137364                0   70.40822231436 3.65451480840769 40.9155687306107                 0  .691284252210309  52.99759    .4647825
      383.476132242685 566.570321624871 312.889174519053                0 154.629432166186 1.94129426157362 135.916534503936                 0  1.51896484881046  16.72642   .11813147
      19.0857413179985 3105.81316060496 532.443053381192                0 123.356063862912 10.6459408233693 247.713389027948                 0  1.21175847489374 191.25804   2.2653148
      183.052082175157 964.846847290963 19.5277054386051 2.30872485429885  12.205479293165  3.3660608482376 7.99756928622693 .0335127118179473  .125063391436577         0           0
      1143.83652842272 2114.08421797936 953.177577892694 277.046982515862 135.937701563726 6.66560440761641 135.402320342865  3.33087435595874  1.31765614441684 177.22276    1.646009
      46.8990181703696 3244.92651400393 495.156033802452 138.523491257931  781.83420758184 11.1030631027782 55.5060955345259  1.66543717797937  7.68015934791806  414.3987    4.330175
      7.75740198904459 3039.72731731427 928.913965289009                0 385.704875740374 9.79563752727059 29.3107120352872                 0  3.78887861163957  1408.569   10.258325
      125.246161488266 1128.20769547981 128.316554585596 43.4352337545466 26.1747195376533 3.90935626655045  55.964483039818 .0729080434026723    .2440940552457  77.80247    .7092485
      126.916110308109 426.928428838571 2.32705096795533                0                0 1.32263628054468 42.1599662043342                 0                 0  .7348129 .0013462423
      335.086933606715 265.525753616805 53.1147032621043                0 6.94963740072746 .902736547032475 14.4353011486274                 0  .068268083092605  8.687047    .0793614
      151.308079716276 2738.74593866926 658.792313574837 380.149390299663 456.938659097831 10.1707127323686 161.895997723417  2.72870854802397  4.48862646333877  1261.281    6.620483
      280.127179238683  1541.4560328359 297.771683611019                0 60.8093272563653 5.42929964717273 82.5105665354603                 0  .597345727060293 31.852505    .3127967
      182.153574925631 515.432345256152 18.6164077436426                0  64.284145956729 2.19785082069131 67.6008666329397                 0  .631479768606596         0           0
      569.439387121557 1814.22765000326  27.924611615464 23.0872485429885 62.5467366065471 6.07542210855589 14.5278852208467  .277572862996562  .614412747833445 225.56375    1.820225
      1143.83652842272 1368.54052293527  204.63907523127 106.201343297747 194.589847220369 4.21393580317347 94.5272768425726  1.36892197807684  1.91150632659294  162.0901   1.6749824
      514.781305564025 302.761984726148 4.65410193591066                0  16.547258114944  .97752763283633 12.6682149755914                 0  .167507173528474  7.792017   .09934342
      186.268913093145 1795.96254492493 89.1975184770486                0 201.539484621096 5.80256832874215 81.7581161555689                 0  1.97977440968554  174.0499   1.9421487
      11.5338292856969 1160.34208196235 57.3975395622786                0 145.942385415277 3.89504356120574 40.9073249668783                 0   1.4336297449447  155.8283   1.2060866
      262.007693033222 1740.15252822559 1208.16008144365 868.704675090933 243.237309025461 4.81915606447086   29.92744416097  48.1613607378093  2.38938290824117  2652.335   17.428644
      44.0910543367156 1102.02096467543  2.7924611615464                0  82.146203399316 3.80348594079131 54.5540776859556                 0   .79180692048921  8.269787   .04431338
      549.410580447809 790.646592849126 116.235412244448 9.23489941719539 114.691696309787 2.81636290597199 47.9833599436093  .134050847271789  1.12933237896073  175.8123   1.0702143
      441.643866516208 827.910914569177 112.470051018473  52.122280505456 103.912222630008 2.61912995888974 74.6392794690089 .0874896520832068  .950963949323883  57.79681    .5655155
      29.4668412015206 1375.02644938548 472.490490213118                0 267.561039928007 4.89570063911344 63.6145288878873                 0  2.62832119906529 17.179987    .1432747
      482.370168731521 307.551019807192 30.7152421886386                0 36.5117646589543 1.29447538471437 24.5361718851179                 0  .361763983850887  8.687047    .0793614
      414.566335588153  764.87342348798 7.44656309745706                0 17.3845608238727  2.3297613732172 67.4990025947116                 0  .172012826777397 2.2044387  .004038727
      114.489673180347 2973.82945216395 822.120011671683                0 330.107776534554 9.30532167489167 29.0843702353033                 0  3.24273394689873  950.3501     6.40302
      43.4927500058792 2187.85226130588 220.281954377293                0 60.8093272563653 6.63682433703742  163.60874212999                 0  .597345727060293  52.90653   .09692945
      172.653601059149 9479.81647662422 1065.89892620687                0 677.631515859144   35.01551692347 182.882004687005                 0  6.66150857771252  905.5413    5.764529
      742.328156720422 908.632122321852  106.20935338217                0 36.7298338684139 3.06260565854893 64.8784523064027                 0  .389735213973477   74.5023     .535207
      482.370168731521 122.056502489034 1.86164077436426                0                0 .371256436583573 107.976195399426                 0                 0         0           0
      451.369889153454 916.384369930017 6.51574271027492 13.8523491257931 21.7176168772733 2.94277710817793 31.1837546535157  .201076270907684   .21333775966439  94.71033    .7518318
      451.369889153454 1122.28217183935 77.1800013672382                0 90.5445319204968 3.81898216032903 31.9882213711554                 0  .860082250045036 291.38632    2.915236
      205.376795115143 2867.53357406013 1576.00602753481 868.704675090933 600.096523924655 9.42653559191593 51.2008745402415  48.1613607378094  5.88924074110012 2689.7305   16.374865
      363.885342244299 591.026485686156 27.2457055753289                0 60.8442183298788 1.91431476533512 11.1963914233425                 0  .601821123879908 212.60303    1.890645
      386.807061761794 663.209026145417  2.7924611615464  4.6174497085977 21.5247270551642 2.14696627783889 49.5719781999676 .0670254236358946  .205969345484159  46.64822   .55554646
      55.3458193709967 708.306013840905 50.7900923618955                0 20.8646131852635 2.64800046349761 32.4139471877951                 0  .206818177846641  462.5886    2.488226
      234.281697177167 908.189781347051 505.627329879128                0 259.742697852189 3.12851724966101 65.9640247145401                 0  2.55151960558611  83.87016    .3154724
      45.2324600061143 1396.82186768098 46.5410193591066                0 62.7624547665193 4.53514520740146 152.024979949866                 0  .614762302321864 107.03002    .9886673
      19.0857413179985 1848.72687262672 57.3975395622786 104.244561010912 186.868678498439 6.50366283713709 40.9073249668782  .174979304166414  1.77405963048392   282.221   2.2555416
      94.4296855877104 2424.97489511727 847.691992432965 64.6442959203678 397.742539886595 7.84173567181813 144.662263779666  .777204016390373  3.76988025344736  995.6613    7.341254
      93.3650254646674 4015.79573109915 514.689604720227 27.7046982515862 264.413526696291 14.2800711531647 175.166426595277  .402152541815368  2.56683630303634 1055.1409   3.5656884
      46.8990181703696 8021.60485459742 753.145859255884 2976.68314385216 885.329324573215 26.2753233763819 211.043529074305  5.23531981437828  6.19091481308745 2312.7441    7.699667
       4.6526591370822 2575.03065560673 564.276475331041                0 285.145572221773 8.55530007128463 129.064234398587                 0  2.43812073027508 1490.6146    9.465966
      546.623360957683 4297.07414225932 636.291153223536  1303.0570126364  375.88524661935 14.0063308703115 29.4444924911398  39.5498011978849  3.44519019857971  376.2227    2.998769
       169.97693344836 1206.74817159587 53.0531008926381                0 15.6366841516368 4.11668140858519 49.0538418064652                 0  .153603186958361  31.62045   .24906354
      125.246161488266 715.637249820341 185.968850514229                0 21.7176168772733 2.39653376891468 79.9551351247466                 0  .213337759664391 15.022044   .20935085
      42.0963970710723 2705.88825128956  139.62305807732 347.481870036373 366.991864310453  9.2702641756912 .308366062841425  19.2645442951237  3.54633572281726 1084.8733    7.520451
      end

      Comment


      • #4
        Already explained: tabplot is from the Stata Journal — so you need to install it first. Type

        Code:
        search tabplot
        and use the most recent source indicated to install.

        At the time of writing that means clicking on
        gr066_1.

        Comment

        Working...
        X