Announcement

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

  • Stacked bar with multiple variables having same value labels

    Hi everyone,

    The figure below shows the reading behaviour of respondents when they signing up for credit card (see picture below). I use the following code to generate the picture.

    graph hbar, over(q6_2_01) percentage stack asyvars ///
    blabel(bar, pos(center) format(%3.0f) size(medium) color(black)) /// add percentage labels
    scheme(white_tableau) ///
    legend(pos(12) col(3)) ///
    title("When Purchasing Financial Products either In-Person or Online", box size(medsmall) fcolor(gs15) color(black) pos(12)) ///
    subtitle("N=3,020", size(small) box fcolor(gs15) color(black)) ///
    ytitle(Percent of Sample) /// title y-axis
    graphregion(margin(small)) /// make region between plot and outer edge of graph size small
    xsize(6.5) ysize(5.5) // make graph 6.5 inches wide and 4.5 inches tall
    Click image for larger version

Name:	tnc_credit_card.png
Views:	1
Size:	70.9 KB
ID:	1763116

    Question - Is there a way I can create a similar graph but for 3 different financial products in the same figure? The idea is to then compare if terms and condition reading behaviour differs by type of financial product.

    Any help on this will be highly appreciated. I've attached part of the data below. Missing values in the data means that the respondent does not own the specific financial product.


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int id byte(q6_2_01 q6_2_02 q6_2_03)
     36 4 . .
     39 . 3 .
     40 4 4 .
     41 . 4 .
     43 . 3 .
     44 3 3 .
     45 4 . .
     46 3 4 .
     47 . 1 .
     48 . 3 .
     49 4 4 .
     50 . 4 .
     51 3 3 .
     53 4 4 .
     54 3 . .
     55 . 3 .
     57 1 4 .
     58 . 3 .
     59 4 3 .
     60 . . .
     61 . 3 .
     62 4 3 .
     63 4 3 .
     64 . 4 .
     65 3 4 .
     66 3 . .
     67 . . .
     68 4 3 3
     69 . . .
     70 4 3 3
     73 4 4 4
     75 3 3 .
     76 2 2 .
     77 4 4 .
     78 . 3 .
     79 . 1 .
     81 4 3 4
     82 . . 4
     83 . . .
     84 . 3 .
     85 4 4 .
     86 3 3 .
     88 3 3 .
     89 . . .
     90 . 4 .
     91 4 3 .
     92 . 4 .
     93 1 3 3
     94 3 3 .
     95 . 3 .
     96 . 4 4
     97 3 3 .
     98 . 3 .
     99 3 3 .
    100 4 4 .
    101 . . .
    102 . 4 .
    103 4 4 .
    104 4 3 4
    105 . 2 .
    106 3 . .
    107 . . .
    108 3 . .
    109 . 4 .
    110 . 4 .
    111 . 2 .
    112 4 4 .
    113 . 4 .
    114 3 3 .
    115 4 2 .
    117 . 3 3
    118 . . .
    119 . 3 .
    120 . 4 .
    121 . 3 4
    123 . 3 3
    124 3 3 .
    125 . . .
    126 3 2 .
    127 . 4 .
    128 . . .
    129 . 2 2
    130 . 3 .
    132 1 1 .
    133 . 4 .
    134 . 3 .
    135 4 4 4
    136 4 . .
    138 . 3 .
    139 . 3 .
    140 3 . .
    141 2 3 .
    142 4 . .
    143 . 3 .
    144 . 1 .
    145 3 3 .
    146 . 2 .
    147 3 3 .
    148 1 1 .
    149 3 4 3
    end
    label values q6_2_01 tnc2
    label values q6_2_02 tnc2
    label values q6_2_03 tnc2
    label def tnc2 1 "Never Read", modify
    label def tnc2 2 "Rarely Read", modify
    label def tnc2 3 "Skim for Key Points", modify
    label def tnc2 4 "Always Read Carefully", modify


  • #2
    You can do this. The ways I find easiest hinge on a temporary reshape; I'd be happy if you found otherwise.

    graph bar and graph hbar can do stacking, as you have already found.. I sometimes find it easier to use catplot from SSC, which calls one of those commands any way.

    Stacking is widely practised but not always especially helpful for several reasons. A design I like separates the bars to allow easier comparison; better track of empty and rare categories when they occur; and easier annotation. That calls for tabplot from the Stata Journal. A bonus is that you can lose the legend.

    In your application being open about sample sizes as well as percents might be advisable if not essential.

    In what is below the tabplot code comes second but the result comes first.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int id byte(q6_2_01 q6_2_02 q6_2_03)
     36 4 . .
     39 . 3 .
     40 4 4 .
     41 . 4 .
     43 . 3 .
     44 3 3 .
     45 4 . .
     46 3 4 .
     47 . 1 .
     48 . 3 .
     49 4 4 .
     50 . 4 .
     51 3 3 .
     53 4 4 .
     54 3 . .
     55 . 3 .
     57 1 4 .
     58 . 3 .
     59 4 3 .
     60 . . .
     61 . 3 .
     62 4 3 .
     63 4 3 .
     64 . 4 .
     65 3 4 .
     66 3 . .
     67 . . .
     68 4 3 3
     69 . . .
     70 4 3 3
     73 4 4 4
     75 3 3 .
     76 2 2 .
     77 4 4 .
     78 . 3 .
     79 . 1 .
     81 4 3 4
     82 . . 4
     83 . . .
     84 . 3 .
     85 4 4 .
     86 3 3 .
     88 3 3 .
     89 . . .
     90 . 4 .
     91 4 3 .
     92 . 4 .
     93 1 3 3
     94 3 3 .
     95 . 3 .
     96 . 4 4
     97 3 3 .
     98 . 3 .
     99 3 3 .
    100 4 4 .
    101 . . .
    102 . 4 .
    103 4 4 .
    104 4 3 4
    105 . 2 .
    106 3 . .
    107 . . .
    108 3 . .
    109 . 4 .
    110 . 4 .
    111 . 2 .
    112 4 4 .
    113 . 4 .
    114 3 3 .
    115 4 2 .
    117 . 3 3
    118 . . .
    119 . 3 .
    120 . 4 .
    121 . 3 4
    123 . 3 3
    124 3 3 .
    125 . . .
    126 3 2 .
    127 . 4 .
    128 . . .
    129 . 2 2
    130 . 3 .
    132 1 1 .
    133 . 4 .
    134 . 3 .
    135 4 4 4
    136 4 . .
    138 . 3 .
    139 . 3 .
    140 3 . .
    141 2 3 .
    142 4 . .
    143 . 3 .
    144 . 1 .
    145 3 3 .
    146 . 2 .
    147 3 3 .
    148 1 1 .
    149 3 4 3
    end
    label values q6_2_01 tnc2
    label values q6_2_02 tnc2
    label values q6_2_03 tnc2
    label def tnc2 1 "never", modify
    label def tnc2 2 "rarely", modify
    label def tnc2 3 "skim", modify
    label def tnc2 4 "carefully", modify
    
    preserve 
    
    reshape long q6_2_0, i(id) j(which)
    
    label values q6_2_0 tnc2 
    label var q6_2_0 "needs better explanation"
    
    label def which 1 add 2 informative 3 detail
    label val which which 
    label var which "possibly redundant title"
    
    drop if missing(q6_2_0)
    
    catplot q6_2_0 which, asyvars stack percent(which) legend(pos(12) row(1)) name(G1, replace)
    
    bysort which (q6_2_0) : gen N = _N 
    
    bysort  which q6_2_0 : gen freq = _N 
    
    gen toshow = strofreal(freq) + " (" + strofreal(100 * freq/N, "%2.1f") + "%)"
    
    tabplot q6_2_0 which, percent(which) showval(toshow) separate(q6_2_0) yreverse subtitle(bars show %) name(G2, replace)
    Click image for larger version

Name:	stack2.png
Views:	1
Size:	35.1 KB
ID:	1763124
    Click image for larger version

Name:	stack1.png
Views:	1
Size:	23.1 KB
ID:	1763123

    Comment


    • #3
      Thanks a lot Nick Cox. This does exactly what I wanted. I'll most definitely be exploring "tabplot" and "catplot" in more detail.

      Comment

      Working...
      X