Hello!
I'm trying to make a percentage barplot with two different variables that have the same values showcasing the difference between the two variables in each category. I thought the easiest way to go about this was calculating the percentage share of each value and then saving it as an estimate to use it with the coefplot-function. However, when I run the following code I get the weirdest output... Can anyone please explain what is going on? I am very open to suggestions if you think I can go about this in an easier way. I have posted my code, the barplot from my output, and a picture of reference for how I would like the plot to look.
The code:
*Udregner procent på q5_4 og gemmer som estimat
count if q5_4==1
display r(N)*100/1199
estimates store q5_4_1
count if q5_4==2
display r(N)*100/1199
estimates store q5_4_2
count if q5_4==3
display r(N)*100/1199
estimates store q5_4_3
count if q5_4==4
display r(N)*100/1199
estimates store q5_4_4
count if q5_4==5
display r(N)*100/1199
estimates store q5_4_5
*Udregner procent på Q13_4 og gemmer som estimat
count if q13_4==1
display r(N)*100/1199
estimates store q13_4_1
count if q13_4==2
display r(N)*100/1199
estimates store q13_4_2
count if q13_4==3
display r(N)*100/1199
estimates store q13_4_3
count if q13_4==4
display r(N)*100/1199
estimates store q13_4_4
count if q13_4==5
display r(N)*100/1199
estimates store q13_4_5
*Sætter nu vores estimater ind i en barplot-figur med coefficientplot-funktionen
coefplot (q5_4_1, bcolor("232 104 119") offset(-0.2)) (q13_4_1, bcolor("255 0 40") offset(0.2)) (q5_4_2, bcolor("232 104 119") offset(-0.2)) (q13_4_2, bcolor("255 0 40") offset(0.2)) (q5_4_3, bcolor("232 104 119") offset(-0.2)) (q13_4_3, bcolor("255 0 40") offset(0.2)) (q5_4_4, bcolor("232 104 119") offset(-0.2)) (q13_4_4, bcolor("255 0 40") offset(0.2)) (q5_4_5, bcolor("232 104 119") offset(-0.2)) (q13_4_5, bcolor("255 0 40") offset(0.2)), recast(bar) vertical scheme (s1color) barwidt (0.4) yscale(range(0(10)50)) ylabel(0 "0%" 10 "10%" 20 "20%" 30 "30%" 40 "40%" 50 "50%", angle(horizontal)) xlabel (1 "Helt uenig" 2 "Uenig" 3 "Hverken enig eller uenig" 4 "Enig" 5 "Helt enig", angle(25) labsize(small)) addplot(scatter @b @at, ms(i) mlabel(@b) mlabpos(12) mlabsize(vsmall) mlabcolor(black)) citop graphregion(color(227 228 229)) plotregion(style(none)) legend(order(1 "2022" 3 "2024") region(lstyle(none)) region(color(227 228 229)))
Desired barplot for reference:

Output as of now:
I'm trying to make a percentage barplot with two different variables that have the same values showcasing the difference between the two variables in each category. I thought the easiest way to go about this was calculating the percentage share of each value and then saving it as an estimate to use it with the coefplot-function. However, when I run the following code I get the weirdest output... Can anyone please explain what is going on? I am very open to suggestions if you think I can go about this in an easier way. I have posted my code, the barplot from my output, and a picture of reference for how I would like the plot to look.

The code:
*Udregner procent på q5_4 og gemmer som estimat
count if q5_4==1
display r(N)*100/1199
estimates store q5_4_1
count if q5_4==2
display r(N)*100/1199
estimates store q5_4_2
count if q5_4==3
display r(N)*100/1199
estimates store q5_4_3
count if q5_4==4
display r(N)*100/1199
estimates store q5_4_4
count if q5_4==5
display r(N)*100/1199
estimates store q5_4_5
*Udregner procent på Q13_4 og gemmer som estimat
count if q13_4==1
display r(N)*100/1199
estimates store q13_4_1
count if q13_4==2
display r(N)*100/1199
estimates store q13_4_2
count if q13_4==3
display r(N)*100/1199
estimates store q13_4_3
count if q13_4==4
display r(N)*100/1199
estimates store q13_4_4
count if q13_4==5
display r(N)*100/1199
estimates store q13_4_5
*Sætter nu vores estimater ind i en barplot-figur med coefficientplot-funktionen
coefplot (q5_4_1, bcolor("232 104 119") offset(-0.2)) (q13_4_1, bcolor("255 0 40") offset(0.2)) (q5_4_2, bcolor("232 104 119") offset(-0.2)) (q13_4_2, bcolor("255 0 40") offset(0.2)) (q5_4_3, bcolor("232 104 119") offset(-0.2)) (q13_4_3, bcolor("255 0 40") offset(0.2)) (q5_4_4, bcolor("232 104 119") offset(-0.2)) (q13_4_4, bcolor("255 0 40") offset(0.2)) (q5_4_5, bcolor("232 104 119") offset(-0.2)) (q13_4_5, bcolor("255 0 40") offset(0.2)), recast(bar) vertical scheme (s1color) barwidt (0.4) yscale(range(0(10)50)) ylabel(0 "0%" 10 "10%" 20 "20%" 30 "30%" 40 "40%" 50 "50%", angle(horizontal)) xlabel (1 "Helt uenig" 2 "Uenig" 3 "Hverken enig eller uenig" 4 "Enig" 5 "Helt enig", angle(25) labsize(small)) addplot(scatter @b @at, ms(i) mlabel(@b) mlabpos(12) mlabsize(vsmall) mlabcolor(black)) citop graphregion(color(227 228 229)) plotregion(style(none)) legend(order(1 "2022" 3 "2024") region(lstyle(none)) region(color(227 228 229)))
Desired barplot for reference:
Output as of now:
Comment