Announcement

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

  • (possibly) simple bar graph comparing two variables (not one variable over the other)

    Hello,


    I've spent hours since yesterday to plot a simple bar chart which displays two variables with grouped bars.
    However I have not made significant progess and that is why I am asking for Your help now.
    (I have not found any prior post here that touches on a problem similar to mine)



    The variables are both numerical and discrete in Stata, both only contain values of 1, 2, 3, 4, 5, or 6.

    I want to have the bars grouped so the number of observations with A=1 relative to the total observations (i.e. bars with percent values) is right next to the bar with the same for Variable B.

    The graph below shows what I would like to archieve in Stata.

    Every advice I found concerned problems where one would graph A over B.
    That is the same problem that I ran into when trying to get to my desired graph with the graphics editor.
    However, that is not what I am looking for and I did not manage to come up with any feasible workaround

    I would be really thankful for any advice on my problem.

    All I am asking for is the strucure of the command that groupes these bars with percent on the y-axis.

    I hope I have been clear and precise enough to solve my problem.
    I am sorry if that should not be the case and will correct my post.
    Click image for larger version

Name:	test.png
Views:	1
Size:	29.7 KB
ID:	1499518

  • #2
    Data example please.

    Comment


    • #3
      Thank you for your quick response, I hope this helps:
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str18 make int(price mpg rep78) float headroom int(trunk weight length turn displacement) float gear_ratio byte foreign float(A B)
      "AMC Concord"        4099 22 3 2.5 11 2930 186 40 121 3.58 0 5 4
      "AMC Pacer"          4749 17 3   3 11 3350 173 40 258 2.53 0 5 4
      "AMC Spirit"         3799 22 .   3 12 2640 168 35 121 3.08 0 4 3
      "Buick Century"      4816 20 3 4.5 16 3250 196 40 196 2.93 0 5 4
      "Buick Electra"      7827 15 4   4 20 4080 222 43 350 2.41 0 6 5
      "Buick LeSabre"      5788 18 3   4 21 3670 218 43 231 2.73 0 5 4
      "Buick Opel"         4453 26 .   3 10 2230 170 34 304 2.87 0 5 4
      "Buick Regal"        5189 20 3   2 16 3280 200 42 196 2.93 0 5 4
      "Buick Riviera"     10372 16 3 3.5 17 3880 207 43 231 2.93 0 6 6
      "Buick Skylark"      4082 19 3 3.5 13 3400 200 42 231 3.08 0 5 4
      "Cad. Deville"      11385 14 3   4 20 4330 221 44 425 2.28 0 6 6
      "Cad. Eldorado"     14500 14 2 3.5 16 3900 204 43 350 2.19 0 . .
      "Cad. Seville"      15906 21 3   3 13 4290 204 45 350 2.24 0 . .
      "Chev. Chevette"     3299 29 3 2.5  9 2110 163 34 231 2.93 0 4 3
      "Chev. Impala"       5705 16 4   4 20 3690 212 43 250 2.56 0 5 4
      "Chev. Malibu"       4504 22 3 3.5 17 3180 193 31 200 2.73 0 5 4
      "Chev. Monte Carlo"  5104 22 2   2 16 3220 200 41 200 2.73 0 5 4
      "Chev. Monza"        3667 24 2   2  7 2750 179 40 151 2.73 0 4 3
      "Chev. Nova"         3955 19 3 3.5 13 3430 197 43 250 2.56 0 4 3
      "Dodge Colt"         3984 30 5   2  8 2120 163 35  98 3.54 0 4 3
      "Dodge Diplomat"     4010 18 2   4 17 3600 206 46 318 2.47 0 5 4
      "Dodge Magnum"       5886 16 2   4 17 3600 206 46 318 2.47 0 5 4
      "Dodge St. Regis"    6342 17 2 4.5 21 3740 220 46 225 2.94 0 6 5
      "Ford Fiesta"        4389 28 4 1.5  9 1800 147 33  98 3.15 0 5 4
      "Ford Mustang"       4187 21 3   2 10 2650 179 43 140 3.08 0 5 4
      "Linc. Continental" 11497 12 3 3.5 22 4840 233 51 400 2.47 0 6 6
      "Linc. Mark V"      13594 12 3 2.5 18 4720 230 48 400 2.47 0 . .
      "Linc. Versailles"  13466 14 3 3.5 15 3830 201 41 302 2.47 0 . .
      "Merc. Bobcat"       3829 22 4   3  9 2580 169 39 140 2.73 0 4 3
      "Merc. Cougar"       5379 14 4 3.5 16 4060 221 48 302 2.75 0 5 4
      "Merc. Marquis"      6165 15 3 3.5 23 3720 212 44 302 2.26 0 6 5
      "Merc. Monarch"      4516 18 3   3 15 3370 198 41 250 2.43 0 5 4
      "Merc. XR-7"         6303 14 4   3 16 4130 217 45 302 2.75 0 6 5
      "Merc. Zephyr"       3291 20 3 3.5 17 2830 195 43 140 3.08 0 4 3
      "Olds 98"            8814 21 4   4 20 4060 220 43 350 2.41 0 6 6
      "Olds Cutl Supr"     5172 19 3   2 16 3310 198 42 231 2.93 0 5 4
      "Olds Cutlass"       4733 19 3 4.5 16 3300 198 42 231 2.93 0 5 4
      "Olds Delta 88"      4890 18 4   4 20 3690 218 42 231 2.73 0 5 4
      "Olds Omega"         4181 19 3 4.5 14 3370 200 43 231 3.08 0 5 4
      "Olds Starfire"      4195 24 1   2 10 2730 180 40 151 2.73 0 5 4
      "Olds Toronado"     10371 16 3 3.5 17 4030 206 43 350 2.41 0 6 6
      "Plym. Arrow"        4647 28 3   2 11 3260 170 37 156 3.05 0 5 4
      "Plym. Champ"        4425 34 5 2.5 11 1800 157 37  86 2.97 0 5 4
      "Plym. Horizon"      4482 25 3   4 17 2200 165 36 105 3.37 0 5 4
      "Plym. Sapporo"      6486 26 . 1.5  8 2520 182 38 119 3.54 0 6 5
      "Plym. Volare"       4060 18 2   5 16 3330 201 44 225 3.23 0 5 4
      "Pont. Catalina"     5798 18 4   4 20 3700 214 42 231 2.73 0 5 4
      "Pont. Firebird"     4934 18 1 1.5  7 3470 198 42 231 3.08 0 5 4
      "Pont. Grand Prix"   5222 19 3   2 16 3210 201 45 231 2.93 0 5 4
      "Pont. Le Mans"      4723 19 3 3.5 17 3200 199 40 231 2.93 0 5 4
      "Pont. Phoenix"      4424 19 . 3.5 13 3420 203 43 231 3.08 0 5 4
      "Pont. Sunbird"      4172 24 2   2  7 2690 179 41 151 2.73 0 5 4
      "Audi 5000"          9690 17 5   3 15 2830 189 37 131  3.2 1 6 6
      "Audi Fox"           6295 23 3 2.5 11 2070 174 36  97  3.7 1 6 5
      "BMW 320i"           9735 25 4 2.5 12 2650 177 34 121 3.64 1 6 6
      "Datsun 200"         6229 23 4 1.5  6 2370 170 35 119 3.89 1 6 5
      "Datsun 210"         4589 35 5   2  8 2020 165 32  85  3.7 1 5 4
      "Datsun 510"         5079 24 4 2.5  8 2280 170 34 119 3.54 1 5 4
      "Datsun 810"         8129 21 4 2.5  8 2750 184 38 146 3.55 1 6 6
      "Fiat Strada"        4296 21 3 2.5 16 2130 161 36 105 3.37 1 5 4
      "Honda Accord"       5799 25 5   3 10 2240 172 36 107 3.05 1 5 4
      "Honda Civic"        4499 28 4 2.5  5 1760 149 34  91  3.3 1 5 4
      "Mazda GLC"          3995 30 4 3.5 11 1980 154 33  86 3.73 1 4 3
      "Peugeot 604"       12990 14 . 3.5 14 3420 192 38 163 3.58 1 . .
      "Renault Le Car"     3895 26 3   3 10 1830 142 34  79 3.72 1 4 3
      "Subaru"             3798 35 5 2.5 11 2050 164 36  97 3.81 1 4 3
      "Toyota Celica"      5899 18 5 2.5 14 2410 174 36 134 3.06 1 5 4
      "Toyota Corolla"     3748 31 5   3  9 2200 165 35  97 3.21 1 4 3
      "Toyota Corona"      5719 18 5   2 11 2670 175 36 134 3.05 1 5 4
      "VW Dasher"          7140 23 4 2.5 12 2160 172 36  97 3.74 1 6 5
      "VW Diesel"          5397 41 5   3 15 2040 155 35  90 3.78 1 5 4
      "VW Rabbit"          4697 25 4   3 15 1930 155 35  89 3.78 1 5 4
      "VW Scirocco"        6850 25 4   2 16 1990 156 36  97 3.78 1 6 5
      "Volvo 260"         11995 17 5 2.5 14 3170 193 37 163 2.98 1 6 6
      end
      label values foreign origin
      label def origin 0 "Domestic", modify
      label def origin 1 "Foreign", modify

      Comment


      • #4
        OK, but that's the auto data, which can be accessed easily. I wanted the data underlying #1. Still, something can be faked.

        Code:
        sysuse auto, clear
        bysort foreign rep78 : gen count = _N
        separate count, by(foreign) veryshortlabel
        graph bar count? , over(rep78) ytitle(Frequency) yla(, ang(h))  ///
        b1title("`: var label rep78'") legend(pos(1) ring(0) col(1) order(1 "Domestic" 2 "Foreign"))


        Comment


        • #5
          Thank you for helping me out!

          I was worried using my own data set due to confidentiality reasons and I'm very happy that you were able to solve it for me with only a standard dataset.

          Comment


          • #6
            Further to the solution of Nick.

            An alternative is provided by coefplotwritten by Ben Jann. Note that data is pre-processed by proportion.
            Code:
            ssc install coefplot
            h coefplot
            sysuse auto , clear
            tab rep foreign
            proportion rep if foreign==0
            estimates store domestic
            proportion rep if foreign==1
            estimates store foreign
            coefplot domestic foreign, vertical recast(bar) barwidth(0.25)fcolor(*.5) ciopts(recast(rcap))citop citype(logit) xtitle(RepairRecord1978) ytitle(Proportion)
            graph export coefplot_rep_by_domestic_foreign.png , replace
            which produces:
            Click image for larger version

Name:	coefplot_rep_by_domestic_foreign.png
Views:	1
Size:	30.2 KB
ID:	1499551




            To apply this to the data of #1, I had to create some auxiliary variables (just for the example):
            Code:
            * To run the code below, first clear all and use code from #1 (!)
            expand 2
            gen AB=.
            replace AB=A if _n<=74
            replace AB=B if _n>74
            gen ABt=.
            replace ABt=1 if _n<=74
            replace ABt=2 if _n>74
            
            tab AB ABt
            proportion AB if ABt==2
            estimates store Bt
            proportion AB if ABt==1
            estimates store At
            coefplot Bt At , vertical recast(bar) barwidth(0.25)fcolor(*.5) ciopts(recast(rcap))citop citype(logit) xtitle(Some comparison) ytitle(Proportion)
            Note that I had to compute first Bt because to get the proper order on the x-axis, coefplot expects some data in the first (which is not available here with At).
            which produces:
            Click image for larger version

Name:	coefplot_AB_by_ABt.png
Views:	1
Size:	29.3 KB
ID:	1499552




            I hope that this gives some inspiration as well.
            Last edited by ericmelse; 22 May 2019, 09:27.
            http://publicationslist.org/eric.melse

            Comment


            • #7
              #5 We address this in the FAQ Advice

              If your dataset is confidential, then provide a fake example instead.

              Comment

              Working...
              X