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:

Array



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