Announcement

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

  • multiple response binary variables with tabplot

    Hello all, I understand how useful tabplot can be when plotting graphs. So, I was wondering if it is possible to use it to plot several binary bar graphs where only the yes responses will be plotted horizontally and disaggregated by state, area and gender as contained in the data below. I provided four binary variables, that is, mh2_1_mult mh2_2_mult mh2_3_mult and mh2_4_mult. I used statplot to get the graph below but I need it to be more informative in terms of being able to show n(%) and be able to disaggregate by state, area and gender.


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(state sex) float(mh2_1_mult mh2_2_mult mh2_3_mult mh2_4_mult) byte(area _freq)
    1 1 0 0 0 0 1  8
    1 1 0 1 0 1 1  1
    1 1 0 1 1 1 1  1
    1 1 1 0 0 0 1  9
    1 1 1 0 1 1 1  3
    1 1 1 1 0 0 1  5
    1 1 1 1 0 1 1  3
    1 1 1 1 1 0 1  7
    1 1 1 1 1 1 1 35
    1 2 0 0 0 0 1  2
    1 2 0 0 1 1 1  2
    1 2 0 1 0 0 1  2
    1 2 1 0 0 0 1  5
    1 2 1 0 1 0 1  1
    1 2 1 0 1 1 1  2
    1 2 1 1 0 0 1 22
    1 2 1 1 0 1 1  3
    1 2 1 1 1 0 1  8
    1 2 1 1 1 1 1 35
    1 1 0 0 0 0 2 22
    1 1 0 0 0 1 2  2
    1 1 0 1 1 0 2  1
    1 1 1 0 0 0 2 33
    1 1 1 0 0 1 2  9
    1 1 1 0 1 0 2  1
    1 1 1 1 0 0 2  3
    1 2 0 0 0 0 2  3
    1 2 0 0 1 0 2  4
    1 2 0 1 0 0 2  2
    1 2 0 1 0 1 2  2
    1 2 0 1 1 0 2  4
    1 2 0 1 1 1 2  1
    1 2 1 0 0 0 2 17
    1 2 1 0 1 0 2  4
    1 2 1 1 0 0 2 35
    1 2 1 1 0 1 2  3
    1 2 1 1 1 0 2  4
    2 1 0 0 0 0 1 21
    2 1 0 0 0 1 1  5
    2 1 0 0 1 0 1  5
    2 1 0 0 1 1 1  1
    2 1 0 1 0 0 1  6
    2 1 0 1 0 1 1  1
    2 1 0 1 1 0 1  1
    2 1 0 1 1 1 1  1
    2 1 1 0 0 0 1  9
    2 1 1 0 0 1 1  2
    2 1 1 0 1 0 1  6
    2 1 1 1 0 0 1 17
    2 1 1 1 0 1 1  1
    2 1 1 1 1 0 1 15
    2 1 1 1 1 1 1  3
    2 2 0 0 0 0 1  1
    2 2 0 0 0 1 1  1
    2 2 0 0 1 0 1  3
    2 2 0 1 0 0 1 13
    2 2 0 1 0 1 1  2
    2 2 0 1 1 0 1  7
    2 2 1 0 0 0 1  5
    2 2 1 0 0 1 1  3
    2 2 1 0 1 0 1  9
    2 2 1 0 1 1 1  1
    2 2 1 1 0 0 1 29
    2 2 1 1 0 1 1  3
    2 2 1 1 1 0 1 15
    2 2 1 1 1 1 1  4
    2 1 0 0 0 0 2 28
    2 1 0 0 0 1 2  2
    2 1 0 0 1 0 2  4
    2 1 0 0 1 1 2  2
    2 1 0 1 0 0 2 11
    2 1 0 1 0 1 2  2
    2 1 0 1 1 0 2  4
    2 1 1 0 0 0 2 14
    2 1 1 0 0 1 2  3
    2 1 1 0 1 0 2  9
    2 1 1 0 1 1 2  2
    2 1 1 1 0 0 2 13
    2 1 1 1 0 1 2  1
    2 1 1 1 1 0 2 14
    2 1 1 1 1 1 2  4
    2 2 0 0 0 0 2  6
    2 2 0 0 0 1 2  2
    2 2 0 0 1 0 2  1
    2 2 0 1 0 0 2  1
    2 2 0 1 1 1 2  1
    2 2 1 0 0 0 2 17
    2 2 1 0 0 1 2  8
    2 2 1 0 1 0 2 10
    2 2 1 0 1 1 2  2
    2 2 1 1 0 0 2 19
    2 2 1 1 0 1 2  6
    2 2 1 1 1 0 2 28
    2 2 1 1 1 1 2  5
    3 1 0 0 0 0 1 26
    3 1 0 0 1 0 1  4
    3 1 0 0 1 1 1  2
    3 1 0 1 0 0 1  7
    3 1 0 1 0 1 1  1
    3 1 0 1 1 0 1  4
    end
    label values state state
    label def state 1 "State1", modify
    label def state 2 "State2", modify
    label def state 3 "State3", modify
    label values sex sex
    label def sex 1 "Male", modify
    label def sex 2 "Female", modify
    label values mh2_1_mult yesno
    label values mh2_2_mult yesno
    label values mh2_3_mult yesno
    label values mh2_4_mult yesno
    label def yesno 0 "No", modify
    label def yesno 1 "Yes", modify
    label values area area
    label def area 1 "Rural", modify
    label def area 2 "Urban", modify
    Code:
    statplot mh2_1_mult mh2_2_mult mh2_3_mult mh2_4_mult, ytitle(Materials used to take care of MH) name(G2, replace)
    Click image for larger version

Name:	multiple.jpg
Views:	1
Size:	22.1 KB
ID:	1767523

  • #2
    I should be able to get to this later today.

    Comment


    • #3
      tabplot is from the Stata Journal.

      statplot is from SSC.

      So, that is for 4 binary variables for 3 states, 2 sexes, 2 kinds of area.

      I would reshape to 4 categories of one composite variable and reduce to percent saying Yes. A framed bar shows the complement too.

      I would advise caution on trying to add too much detail.
      ​​​​​​​

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input byte(state sex) float(mh2_1_mult mh2_2_mult mh2_3_mult mh2_4_mult) byte(area _freq)
      1 1 0 0 0 0 1  8
      1 1 0 1 0 1 1  1
      1 1 0 1 1 1 1  1
      1 1 1 0 0 0 1  9
      1 1 1 0 1 1 1  3
      1 1 1 1 0 0 1  5
      1 1 1 1 0 1 1  3
      1 1 1 1 1 0 1  7
      1 1 1 1 1 1 1 35
      1 2 0 0 0 0 1  2
      1 2 0 0 1 1 1  2
      1 2 0 1 0 0 1  2
      1 2 1 0 0 0 1  5
      1 2 1 0 1 0 1  1
      1 2 1 0 1 1 1  2
      1 2 1 1 0 0 1 22
      1 2 1 1 0 1 1  3
      1 2 1 1 1 0 1  8
      1 2 1 1 1 1 1 35
      1 1 0 0 0 0 2 22
      1 1 0 0 0 1 2  2
      1 1 0 1 1 0 2  1
      1 1 1 0 0 0 2 33
      1 1 1 0 0 1 2  9
      1 1 1 0 1 0 2  1
      1 1 1 1 0 0 2  3
      1 2 0 0 0 0 2  3
      1 2 0 0 1 0 2  4
      1 2 0 1 0 0 2  2
      1 2 0 1 0 1 2  2
      1 2 0 1 1 0 2  4
      1 2 0 1 1 1 2  1
      1 2 1 0 0 0 2 17
      1 2 1 0 1 0 2  4
      1 2 1 1 0 0 2 35
      1 2 1 1 0 1 2  3
      1 2 1 1 1 0 2  4
      2 1 0 0 0 0 1 21
      2 1 0 0 0 1 1  5
      2 1 0 0 1 0 1  5
      2 1 0 0 1 1 1  1
      2 1 0 1 0 0 1  6
      2 1 0 1 0 1 1  1
      2 1 0 1 1 0 1  1
      2 1 0 1 1 1 1  1
      2 1 1 0 0 0 1  9
      2 1 1 0 0 1 1  2
      2 1 1 0 1 0 1  6
      2 1 1 1 0 0 1 17
      2 1 1 1 0 1 1  1
      2 1 1 1 1 0 1 15
      2 1 1 1 1 1 1  3
      2 2 0 0 0 0 1  1
      2 2 0 0 0 1 1  1
      2 2 0 0 1 0 1  3
      2 2 0 1 0 0 1 13
      2 2 0 1 0 1 1  2
      2 2 0 1 1 0 1  7
      2 2 1 0 0 0 1  5
      2 2 1 0 0 1 1  3
      2 2 1 0 1 0 1  9
      2 2 1 0 1 1 1  1
      2 2 1 1 0 0 1 29
      2 2 1 1 0 1 1  3
      2 2 1 1 1 0 1 15
      2 2 1 1 1 1 1  4
      2 1 0 0 0 0 2 28
      2 1 0 0 0 1 2  2
      2 1 0 0 1 0 2  4
      2 1 0 0 1 1 2  2
      2 1 0 1 0 0 2 11
      2 1 0 1 0 1 2  2
      2 1 0 1 1 0 2  4
      2 1 1 0 0 0 2 14
      2 1 1 0 0 1 2  3
      2 1 1 0 1 0 2  9
      2 1 1 0 1 1 2  2
      2 1 1 1 0 0 2 13
      2 1 1 1 0 1 2  1
      2 1 1 1 1 0 2 14
      2 1 1 1 1 1 2  4
      2 2 0 0 0 0 2  6
      2 2 0 0 0 1 2  2
      2 2 0 0 1 0 2  1
      2 2 0 1 0 0 2  1
      2 2 0 1 1 1 2  1
      2 2 1 0 0 0 2 17
      2 2 1 0 0 1 2  8
      2 2 1 0 1 0 2 10
      2 2 1 0 1 1 2  2
      2 2 1 1 0 0 2 19
      2 2 1 1 0 1 2  6
      2 2 1 1 1 0 2 28
      2 2 1 1 1 1 2  5
      3 1 0 0 0 0 1 26
      3 1 0 0 1 0 1  4
      3 1 0 0 1 1 1  2
      3 1 0 1 0 0 1  7
      3 1 0 1 0 1 1  1
      3 1 0 1 1 0 1  4
      end
      label values state state
      label def state 1 "State1", modify
      label def state 2 "State2", modify
      label def state 3 "State3", modify
      label values sex sex
      label def sex 1 "Male", modify
      label def sex 2 "Female", modify
      label values mh2_1_mult yesno
      label values mh2_2_mult yesno
      label values mh2_3_mult yesno
      label values mh2_4_mult yesno
      label def yesno 0 "No", modify
      label def yesno 1 "Yes", modify
      label values area area
      label def area 1 "Rural", modify
      label def area 2 "Urban", modify
      
      gen id = _n
      
      reshape long mh2_@_mult , i(id) j(which)
      
      bysort state sex area which : egen total = total(_freq)
      
      collapse mh2 total [fw=_freq], by(which state sex area)
      
      gen toshow = strofreal(100 * mh2, "%2.1f") + "%"
      
      set scheme stcolor
      
      label def which 1 "clean materials" 2 "washing hands and body" 3 "disposal of materials" 4 "privacy, respect, comfort"
      label val which which
      
      local opts xtitle("") ytitle("") frame(1) showval(toshow) frameopts(lc(stc2))
      
      local byopts , note("") compact
      
      tabplot which state [iw=mh2] if area == 1, by(sex `byopts' title(Rural)) `opts' name(G1, replace)
      
      tabplot which state [iw=mh2] if area == 2, by(sex `byopts' title(Urban)) `opts' name(G2, replace)
      
      graph combine G1 G2

      Comment


      • #4
        Originally posted by Nick Cox View Post
        Thank you very much for your support. I was thinking if there is a way of modifying the ylabels and xlables in tabplot. I haven't seen what enables such modification yet in my searches. Thanks.

        Comment


        • #5
          xlabel() and ylabel() apply. The only twist -- which is explained in the help -- is that whatever the original categories are -- string or numeric -- they are by default mapped to integers 1 up. Hence

          Code:
          . sysuse auto
          (1978 automobile data)
          
          . tabplot foreign rep78
          
          . tabplot foreign rep78, yla(1 "made in US" 2 "not made in US")
          
          . tabplot foreign rep78, yla(1 "made in US" 2 "not made in US") xla(3 "mediocre")
          In this example foreign has distinct values 0 and 1 but they are mapped to 1 and 2, while rep78 has values 1 2 3 4 5, so they are mapped to the same numbers.

          You can override that default by using xasis and/or yasis according to taste or need.

          I sense a temptation to pack very long value labels into your display. The simple but also hard question about that is whether your readers will really prefer that.

          Comment


          • #6
            Originally posted by Nick Cox View Post
            Actually, I am trying to extend the code to the actual dataset which has more binary variables than what I initially shared. I have decided to have Rural and Uban graphs separately but the xlabels are overlapping. So, I was looking at how to overcome the overlapping and reduce the font size of the labels as well. Attached is an image of my output showing the overlapping.
            Click image for larger version

Name:	G1.jpg
Views:	1
Size:	140.5 KB
ID:	1767668










            Comment


            • #7
              I mean this in a friendly way, but I have already warned twice about trying to include too much! In Britain it is proverbial that you can't get a quart [two pints] into a pint pot.

              Comment

              Working...
              X