Announcement

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

  • catplot - how to show n to the left of the bars, percentages within bars, and mean values outside the bars (to the right)

    Dear all,

    I am using the community-contributed command catplot from SSC in Stata 16.1 in order to produce a number of graphs (with horizontal bars) for a report I am writing. What I am trying to do is cram a whole lot of information into the graph. I know it looks bloated, but there are reasons why I want to do this, so please bare with me.

    Here is what i (ideally) want: To show the number of observations to the left of the bar and then have percentages within the bars and mean values (one decimal point) to the right outside the bars.

    I have managed to cram everything into the graph, but where I am stuck is dealing with the mean value. I have found a way to show it to the left of the bar, but what I want is for it to show to the right of the bar. Can anyone please help me?

    Here is an excerpt of my data, followed by the code I have written:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float lpenr byte nivaa int(foreles sem gruppe innlev prosj felt lab annen_pr case sim praksis veil)
      1 . .  .  .  .  .  .  .  .  .  .  .  .
      2 1 5  4  1  1  1  1  1  1  1  1  1  2
      3 2 5  .  5  3  5  5  5  5  5  4  5  5
      4 1 4  4  .  4  .  .  .  .  .  .  .  4
      5 3 2  2  1  4  5  5  5  5  5  1  5  5
      6 2 2  4  2  1  2  1  1  4  3  3  3  2
      7 1 5  5  1  4  4  1  1  4 .a  1  1  2
      8 2 2  3  3  5  .  2  1  1  1  1  1  4
      9 3 2  5  2  4  3  1  1  1  1  3  1  2
     10 3 4  4 .a  3  1  1  1  1  1  1  1  4
     11 1 2 .a  2  5  4  1  1  1  1  1  1  5
     12 2 4  3  3  3  3  2  3  5  5  3  5  5
     13 2 4  3  4  3  1  1  1  3  1  3  1 .a
     14 2 5  2  2  3  3  1  1  3  1  1  1  1
     15 1 5  1  1  5  1  1  1  1  1  1  1  5
     16 3 2  2  2  2  5  3  3  4  3  4  4  4
     17 1 3  2  1  1  2  1  2  2  2  1  1  3
     18 2 4  2  1  1  2  1  1  1  3  3  1  2
     19 1 3  3  1  3  5  1  1  1  1  1  1  5
     20 2 2  5  2  4  1  4  1  1  1  1  1  5
     21 3 4  3  3  3  1  1  1  1  2  1  1  4
     22 1 3  5 .a  5  1  1  1  5  1  1  1  1
     23 1 5  5 .a  4  3  5  1  1  1  1  1  3
     24 2 4  1  1  1  1  1  3  1  1  1  1  1
     25 3 5  2  1  4  4  4  4  4  1  1  3  4
     26 3 5  2  2  2  3  1  1  5  3  2  5  5
     27 1 4  3  3  4  3  2  4  1  1  1  2  1
     28 3 4  2  1  4  1  4  1  3  1  1  1  4
     29 3 3  3  5  5  5  5  1 .a .a .a .a .a
     30 3 5  5 .a  5  1  3  1  3  3  1  1  5
     31 3 5  4  3  4  1  3  1  1  1  1  1  5
     32 2 5  1  1  3  2  1  1  3  1  1  1  1
     33 2 5  3  4  2  5  1  1  1  1  1  3  4
     34 2 4  3  3  2  3  2  3  4  2  2  4  2
     35 1 3  3  3  3  3  2  2  4  3  4  4  4
     36 2 4  1  1  3  1  .  3  1  1  1  1  1
     37 2 5  4  1  1  1  1  1  2  5  1  2  3
     38 1 5  .  .  .  .  .  .  .  .  .  .  .
     39 2 .  .  .  .  .  .  .  .  .  .  .  .
     40 3 5  3  3  3  1  2  1  2  1  1  1  2
     41 2 5  2  3  1  1  1  1  1  1  1  1  1
     42 2 3  3  3  3  3  1  1  3  3  3  1  2
     43 2 4  4  3  4  5  3  1  1  1  1  4  3
     44 2 4  4 .a  2  3  2  2  4  3  1  5  5
     45 1 3  4  1  4  1  1  1  1  1  1  1  3
     46 2 5  5  2  4  4  4  1  1  2  1  1  4
     47 2 4  3  1  3  3  2  1  4  2  1  4  3
     48 3 .  .  .  .  .  .  .  5  .  .  .  5
     49 3 5  1  1  1  1  1  1  1  1  1  1  1
     50 3 4  3  3  2  2 .a  2  3  3  1  1  3
     51 1 3  3  4  4  1  1  1  1  1  1  4  4
     52 2 4  2  2  2  2  2  4  2  2  2  4  3
     53 1 5  4  1  5  1  1  5  1  1  1  1  1
     54 1 5  1  2  4  4  1  1  1  1  1  1  4
     55 2 4  3  3  3  2  2  3  4  4  3  5  5
     56 3 5  5  3  3  1  2  1  4  1  1  1  1
     57 2 4  1  3  3  1  1  1  1  3  1  1  .
     58 1 3  .  3  3  3  .  .  3  .  .  .  .
     59 2 4  .  .  4  .  3  4  .  .  .  .  .
     60 3 4  1  4  4  4  1  1  4  4  1  4  4
     61 1 4  1  1  4  1  1  1  1  1  1  1  1
     62 3 5  3 .a  4  4  3 .a .a .a .a  2  4
     63 3 4  4  2  3  1  1  1  1  1  1  1  2
     64 2 1  4  1  2  1  1  1  1  5  1  1  3
     65 1 3  3  .  3  1  1  1  4  1  1  1  1
     66 2 4  3 .a .a  2 .a .a .a .a .a  4  3
     67 3 2  3  3  3  4  1  3  1  1  1  4  3
     68 1 4  4  3  3  3  3  2  3  3  2  5  5
     69 1 5  4  4  3  3  1  1  1  1  1  1  1
     70 2 4  4  3  5  4  1  1  3  3  1  1  5
     71 1 5  1  5  2  5  1  1  2  1  1  1  1
     72 3 5  4  3  3  3  3  4  2  3  1  1  3
     73 1 5  2  2  2  2  1  1  4  4  4  4  4
     74 2 3  1  1  4  .  3  1  3  3  1  5  5
     75 3 2  2  2  3  3  3  3  3  1  1  3  3
     76 1 4  3  2  3  1  1  1  1  1  1  1  3
     77 1 4  3  3  2  1  1  3  1  1  1  1  1
     78 3 4  4  2  3  4  3  3  4  4  4  4  4
     79 2 4  .  4  .  4  .  .  .  .  .  .  .
     80 3 4  1  2  4  4  1  1  1  1  1  1  1
     81 3 5  2  2  2  1  1  3  1  1  1  1  1
     82 2 5  1  1  1  1  1  1  1  3  1  1  1
     83 3 5  3  .  .  .  .  5  5  .  .  .  .
     84 1 5  4  1  3  3  1  1  1  1  1  1  1
     85 2 4  3  2  1  1  1  1  4  3  4  4  2
     86 3 5  4  3  5  1  1  4  1  1  1  1  2
     87 2 5  3 .a  3 .a .a .a .a .a .a .a .a
     88 3 5  1  3  3  1  2  3  3  3  1  1  1
     89 2 3  3  1  3  1  1  1  3  1  1  1  3
     90 2 5  1  1  1  5  5  1  1  1  1  1  5
     91 1 2  3  1  1  3  3  1  1  1  1  5  5
     92 1 3  2  .  5  5  5  1  5  1  1  1  1
     93 3 4  4  .  2  2  1  1  2  1  1  1  2
     94 1 5  3  .  .  .  .  .  .  .  .  .  3
     95 2 5  3  2  3  2  2  1  3  4  4  3  3
     96 1 4  4  2  4  3  4  1  2  1  1  1  3
     97 3 4  2  2  2  2  2  2  2  1  1  2  3
     98 3 3  3 .a  4  5  5 .a .a .a  1  4  1
     99 1 2  5  5  3  5  1  1  5  5  1  1  4
    100 3 5  4  4  4  1  1  4  2  2  2  3  3
    end
    label values nivaa nivaa
    label def nivaa 1 "Bachelorgradsnivå", modify
    label def nivaa 2 "Mastergradsnivå", modify
    label def nivaa 3 "Både bachelor- og mastergradsnivå", modify
    label values foreles foreles
    label def foreles 1 "I ingen / i svært liten grad", modify
    label def foreles 2 "I liten grad", modify
    label def foreles 3 "I noen grad", modify
    label def foreles 4 "I stor grad", modify
    label def foreles 5 "I svært stor grad", modify
    label values sem sem
    label def sem 1 "I ingen / i svært liten grad", modify
    label def sem 2 "I liten grad", modify
    label def sem 3 "I noen grad", modify
    label def sem 4 "I stor grad", modify
    label def sem 5 "I svært stor grad", modify
    label def sem .a "Vet ikke", modify
    label values gruppe gruppe
    label def gruppe 1 "I ingen / i svært liten grad", modify
    label def gruppe 2 "I liten grad", modify
    label def gruppe 3 "I noen grad", modify
    label def gruppe 4 "I stor grad", modify
    label def gruppe 5 "I svært stor grad", modify
    label def gruppe .a "Vet ikke", modify
    label values innlev innlev
    label def innlev 1 "I ingen / i svært liten grad", modify
    label def innlev 2 "I liten grad", modify
    label def innlev 3 "I noen grad", modify
    label def innlev 4 "I stor grad", modify
    label def innlev 5 "I svært stor grad", modify
    label def innlev .a "Vet ikke", modify
    label values prosj prosj
    label def prosj 1 "I ingen / i svært liten grad", modify
    label def prosj 2 "I liten grad", modify
    label def prosj 3 "I noen grad", modify
    label def prosj 4 "I stor grad", modify
    label def prosj 5 "I svært stor grad", modify
    label def prosj .a "Vet ikke", modify
    label values felt felt
    label def felt 1 "I ingen / i svært liten grad", modify
    label def felt 2 "I liten grad", modify
    label def felt 3 "I noen grad", modify
    label def felt 4 "I stor grad", modify
    label def felt 5 "I svært stor grad", modify
    label def felt .a "Vet ikke", modify
    label values lab lab
    label def lab 1 "I ingen / i svært liten grad", modify
    label def lab 2 "I liten grad", modify
    label def lab 3 "I noen grad", modify
    label def lab 4 "I stor grad", modify
    label def lab 5 "I svært stor grad", modify
    label def lab .a "Vet ikke", modify
    label values annen_pr annen_pr
    label def annen_pr 1 "I ingen / i svært liten grad", modify
    label def annen_pr 2 "I liten grad", modify
    label def annen_pr 3 "I noen grad", modify
    label def annen_pr 4 "I stor grad", modify
    label def annen_pr 5 "I svært stor grad", modify
    label def annen_pr .a "Vet ikke", modify
    label values case case
    label def case 1 "I ingen / i svært liten grad", modify
    label def case 2 "I liten grad", modify
    label def case 3 "I noen grad", modify
    label def case 4 "I stor grad", modify
    label def case 5 "I svært stor grad", modify
    label def case .a "Vet ikke", modify
    label values sim sim
    label def sim 1 "I ingen / i svært liten grad", modify
    label def sim 2 "I liten grad", modify
    label def sim 3 "I noen grad", modify
    label def sim 4 "I stor grad", modify
    label def sim .a "Vet ikke", modify
    label values praksis praksis
    label def praksis 1 "I ingen / i svært liten grad", modify
    label def praksis 2 "I liten grad", modify
    label def praksis 3 "I noen grad", modify
    label def praksis 4 "I stor grad", modify
    label def praksis 5 "I svært stor grad", modify
    label def praksis .a "Vet ikke", modify
    label values veil veil
    label def veil 1 "I ingen / i svært liten grad", modify
    label def veil 2 "I liten grad", modify
    label def veil 3 "I noen grad", modify
    label def veil 4 "I stor grad", modify
    label def veil 5 "I svært stor grad", modify
    label def veil .a "Vet ikke", modify
    Code:
    preserve
    
    *rename in order to reshape
    rename foreles     b1_var1 
    rename sem         b1_var2 
    rename gruppe   b1_var3 
    rename innlev   b1_var4 
    rename prosj    b1_var5
    rename felt     b1_var6
    rename lab      b1_var7
    rename annen_pr b1_var8
    rename case     b1_var9
    rename sim      b1_var10
    rename praksis  b1_var11
    rename veil     b1_var12
    
    *reshape from wide to long
    keep lpenr b1* nivaa 
    reshape long b1_var, i(lpenr) j(battcount)
    rename b1_var b1vars
    
    *Save n for later
    levelsof battcount, local(bcn) 
    foreach n in `bcn' {
    su b1vars if b1vars != . & battcount == `n'
    local bcn_`n' `"`r(N)'"'
     }
    *Save mean for later 
    levelsof battcount, local(bcm) 
    foreach m in `bcm' {
    su b1vars if b1vars != . & battcount == `m'
    local bcm_`m' `"`: di %2.1f r(mean)' "'
     }
     
    *Relabel variable(s) and draw on information stored in the local macros
    la def battcount 1"Forelesning (n=`bcn_1', m=`bcm_1')" 2"Seminar (n=`bcn_2', m=`bcm_2')" 3"Gruppe (n=`bcn_3', m=`bcm_3')" 4"Innlevering (n=`bcn_4', m=`bcm_4')" 5"Prosjekt (n=`bcn_5', m=`bcm_5')" 6"Feltarbeid (n=`bcn_6', m=`bcm_6')" 7"Lab (n=`bcn_7', m=`bcm_7')" 8"An. prakt.øv. (n=`bcn_8', m=`bcm_8')" 9"Case (n=`bcn_9', m=`bcm_9')" 10"Simulering (n=`bcn_10', m=`bcm_10')" 11"Praksis (n=`bcn_11', m=`bcm_11')" 12"Veiledning (n=`bcn_12', m=`bcm_12')"
    la val battcount battcount
    
    *catplot with percent within bars, n to the left of the bar and (ideally) mean to the right of the bar (outside the bar)
    catplot b1vars, over(battcount) percent(battcount) asyvars stack blabel(bar, pos(center) format(%2.1f) size(small)) intensity(65) bargap(20) ylabel(`label', nogrid labsize(small)) ytitle(" ", size(small))  scheme(s2color) ysize(3.5)  plotregion(lcolor(none) margin(small)) l1title(" ") title("Some title" " ", size(medlarge)) graphregion(margin(medlarge)) legend(position(12) row(1) keygap(0.8) symxsize(4) size(*0.9) region(lcolor(white)) margin(tiny) colgap(5)) scale(0.8) note("Note: An.prakt.øv = Annen praktisk øving", size(medsmall) span) 
    
    restore
    Here is the resulting .png:

    Click image for larger version

Name:	catplot-n-percent-mean.png
Views:	1
Size:	57.7 KB
ID:	1633703



    As you can see, all the "extra" information is placed to the left of the bars (together with the n -count). It is a bit too much information in one place.

    Is there a way to move the "m=....."-estimates to the right of the bars (outside the bars)?

    Any help on this matter is highly appreciated.

    Kind regards,
    Johanne




  • #2
    Thanks for the data example. Use the -text()- option. In your graph, subtracting 9 to the y-axis coordinate looks like a good approximation to me.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float lpenr byte nivaa int(foreles sem gruppe innlev prosj felt lab annen_pr case sim praksis veil)
      1 . .  .  .  .  .  .  .  .  .  .  .  .
      2 1 5  4  1  1  1  1  1  1  1  1  1  2
      3 2 5  .  5  3  5  5  5  5  5  4  5  5
      4 1 4  4  .  4  .  .  .  .  .  .  .  4
      5 3 2  2  1  4  5  5  5  5  5  1  5  5
      6 2 2  4  2  1  2  1  1  4  3  3  3  2
      7 1 5  5  1  4  4  1  1  4 .a  1  1  2
      8 2 2  3  3  5  .  2  1  1  1  1  1  4
      9 3 2  5  2  4  3  1  1  1  1  3  1  2
     10 3 4  4 .a  3  1  1  1  1  1  1  1  4
     11 1 2 .a  2  5  4  1  1  1  1  1  1  5
     12 2 4  3  3  3  3  2  3  5  5  3  5  5
     13 2 4  3  4  3  1  1  1  3  1  3  1 .a
     14 2 5  2  2  3  3  1  1  3  1  1  1  1
     15 1 5  1  1  5  1  1  1  1  1  1  1  5
     16 3 2  2  2  2  5  3  3  4  3  4  4  4
     17 1 3  2  1  1  2  1  2  2  2  1  1  3
     18 2 4  2  1  1  2  1  1  1  3  3  1  2
     19 1 3  3  1  3  5  1  1  1  1  1  1  5
     20 2 2  5  2  4  1  4  1  1  1  1  1  5
     21 3 4  3  3  3  1  1  1  1  2  1  1  4
     22 1 3  5 .a  5  1  1  1  5  1  1  1  1
     23 1 5  5 .a  4  3  5  1  1  1  1  1  3
     24 2 4  1  1  1  1  1  3  1  1  1  1  1
     25 3 5  2  1  4  4  4  4  4  1  1  3  4
     26 3 5  2  2  2  3  1  1  5  3  2  5  5
     27 1 4  3  3  4  3  2  4  1  1  1  2  1
     28 3 4  2  1  4  1  4  1  3  1  1  1  4
     29 3 3  3  5  5  5  5  1 .a .a .a .a .a
     30 3 5  5 .a  5  1  3  1  3  3  1  1  5
     31 3 5  4  3  4  1  3  1  1  1  1  1  5
     32 2 5  1  1  3  2  1  1  3  1  1  1  1
     33 2 5  3  4  2  5  1  1  1  1  1  3  4
     34 2 4  3  3  2  3  2  3  4  2  2  4  2
     35 1 3  3  3  3  3  2  2  4  3  4  4  4
     36 2 4  1  1  3  1  .  3  1  1  1  1  1
     37 2 5  4  1  1  1  1  1  2  5  1  2  3
     38 1 5  .  .  .  .  .  .  .  .  .  .  .
     39 2 .  .  .  .  .  .  .  .  .  .  .  .
     40 3 5  3  3  3  1  2  1  2  1  1  1  2
     41 2 5  2  3  1  1  1  1  1  1  1  1  1
     42 2 3  3  3  3  3  1  1  3  3  3  1  2
     43 2 4  4  3  4  5  3  1  1  1  1  4  3
     44 2 4  4 .a  2  3  2  2  4  3  1  5  5
     45 1 3  4  1  4  1  1  1  1  1  1  1  3
     46 2 5  5  2  4  4  4  1  1  2  1  1  4
     47 2 4  3  1  3  3  2  1  4  2  1  4  3
     48 3 .  .  .  .  .  .  .  5  .  .  .  5
     49 3 5  1  1  1  1  1  1  1  1  1  1  1
     50 3 4  3  3  2  2 .a  2  3  3  1  1  3
     51 1 3  3  4  4  1  1  1  1  1  1  4  4
     52 2 4  2  2  2  2  2  4  2  2  2  4  3
     53 1 5  4  1  5  1  1  5  1  1  1  1  1
     54 1 5  1  2  4  4  1  1  1  1  1  1  4
     55 2 4  3  3  3  2  2  3  4  4  3  5  5
     56 3 5  5  3  3  1  2  1  4  1  1  1  1
     57 2 4  1  3  3  1  1  1  1  3  1  1  .
     58 1 3  .  3  3  3  .  .  3  .  .  .  .
     59 2 4  .  .  4  .  3  4  .  .  .  .  .
     60 3 4  1  4  4  4  1  1  4  4  1  4  4
     61 1 4  1  1  4  1  1  1  1  1  1  1  1
     62 3 5  3 .a  4  4  3 .a .a .a .a  2  4
     63 3 4  4  2  3  1  1  1  1  1  1  1  2
     64 2 1  4  1  2  1  1  1  1  5  1  1  3
     65 1 3  3  .  3  1  1  1  4  1  1  1  1
     66 2 4  3 .a .a  2 .a .a .a .a .a  4  3
     67 3 2  3  3  3  4  1  3  1  1  1  4  3
     68 1 4  4  3  3  3  3  2  3  3  2  5  5
     69 1 5  4  4  3  3  1  1  1  1  1  1  1
     70 2 4  4  3  5  4  1  1  3  3  1  1  5
     71 1 5  1  5  2  5  1  1  2  1  1  1  1
     72 3 5  4  3  3  3  3  4  2  3  1  1  3
     73 1 5  2  2  2  2  1  1  4  4  4  4  4
     74 2 3  1  1  4  .  3  1  3  3  1  5  5
     75 3 2  2  2  3  3  3  3  3  1  1  3  3
     76 1 4  3  2  3  1  1  1  1  1  1  1  3
     77 1 4  3  3  2  1  1  3  1  1  1  1  1
     78 3 4  4  2  3  4  3  3  4  4  4  4  4
     79 2 4  .  4  .  4  .  .  .  .  .  .  .
     80 3 4  1  2  4  4  1  1  1  1  1  1  1
     81 3 5  2  2  2  1  1  3  1  1  1  1  1
     82 2 5  1  1  1  1  1  1  1  3  1  1  1
     83 3 5  3  .  .  .  .  5  5  .  .  .  .
     84 1 5  4  1  3  3  1  1  1  1  1  1  1
     85 2 4  3  2  1  1  1  1  4  3  4  4  2
     86 3 5  4  3  5  1  1  4  1  1  1  1  2
     87 2 5  3 .a  3 .a .a .a .a .a .a .a .a
     88 3 5  1  3  3  1  2  3  3  3  1  1  1
     89 2 3  3  1  3  1  1  1  3  1  1  1  3
     90 2 5  1  1  1  5  5  1  1  1  1  1  5
     91 1 2  3  1  1  3  3  1  1  1  1  5  5
     92 1 3  2  .  5  5  5  1  5  1  1  1  1
     93 3 4  4  .  2  2  1  1  2  1  1  1  2
     94 1 5  3  .  .  .  .  .  .  .  .  .  3
     95 2 5  3  2  3  2  2  1  3  4  4  3  3
     96 1 4  4  2  4  3  4  1  2  1  1  1  3
     97 3 4  2  2  2  2  2  2  2  1  1  2  3
     98 3 3  3 .a  4  5  5 .a .a .a  1  4  1
     99 1 2  5  5  3  5  1  1  5  5  1  1  4
    100 3 5  4  4  4  1  1  4  2  2  2  3  3
    end
    label values nivaa nivaa
    label def nivaa 1 "Bachelorgradsnivå", modify
    label def nivaa 2 "Mastergradsnivå", modify
    label def nivaa 3 "Både bachelor- og mastergradsnivå", modify
    label values foreles foreles
    label def foreles 1 "I ingen / i svært liten grad", modify
    label def foreles 2 "I liten grad", modify
    label def foreles 3 "I noen grad", modify
    label def foreles 4 "I stor grad", modify
    label def foreles 5 "I svært stor grad", modify
    label values sem sem
    label def sem 1 "I ingen / i svært liten grad", modify
    label def sem 2 "I liten grad", modify
    label def sem 3 "I noen grad", modify
    label def sem 4 "I stor grad", modify
    label def sem 5 "I svært stor grad", modify
    label def sem .a "Vet ikke", modify
    label values gruppe gruppe
    label def gruppe 1 "I ingen / i svært liten grad", modify
    label def gruppe 2 "I liten grad", modify
    label def gruppe 3 "I noen grad", modify
    label def gruppe 4 "I stor grad", modify
    label def gruppe 5 "I svært stor grad", modify
    label def gruppe .a "Vet ikke", modify
    label values innlev innlev
    label def innlev 1 "I ingen / i svært liten grad", modify
    label def innlev 2 "I liten grad", modify
    label def innlev 3 "I noen grad", modify
    label def innlev 4 "I stor grad", modify
    label def innlev 5 "I svært stor grad", modify
    label def innlev .a "Vet ikke", modify
    label values prosj prosj
    label def prosj 1 "I ingen / i svært liten grad", modify
    label def prosj 2 "I liten grad", modify
    label def prosj 3 "I noen grad", modify
    label def prosj 4 "I stor grad", modify
    label def prosj 5 "I svært stor grad", modify
    label def prosj .a "Vet ikke", modify
    label values felt felt
    label def felt 1 "I ingen / i svært liten grad", modify
    label def felt 2 "I liten grad", modify
    label def felt 3 "I noen grad", modify
    label def felt 4 "I stor grad", modify
    label def felt 5 "I svært stor grad", modify
    label def felt .a "Vet ikke", modify
    label values lab lab
    label def lab 1 "I ingen / i svært liten grad", modify
    label def lab 2 "I liten grad", modify
    label def lab 3 "I noen grad", modify
    label def lab 4 "I stor grad", modify
    label def lab 5 "I svært stor grad", modify
    label def lab .a "Vet ikke", modify
    label values annen_pr annen_pr
    label def annen_pr 1 "I ingen / i svært liten grad", modify
    label def annen_pr 2 "I liten grad", modify
    label def annen_pr 3 "I noen grad", modify
    label def annen_pr 4 "I stor grad", modify
    label def annen_pr 5 "I svært stor grad", modify
    label def annen_pr .a "Vet ikke", modify
    label values case case
    label def case 1 "I ingen / i svært liten grad", modify
    label def case 2 "I liten grad", modify
    label def case 3 "I noen grad", modify
    label def case 4 "I stor grad", modify
    label def case 5 "I svært stor grad", modify
    label def case .a "Vet ikke", modify
    label values sim sim
    label def sim 1 "I ingen / i svært liten grad", modify
    label def sim 2 "I liten grad", modify
    label def sim 3 "I noen grad", modify
    label def sim 4 "I stor grad", modify
    label def sim .a "Vet ikke", modify
    label values praksis praksis
    label def praksis 1 "I ingen / i svært liten grad", modify
    label def praksis 2 "I liten grad", modify
    label def praksis 3 "I noen grad", modify
    label def praksis 4 "I stor grad", modify
    label def praksis 5 "I svært stor grad", modify
    label def praksis .a "Vet ikke", modify
    label values veil veil
    label def veil 1 "I ingen / i svært liten grad", modify
    label def veil 2 "I liten grad", modify
    label def veil 3 "I noen grad", modify
    label def veil 4 "I stor grad", modify
    label def veil 5 "I svært stor grad", modify
    label def veil .a "Vet ikke", modify
    
    preserve
    
    *rename in order to reshape
    rename foreles     b1_var1 
    rename sem         b1_var2 
    rename gruppe   b1_var3 
    rename innlev   b1_var4 
    rename prosj    b1_var5
    rename felt     b1_var6
    rename lab      b1_var7
    rename annen_pr b1_var8
    rename case     b1_var9
    rename sim      b1_var10
    rename praksis  b1_var11
    rename veil     b1_var12
    
    *reshape from wide to long
    keep lpenr b1* nivaa 
    reshape long b1_var, i(lpenr) j(battcount)
    rename b1_var b1vars
    
    *Save n for later
    levelsof battcount, local(bcn) 
    foreach n in `bcn' {
    su b1vars if b1vars != . & battcount == `n'
    local bcn_`n' `"`r(N)'"'
     }
    *Save mean for later 
    levelsof battcount, local(bcm) 
    foreach m in `bcm' {
    su b1vars if b1vars != . & battcount == `m'
    local bcm_`m' `"`: di %2.1f r(mean)' "'
     }
     
    *Relabel variable(s) and draw on information stored in the local macros
    la def battcount 1"Forelesning (n=`bcn_1')" 2"Seminar (n=`bcn_2')" 3"Gruppe (n=`bcn_3', m=`bcm_3')" 4"Innlevering (n=`bcn_4', m=`bcm_4')" 5"Prosjekt (n=`bcn_5', m=`bcm_5')" 6"Feltarbeid (n=`bcn_6', m=`bcm_6')" 7"Lab (n=`bcn_7', m=`bcm_7')" 8"An. prakt.øv. (n=`bcn_8', m=`bcm_8')" 9"Case (n=`bcn_9', m=`bcm_9')" 10"Simulering (n=`bcn_10', m=`bcm_10')" 11"Praksis (n=`bcn_11', m=`bcm_11')" 12"Veiledning (n=`bcn_12', m=`bcm_12')"
    la val battcount battcount
    
    
    *catplot with percent within bars, n to the left of the bar and (ideally) mean to the right of the bar (outside the bar)
    catplot b1vars, over(battcount) percent(battcount) asyvars stack blabel(bar, pos(center) ///
    format(%2.1f) size(small)) intensity(65) bargap(20) ylabel(`label', nogrid labsize(small)) ///
    ytitle(" ", size(small))  scheme(s1color) ysize(3.5)  plotregion(lcolor(none) margin(small)) ///
    l1title(" ") title("Some title" " ", size(medlarge)) graphregion(margin(medlarge)) ///
    legend(position(12) row(1) keygap(0.8) symxsize(4) size(*0.9) region(lcolor(white)) ///
    margin(tiny) colgap(5)) scale(0.8) note("Note: An.prakt.øv = Annen praktisk øving", size(medsmall) span) ///
     ysc(r(0 110)) text(106 97 "m=`bcm_1'")  text(106 88 "m=`bcm_2'") text(106 79 "m=`bcm_3'")
    
    restore
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	97.0 KB
ID:	1633721

    Comment


    • #3
      Dear Johanne,

      Nick Cox recently contributed floatplot that enables you to analyse your data with much more flexibility to sort by a selected response option.
      The reference for it is here, but note that this module should be installed from within Stata by typing:
      Code:
      ssc install floatplot
      h floatplot
      For some example visit this page, but you should also first work with the examples presented in the help file.
      http://publicationslist.org/eric.melse

      Comment


      • #4
        ericmelse is an even bigger fan of floatplot (SSC) than I am, but he is not the only person to have singled them out. I suspect that some people like the design as a twist on stacked bars, so familiar, but a twist, so a little intriguing.

        Meanwhile catplot is an old command of mine written given my surprise in 2003 that the new graphics in Stata 8 didn't allow you to go directly to bar or dot charts showing frequencies, fractions or percents in each cell of a table for one, two or three categorical variables. (Stata caught up on that in 2014 in Stata 13: see help whatsnew13.) It's a personal as well as general principle that being old(er) doesn't rule out being useful, but I tend to recommend tabplot from the Stata Journal particularly for ordered variables, where I think the stacked design is oversold. In particular,

        ! There can be a problem with unusual or absent cells which necessarily are represented by bars that are short or even of zero length or heiight.

        ! The wish to show percents as text is fine by me except that with the stacked design obliges overprinting of text.

        ! The signal that category percents add to 100 is reassuring but not informative about a particular dataset.

        ! It can be hard work to decode patterns across categories as well as within them.

        Regardless of the design adopted, I recommend

        1. Trimming marginal text labels if possible, although the goal of being concise without being cryptic can be elusive.

        2. For an ordered (ordinal, graded) response I think you need a colour scheme that corresponds. A sequence from (strong) red to (strong) blue is one possibility, despite a little awkwardness that making a middle colour white is not a good idea for other reasons.

        3. Putting algebraic notation in italic will be satisfying to anyone at all mathematical and is (I guess, or I hope) unlikely to irritate or mystify others.

        As this example has provoked a lot of interest, I've slimmed down the code in the hope that the result is easier to follow.


        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input float lpenr byte nivaa int(foreles sem gruppe innlev prosj felt lab annen_pr case sim praksis veil)
          1 . .  .  .  .  .  .  .  .  .  .  .  .
          2 1 5  4  1  1  1  1  1  1  1  1  1  2
          3 2 5  .  5  3  5  5  5  5  5  4  5  5
          4 1 4  4  .  4  .  .  .  .  .  .  .  4
          5 3 2  2  1  4  5  5  5  5  5  1  5  5
          6 2 2  4  2  1  2  1  1  4  3  3  3  2
          7 1 5  5  1  4  4  1  1  4 .a  1  1  2
          8 2 2  3  3  5  .  2  1  1  1  1  1  4
          9 3 2  5  2  4  3  1  1  1  1  3  1  2
         10 3 4  4 .a  3  1  1  1  1  1  1  1  4
         11 1 2 .a  2  5  4  1  1  1  1  1  1  5
         12 2 4  3  3  3  3  2  3  5  5  3  5  5
         13 2 4  3  4  3  1  1  1  3  1  3  1 .a
         14 2 5  2  2  3  3  1  1  3  1  1  1  1
         15 1 5  1  1  5  1  1  1  1  1  1  1  5
         16 3 2  2  2  2  5  3  3  4  3  4  4  4
         17 1 3  2  1  1  2  1  2  2  2  1  1  3
         18 2 4  2  1  1  2  1  1  1  3  3  1  2
         19 1 3  3  1  3  5  1  1  1  1  1  1  5
         20 2 2  5  2  4  1  4  1  1  1  1  1  5
         21 3 4  3  3  3  1  1  1  1  2  1  1  4
         22 1 3  5 .a  5  1  1  1  5  1  1  1  1
         23 1 5  5 .a  4  3  5  1  1  1  1  1  3
         24 2 4  1  1  1  1  1  3  1  1  1  1  1
         25 3 5  2  1  4  4  4  4  4  1  1  3  4
         26 3 5  2  2  2  3  1  1  5  3  2  5  5
         27 1 4  3  3  4  3  2  4  1  1  1  2  1
         28 3 4  2  1  4  1  4  1  3  1  1  1  4
         29 3 3  3  5  5  5  5  1 .a .a .a .a .a
         30 3 5  5 .a  5  1  3  1  3  3  1  1  5
         31 3 5  4  3  4  1  3  1  1  1  1  1  5
         32 2 5  1  1  3  2  1  1  3  1  1  1  1
         33 2 5  3  4  2  5  1  1  1  1  1  3  4
         34 2 4  3  3  2  3  2  3  4  2  2  4  2
         35 1 3  3  3  3  3  2  2  4  3  4  4  4
         36 2 4  1  1  3  1  .  3  1  1  1  1  1
         37 2 5  4  1  1  1  1  1  2  5  1  2  3
         38 1 5  .  .  .  .  .  .  .  .  .  .  .
         39 2 .  .  .  .  .  .  .  .  .  .  .  .
         40 3 5  3  3  3  1  2  1  2  1  1  1  2
         41 2 5  2  3  1  1  1  1  1  1  1  1  1
         42 2 3  3  3  3  3  1  1  3  3  3  1  2
         43 2 4  4  3  4  5  3  1  1  1  1  4  3
         44 2 4  4 .a  2  3  2  2  4  3  1  5  5
         45 1 3  4  1  4  1  1  1  1  1  1  1  3
         46 2 5  5  2  4  4  4  1  1  2  1  1  4
         47 2 4  3  1  3  3  2  1  4  2  1  4  3
         48 3 .  .  .  .  .  .  .  5  .  .  .  5
         49 3 5  1  1  1  1  1  1  1  1  1  1  1
         50 3 4  3  3  2  2 .a  2  3  3  1  1  3
         51 1 3  3  4  4  1  1  1  1  1  1  4  4
         52 2 4  2  2  2  2  2  4  2  2  2  4  3
         53 1 5  4  1  5  1  1  5  1  1  1  1  1
         54 1 5  1  2  4  4  1  1  1  1  1  1  4
         55 2 4  3  3  3  2  2  3  4  4  3  5  5
         56 3 5  5  3  3  1  2  1  4  1  1  1  1
         57 2 4  1  3  3  1  1  1  1  3  1  1  .
         58 1 3  .  3  3  3  .  .  3  .  .  .  .
         59 2 4  .  .  4  .  3  4  .  .  .  .  .
         60 3 4  1  4  4  4  1  1  4  4  1  4  4
         61 1 4  1  1  4  1  1  1  1  1  1  1  1
         62 3 5  3 .a  4  4  3 .a .a .a .a  2  4
         63 3 4  4  2  3  1  1  1  1  1  1  1  2
         64 2 1  4  1  2  1  1  1  1  5  1  1  3
         65 1 3  3  .  3  1  1  1  4  1  1  1  1
         66 2 4  3 .a .a  2 .a .a .a .a .a  4  3
         67 3 2  3  3  3  4  1  3  1  1  1  4  3
         68 1 4  4  3  3  3  3  2  3  3  2  5  5
         69 1 5  4  4  3  3  1  1  1  1  1  1  1
         70 2 4  4  3  5  4  1  1  3  3  1  1  5
         71 1 5  1  5  2  5  1  1  2  1  1  1  1
         72 3 5  4  3  3  3  3  4  2  3  1  1  3
         73 1 5  2  2  2  2  1  1  4  4  4  4  4
         74 2 3  1  1  4  .  3  1  3  3  1  5  5
         75 3 2  2  2  3  3  3  3  3  1  1  3  3
         76 1 4  3  2  3  1  1  1  1  1  1  1  3
         77 1 4  3  3  2  1  1  3  1  1  1  1  1
         78 3 4  4  2  3  4  3  3  4  4  4  4  4
         79 2 4  .  4  .  4  .  .  .  .  .  .  .
         80 3 4  1  2  4  4  1  1  1  1  1  1  1
         81 3 5  2  2  2  1  1  3  1  1  1  1  1
         82 2 5  1  1  1  1  1  1  1  3  1  1  1
         83 3 5  3  .  .  .  .  5  5  .  .  .  .
         84 1 5  4  1  3  3  1  1  1  1  1  1  1
         85 2 4  3  2  1  1  1  1  4  3  4  4  2
         86 3 5  4  3  5  1  1  4  1  1  1  1  2
         87 2 5  3 .a  3 .a .a .a .a .a .a .a .a
         88 3 5  1  3  3  1  2  3  3  3  1  1  1
         89 2 3  3  1  3  1  1  1  3  1  1  1  3
         90 2 5  1  1  1  5  5  1  1  1  1  1  5
         91 1 2  3  1  1  3  3  1  1  1  1  5  5
         92 1 3  2  .  5  5  5  1  5  1  1  1  1
         93 3 4  4  .  2  2  1  1  2  1  1  1  2
         94 1 5  3  .  .  .  .  .  .  .  .  .  3
         95 2 5  3  2  3  2  2  1  3  4  4  3  3
         96 1 4  4  2  4  3  4  1  2  1  1  1  3
         97 3 4  2  2  2  2  2  2  2  1  1  2  3
         98 3 3  3 .a  4  5  5 .a .a .a  1  4  1
         99 1 2  5  5  3  5  1  1  5  5  1  1  4
        100 3 5  4  4  4  1  1  4  2  2  2  3  3
        end
        
        label values nivaa nivaa
        label def nivaa 1 "Bachelorgradsnivå", modify
        label def nivaa 2 "Mastergradsnivå", modify
        label def nivaa 3 "Både bachelor- og mastergradsnivå", modify
        
        local myvars foreles sem gruppe innlev prosj felt lab annen_pr case sim praksis veil
        foreach v in `myvars' { 
            label val `v' answer 
        } 
        label def answer 1 "ingen / svært liten grad", modify
        label def answer 2 "liten grad", modify
        label def answer 3 "noen grad", modify
        label def answer 4 "stor grad", modify
        label def answer 5 "svært stor grad", modify
        label def answer .a "Vet ikke", modify
        
        preserve
        
        * rename in order to reshape
        rename (`myvars') b1vars#, addnumber 
        
        * reshape from wide to long
        reshape long b1vars, i(lpenr) j(battcount)
        
        * save n and mean for later
        levelsof battcount, local(bcn) 
        foreach n in `bcn' {
            su b1vars if b1vars != . & battcount == `n', meanonly 
            local bcn_`n' = r(N) 
            local bcm_`n': di %2.1f r(mean) 
        }
         
        * relabel variable(s) and draw on information stored in the local macros
        la def battcount 1"Forelesning ({it:n=}`bcn_1', {it:m=}`bcm_1')" 2"Seminar ({it:n=}`bcn_2', {it:m=}`bcm_2')" 3"Gruppe ({it:n=}`bcn_3', {it:m=}`bcm_3')" 4"Innlevering ({it:n=}`bcn_4', {it:m=}`bcm_4')" 5"Prosjekt ({it:n=}`bcn_5', {it:m=}`bcm_5')" 6"Feltarbeid ({it:n=}`bcn_6', {it:m=}`bcm_6')" 7"Lab ({it:n=}`bcn_7', {it:m=}`bcm_7')" 8"An. prakt. øv. ({it:n=}`bcn_8', {it:m=}`bcm_8')" 9"Case ({it:n=}`bcn_9', {it:m=}`bcm_9')" 10"Simulering ({it:n=}`bcn_10', {it:m=}`bcm_10')" 11"Praksis ({it:n=}`bcn_11', {it:m=}`bcm_11')" 12"Veiledning ({it:n=}`bcn_12', {it:m=}`bcm_12')"
        la val battcount battcount
        
        
        label def answer 1 `" "ingen /" "svært liten grad" "', modify
        label def answer 5 `" "svært" "stor grad" "', modify 
        
        la def battcount 8"Annen praktisk øving ({it:n=}`bcn_8', {it:m=}`bcm_8')", modify
        
        tabplot  battcount b1vars, percent(battcount)  showval(offset(0.3) mlabsize(vsmall)) ytitle("") xtitle("")  title("Some title", size(medlarge))  yla(, labsize(small)) subtitle("%", size(small)) xla(, labsize(small)) separate(b1vars) bar1(blcolor(blue) bfcolor(blue)) bar2(blcolor(blue) bfcolor(blue*0.6)) bar3(blcolor(blue) bfcolor(blue*0.2)) bar4(blcolor(red) bfcolor(red*0.2)) bar5(blcolor(red) bfcolor(red*0.6)) name(G1, replace) horizontal xsc(r(0.8 .)) ysize(4) height(0.75)
        
        restore
        Click image for larger version

Name:	storgrad.png
Views:	1
Size:	54.7 KB
ID:	1633789


        I don't know why the leftmost and rightmost x axis labels have been shifted down.

        For another possibility, is there a rationale for the ordering of the categories on the y axis?

        On tabplot: if you seek more detail,see https://www.stata-journal.com/articl...article=gr0066 (although if interested in installing search for the latest update) or https://www.statalist.org/forums/for...updated-on-ssc for a quicker overview.

        Comment


        • #5
          By the way I don't know Norwegian (?) (or is it Danish (?) or Swedish (?)), so sorry if cutting "I " or "I " makes the wording ungrammatical or unidiomatic.

          Comment


          • #6
            I guess that these are survey questions in Norwegian where the question is "How much do you agree with statement X?". The difference can be seen as "to a large extent" and "large extent" corresponding to "i stor grad" and "stor grad".

            Comment


            • #7
              Hi everyone!

              Thank you all so much for helping me out! This forum is beyond helpful! I am so glad I asked about this; not only because Andrew Musau offered a solution for my problem and suggested an option that I didn't know about (thank you so much Andrew Musau, I have now successfully implemented your solution for all fourteen graphs I had to make!)....but also because I get to learn so much more.

              For example, the -text()- option makes me curious about the coordinate system of Stata graphs. Is there a standard "plot-template"/"graph-standard" of sorts? I mean, in order to use the -text()- option in catplot I had to fiddle around with the numbers/coordinates and then run the code to see if the result ended up okay visually. Is there somewhere I can familiarize myself with how Stata graphs are built, not just in terms of graph region, plot region, margins etc, but down to pixel/coordinate level? I felt like I was brute forcing where the text should be presented.

              I have read about tabplot, but never really used it, so the code you offer, Nick Cox, is really helpful and thought provoking. What I mean by that is: I regularly write reports (and yes, those are usually reports based on "survey questions in Norwegian", as Andrew Musau correctly identified) where data needs to be visualized either/both as mean values and as percentages, but somewhere along the way I think I married catplot and hbar. Clearly, I need to not only read about other types of plots, but figure out where those plots can be particularly useful in our reports.

              Not really important, but anyway: The variables I currently work with are ordinal level and on a 5-point Likert scale. However, the scale isn't "normative", i.e. the value 1 is not necessarily any worse (or better) than the value 5. Without going into lengthy explanations for why this is, the implication ends up being that the color of bars cannot be green/yellow/orange/red, but rather should have "neutral" colors that does not imply something is better or worse. I have created a scheme file that draws on the colors of the company I work in (light shades of purple, turquoise, brown and sand/white), but for the example given here I decided on using a standard Stata scheme.

              Again, thank you all so much for helping me with my problem, and for offering other solutions that could even be better. I have to say i love catplot, but after writing this post I will study up on floatplot (thank you for suggesting it to me, ericmelse) and tabplot, so that I can have those plots in mind for future data visualizations.

              Comment


              • #8
                s there a standard "plot-template"/"graph-standard" of sorts? I mean, in order to use the -text()- option in catplot I had to fiddle around with the numbers/coordinates and then run the code to see if the result ended up okay visually. Is there somewhere I can familiarize myself with how Stata graphs are built, not just in terms of graph region, plot region, margins etc, but down to pixel/coordinate level? I felt like I was brute forcing where the text should be presented.
                catplot is a wrapper for graph bar, and the source code for graph is not public. So the only way you can familiarize yourself with its inner-workings of the command is by reading the documentation and/or experimenting. I suspect that there is a way to find out how to refer to the mid-point of the bars, possibly by looking at where Stata prints the bar labels through the graph editor. But graph bar is not very flexible, so if you want more flexibility, you should switch to twoway bar. The labels can be included as marker labels of an invisible scatter plot. If I find the time, I will illustrate this using your data.

                Comment


                • #9
                  Andrew Musau, thank you for sharing your knowledge with me. I will read the documentation for graph more thoroughly, but twoway bar sounds like it will be a better option for what I am currently trying to do. If you find the time to illustrate twoway bar using my data (or other data), I will greatly appreciate it and will make sure to read it :-)

                  Comment


                  • #10
                    Regarding #8, there is a way of determining the center of each bar (y-axis coordinate).

                    Start Graph Editor -> Click on the LHS labels -> Show grid -> More -> (Bottom) Edit or add individual ticks and labels

                    The exact y-axis position is shown on the LHS before the labels. There does not seem to be an automatic way to retrieve these values, but at least you can get accuracy with some manual work. The following should not be too hard to follow, but the process involves manually calculating the percentages and the center positions of the bars. All the same, the code pretty much replicates the graph produced by catplot. If you face difficulties following, run the code in bits. Otherwise post back if you are completely stumped.


                    Code:
                    * Example generated by -dataex-. To install: ssc install dataex
                    clear
                    input float lpenr byte nivaa int(foreles sem gruppe innlev prosj felt lab annen_pr case sim praksis veil)
                      1 . .  .  .  .  .  .  .  .  .  .  .  .
                      2 1 5  4  1  1  1  1  1  1  1  1  1  2
                      3 2 5  .  5  3  5  5  5  5  5  4  5  5
                      4 1 4  4  .  4  .  .  .  .  .  .  .  4
                      5 3 2  2  1  4  5  5  5  5  5  1  5  5
                      6 2 2  4  2  1  2  1  1  4  3  3  3  2
                      7 1 5  5  1  4  4  1  1  4 .a  1  1  2
                      8 2 2  3  3  5  .  2  1  1  1  1  1  4
                      9 3 2  5  2  4  3  1  1  1  1  3  1  2
                     10 3 4  4 .a  3  1  1  1  1  1  1  1  4
                     11 1 2 .a  2  5  4  1  1  1  1  1  1  5
                     12 2 4  3  3  3  3  2  3  5  5  3  5  5
                     13 2 4  3  4  3  1  1  1  3  1  3  1 .a
                     14 2 5  2  2  3  3  1  1  3  1  1  1  1
                     15 1 5  1  1  5  1  1  1  1  1  1  1  5
                     16 3 2  2  2  2  5  3  3  4  3  4  4  4
                     17 1 3  2  1  1  2  1  2  2  2  1  1  3
                     18 2 4  2  1  1  2  1  1  1  3  3  1  2
                     19 1 3  3  1  3  5  1  1  1  1  1  1  5
                     20 2 2  5  2  4  1  4  1  1  1  1  1  5
                     21 3 4  3  3  3  1  1  1  1  2  1  1  4
                     22 1 3  5 .a  5  1  1  1  5  1  1  1  1
                     23 1 5  5 .a  4  3  5  1  1  1  1  1  3
                     24 2 4  1  1  1  1  1  3  1  1  1  1  1
                     25 3 5  2  1  4  4  4  4  4  1  1  3  4
                     26 3 5  2  2  2  3  1  1  5  3  2  5  5
                     27 1 4  3  3  4  3  2  4  1  1  1  2  1
                     28 3 4  2  1  4  1  4  1  3  1  1  1  4
                     29 3 3  3  5  5  5  5  1 .a .a .a .a .a
                     30 3 5  5 .a  5  1  3  1  3  3  1  1  5
                     31 3 5  4  3  4  1  3  1  1  1  1  1  5
                     32 2 5  1  1  3  2  1  1  3  1  1  1  1
                     33 2 5  3  4  2  5  1  1  1  1  1  3  4
                     34 2 4  3  3  2  3  2  3  4  2  2  4  2
                     35 1 3  3  3  3  3  2  2  4  3  4  4  4
                     36 2 4  1  1  3  1  .  3  1  1  1  1  1
                     37 2 5  4  1  1  1  1  1  2  5  1  2  3
                     38 1 5  .  .  .  .  .  .  .  .  .  .  .
                     39 2 .  .  .  .  .  .  .  .  .  .  .  .
                     40 3 5  3  3  3  1  2  1  2  1  1  1  2
                     41 2 5  2  3  1  1  1  1  1  1  1  1  1
                     42 2 3  3  3  3  3  1  1  3  3  3  1  2
                     43 2 4  4  3  4  5  3  1  1  1  1  4  3
                     44 2 4  4 .a  2  3  2  2  4  3  1  5  5
                     45 1 3  4  1  4  1  1  1  1  1  1  1  3
                     46 2 5  5  2  4  4  4  1  1  2  1  1  4
                     47 2 4  3  1  3  3  2  1  4  2  1  4  3
                     48 3 .  .  .  .  .  .  .  5  .  .  .  5
                     49 3 5  1  1  1  1  1  1  1  1  1  1  1
                     50 3 4  3  3  2  2 .a  2  3  3  1  1  3
                     51 1 3  3  4  4  1  1  1  1  1  1  4  4
                     52 2 4  2  2  2  2  2  4  2  2  2  4  3
                     53 1 5  4  1  5  1  1  5  1  1  1  1  1
                     54 1 5  1  2  4  4  1  1  1  1  1  1  4
                     55 2 4  3  3  3  2  2  3  4  4  3  5  5
                     56 3 5  5  3  3  1  2  1  4  1  1  1  1
                     57 2 4  1  3  3  1  1  1  1  3  1  1  .
                     58 1 3  .  3  3  3  .  .  3  .  .  .  .
                     59 2 4  .  .  4  .  3  4  .  .  .  .  .
                     60 3 4  1  4  4  4  1  1  4  4  1  4  4
                     61 1 4  1  1  4  1  1  1  1  1  1  1  1
                     62 3 5  3 .a  4  4  3 .a .a .a .a  2  4
                     63 3 4  4  2  3  1  1  1  1  1  1  1  2
                     64 2 1  4  1  2  1  1  1  1  5  1  1  3
                     65 1 3  3  .  3  1  1  1  4  1  1  1  1
                     66 2 4  3 .a .a  2 .a .a .a .a .a  4  3
                     67 3 2  3  3  3  4  1  3  1  1  1  4  3
                     68 1 4  4  3  3  3  3  2  3  3  2  5  5
                     69 1 5  4  4  3  3  1  1  1  1  1  1  1
                     70 2 4  4  3  5  4  1  1  3  3  1  1  5
                     71 1 5  1  5  2  5  1  1  2  1  1  1  1
                     72 3 5  4  3  3  3  3  4  2  3  1  1  3
                     73 1 5  2  2  2  2  1  1  4  4  4  4  4
                     74 2 3  1  1  4  .  3  1  3  3  1  5  5
                     75 3 2  2  2  3  3  3  3  3  1  1  3  3
                     76 1 4  3  2  3  1  1  1  1  1  1  1  3
                     77 1 4  3  3  2  1  1  3  1  1  1  1  1
                     78 3 4  4  2  3  4  3  3  4  4  4  4  4
                     79 2 4  .  4  .  4  .  .  .  .  .  .  .
                     80 3 4  1  2  4  4  1  1  1  1  1  1  1
                     81 3 5  2  2  2  1  1  3  1  1  1  1  1
                     82 2 5  1  1  1  1  1  1  1  3  1  1  1
                     83 3 5  3  .  .  .  .  5  5  .  .  .  .
                     84 1 5  4  1  3  3  1  1  1  1  1  1  1
                     85 2 4  3  2  1  1  1  1  4  3  4  4  2
                     86 3 5  4  3  5  1  1  4  1  1  1  1  2
                     87 2 5  3 .a  3 .a .a .a .a .a .a .a .a
                     88 3 5  1  3  3  1  2  3  3  3  1  1  1
                     89 2 3  3  1  3  1  1  1  3  1  1  1  3
                     90 2 5  1  1  1  5  5  1  1  1  1  1  5
                     91 1 2  3  1  1  3  3  1  1  1  1  5  5
                     92 1 3  2  .  5  5  5  1  5  1  1  1  1
                     93 3 4  4  .  2  2  1  1  2  1  1  1  2
                     94 1 5  3  .  .  .  .  .  .  .  .  .  3
                     95 2 5  3  2  3  2  2  1  3  4  4  3  3
                     96 1 4  4  2  4  3  4  1  2  1  1  1  3
                     97 3 4  2  2  2  2  2  2  2  1  1  2  3
                     98 3 3  3 .a  4  5  5 .a .a .a  1  4  1
                     99 1 2  5  5  3  5  1  1  5  5  1  1  4
                    100 3 5  4  4  4  1  1  4  2  2  2  3  3
                    end
                    label values nivaa nivaa
                    label def nivaa 1 "Bachelorgradsnivå", modify
                    label def nivaa 2 "Mastergradsnivå", modify
                    label def nivaa 3 "Både bachelor- og mastergradsnivå", modify
                    label values foreles foreles
                    label def foreles 1 "I ingen / i svært liten grad", modify
                    label def foreles 2 "I liten grad", modify
                    label def foreles 3 "I noen grad", modify
                    label def foreles 4 "I stor grad", modify
                    label def foreles 5 "I svært stor grad", modify
                    label values sem sem
                    label def sem 1 "I ingen / i svært liten grad", modify
                    label def sem 2 "I liten grad", modify
                    label def sem 3 "I noen grad", modify
                    label def sem 4 "I stor grad", modify
                    label def sem 5 "I svært stor grad", modify
                    label def sem .a "Vet ikke", modify
                    label values gruppe gruppe
                    label def gruppe 1 "I ingen / i svært liten grad", modify
                    label def gruppe 2 "I liten grad", modify
                    label def gruppe 3 "I noen grad", modify
                    label def gruppe 4 "I stor grad", modify
                    label def gruppe 5 "I svært stor grad", modify
                    label def gruppe .a "Vet ikke", modify
                    label values innlev innlev
                    label def innlev 1 "I ingen / i svært liten grad", modify
                    label def innlev 2 "I liten grad", modify
                    label def innlev 3 "I noen grad", modify
                    label def innlev 4 "I stor grad", modify
                    label def innlev 5 "I svært stor grad", modify
                    label def innlev .a "Vet ikke", modify
                    label values prosj prosj
                    label def prosj 1 "I ingen / i svært liten grad", modify
                    label def prosj 2 "I liten grad", modify
                    label def prosj 3 "I noen grad", modify
                    label def prosj 4 "I stor grad", modify
                    label def prosj 5 "I svært stor grad", modify
                    label def prosj .a "Vet ikke", modify
                    label values felt felt
                    label def felt 1 "I ingen / i svært liten grad", modify
                    label def felt 2 "I liten grad", modify
                    label def felt 3 "I noen grad", modify
                    label def felt 4 "I stor grad", modify
                    label def felt 5 "I svært stor grad", modify
                    label def felt .a "Vet ikke", modify
                    label values lab lab
                    label def lab 1 "I ingen / i svært liten grad", modify
                    label def lab 2 "I liten grad", modify
                    label def lab 3 "I noen grad", modify
                    label def lab 4 "I stor grad", modify
                    label def lab 5 "I svært stor grad", modify
                    label def lab .a "Vet ikke", modify
                    label values annen_pr annen_pr
                    label def annen_pr 1 "I ingen / i svært liten grad", modify
                    label def annen_pr 2 "I liten grad", modify
                    label def annen_pr 3 "I noen grad", modify
                    label def annen_pr 4 "I stor grad", modify
                    label def annen_pr 5 "I svært stor grad", modify
                    label def annen_pr .a "Vet ikke", modify
                    label values case case
                    label def case 1 "I ingen / i svært liten grad", modify
                    label def case 2 "I liten grad", modify
                    label def case 3 "I noen grad", modify
                    label def case 4 "I stor grad", modify
                    label def case 5 "I svært stor grad", modify
                    label def case .a "Vet ikke", modify
                    label values sim sim
                    label def sim 1 "I ingen / i svært liten grad", modify
                    label def sim 2 "I liten grad", modify
                    label def sim 3 "I noen grad", modify
                    label def sim 4 "I stor grad", modify
                    label def sim .a "Vet ikke", modify
                    label values praksis praksis
                    label def praksis 1 "I ingen / i svært liten grad", modify
                    label def praksis 2 "I liten grad", modify
                    label def praksis 3 "I noen grad", modify
                    label def praksis 4 "I stor grad", modify
                    label def praksis 5 "I svært stor grad", modify
                    label def praksis .a "Vet ikke", modify
                    label values veil veil
                    label def veil 1 "I ingen / i svært liten grad", modify
                    label def veil 2 "I liten grad", modify
                    label def veil 3 "I noen grad", modify
                    label def veil 4 "I stor grad", modify
                    label def veil 5 "I svært stor grad", modify
                    label def veil .a "Vet ikke", modify
                    
                    *preserve
                    
                    *rename in order to reshape
                    rename foreles     b1_var1
                    rename sem         b1_var2
                    rename gruppe   b1_var3
                    rename innlev   b1_var4
                    rename prosj    b1_var5
                    rename felt     b1_var6
                    rename lab      b1_var7
                    rename annen_pr b1_var8
                    rename case     b1_var9
                    rename sim      b1_var10
                    rename praksis  b1_var11
                    rename veil     b1_var12
                    
                    *reshape from wide to long
                    keep lpenr b1* nivaa
                    reshape long b1_var, i(lpenr) j(battcount)
                    rename b1_var b1vars
                    
                    *Save n for later
                    levelsof battcount, local(bcn)
                    foreach n in `bcn' {
                    su b1vars if b1vars != . & battcount == `n'
                    local bcn_`n' `"`r(N)'"'
                     }
                    *Save mean for later
                    levelsof battcount, local(bcm)
                    foreach m in `bcm' {
                    su b1vars if b1vars != . & battcount == `m'
                    local bcm_`m' `"`: di %2.1f r(mean)' "'
                     }
                     
                    *Relabel variable(s) and draw on information stored in the local macros
                    la def battcount 1"Forelesning ({it:n}=`bcn_1')" 2"Seminar ({it:n}=`bcn_2')" 3"Gruppe ({it:n}=`bcn_3')" 4"Innlevering ({it:n}=`bcn_4')" 5"Prosjekt ({it:n}=`bcn_5')" 6"Feltarbeid ({it:n}=`bcn_6')" 7"Lab ({it:n}=`bcn_7')" 8"An. prakt.øv. ({it:n}=`bcn_8')" 9"Case ({it:n}=`bcn_9')" 10"Simulering ({it:n}=`bcn_10')" 11"Praksis ({it:n}=`bcn_11')" 12"Veiledning ({it:n}=`bcn_12')"
                    la val battcount battcount
                    
                    gen battcount2=battcount
                    la def battcount2 1 "{it:m}=`bcm_1'" 2"{it:m}=`bcm_2'" 3 "{it:m}=`bcm_3'" 4"{it:m}=`bcm_4'" 5"{it:m}=`bcm_5'" 6"{it:m}=`bcm_6'" 7"{it:m}=`bcm_7'" 8"{it:m}=`bcm_8'" 9"{it:m}=`bcm_9'" 10"{it:m}=`bcm_10'" 11"{it:m}=`bcm_11'" 12"{it:m}=`bcm_12'"
                    la val battcount2 battcount2
                    
                    
                    drop if missing(b1vars)
                    bys battcount: gen percent= _N
                    bys battcount b1vars: replace percent= (_N/ percent)*100
                    contract battcount battcount2 b1vars percent, freq(cumpct)
                    bys battcount: replace cumpct= sum(percent)
                    separate cumpct, by(b1vars) veryshortlabel
                    bys battcount (cumpct): gen center= cond(_n==1,cumpct/2, cumpct[_n-1]+((cumpct-cumpct[_n-1])/2))
                    format percent %2.1f
                    twoway (bar cumpct5 cumpct4 cumpct3 cumpct2 cumpct1 battcount, horiz xsc(r(-1,101)) ///
                    ylab(1/12, val angle(horiz) noticks) ytitle("") xtitle("") plotregion(margin(zero)) barw(0.5 0.5 0.5 0.5 0.5) ///
                    legend(pos(12) order(1 2 3 4 5) nobox region(lstyle(none)) rows(1) ring(1)  si(7.7pt) keygap(*0) colgap(*6) ///
                    bexpand bplacement(12) )) (scatter battcount center, msy(none) mlab(percent) mlabsize(small) mlabc(black) ///
                    mlabpos(0) ylab(,labsize(medsmall)) xlab(,labsize(medsmall)))  (bar cumpct battcount2, horiz color(none) ///
                    barw(0.5) yaxis(2) ylab(1/12, val axis(2) angle(horiz) noticks) ylab(,labsize(medsmall) axis(2)) ///
                    ytitle("", axis(2))  note("Note: An.prakt.øv = Annen praktisk øving", size(medsmall) span) scale(0.8))
                    Res.:
                    Click image for larger version

Name:	Graph.png
Views:	1
Size:	127.8 KB
ID:	1634650

                    Last edited by Andrew Musau; 03 Nov 2021, 15:56.

                    Comment

                    Working...
                    X