Announcement

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

  • Making barplots with different variables using coefplot

    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:
    Click image for larger version

Name:	reference.png
Views:	1
Size:	27.0 KB
ID:	1750948



    Output as of now:
    Click image for larger version

Name:	output.png
Views:	1
Size:	58.0 KB
ID:	1750949

  • #2
    I haven't used coefplot much, if at all. It is a command (not a function) from SSC (as you are asked to explain).

    My main constructive point is that you can get bar charts like this directly using official commands.

    You don't give a data example, but your percents can be read off the first chart and, other way round, getting from your data to where I start here would be easy to show if you just explained your data a little more directly. On that and the point about SSC, please see https://www.statalist.org/forums/help#stata


    Code:
    clear
    input x y2022 y2024
    1    10 8
    2    18 9
    3    19 11
    4    34 45
    5    21 27
    end
    
    graph bar (asis) y2022 y2024, over(x)  name(G1, replace) blabel(bar) legend(order(1 "2022" 2 "2024"))
    
    gen x_L = x - 0.2
    gen x_R = x + 0.2  
    
    gen show22 = strofreal(y2022) + "%"
    gen show24 = strofreal(y2024) + "%"
    
    twoway bar y2022 x_L, barw(0.4) color(stc1) base(0) || scatter y2022 x_L, ms(none) mla(show22) mlabpos(12) mlabc(stc1) ///
    ||     bar y2024 x_R, barw(0.4) color(stc2) || scatter y2024 x_R, ms(none) mla(show24) mlabpos(12) mlabc(stc2) ///
    legend(order(1 "2022" 3 "2024")) ylabel(0(10)50) name(G2, replace) ytitle(% of whatever)


    The two graphs are very similar, so I just show the second here.

    EDIT: It seems clear from Norwegian words that your variable is ordinal (ordered, graded) so floatplot from SSC is an alternative. Search the site using floatplot as a keyword.
    Click image for larger version

Name:	anotherbarchart.png
Views:	1
Size:	41.5 KB
ID:	1750957

    Last edited by Nick Cox; 23 Apr 2024, 09:13.

    Comment


    • #3
      Thank you so much for the answer! But this still does not work - I have might not explained myself proberly. See, the 1-5 is not another variable. It is the different values of each variable (y2022 and y2024 in your code).

      Variable 1 (q4_4):
      tab q5_4

      Q5_4 De |
      følgende |
      spørgsmål |
      handler om, |
      hvordan du |
      oplever dit |
      daglige |
      arbejde. | Freq. Percent Cum.
      ------------+-----------------------------------
      1 | 535 44.62 44.62
      2 | 448 37.36 81.98
      3 | 151 12.59 94.58
      4 | 45 3.75 98.33
      5 | 20 1.67 100.00
      ------------+-----------------------------------
      Total | 1,199 100.00

      Variable 2 (q13_4):
      tab q13_4

      Q13_4 |
      Kvalitet |
      kan opleves |
      på en |
      række |
      forskellige |
      områder. I |
      hvilken |
      grad vurde | Freq. Percent Cum.
      ------------+-----------------------------------
      1 | 116 9.67 9.67
      2 | 465 38.78 48.46
      3 | 519 43.29 91.74
      4 | 92 7.67 99.42
      5 | 7 0.58 100.00
      ------------+-----------------------------------
      Total | 1,199 100.00


      So on the X-axis I want to have the values of the variable, which are the same, and then I want to have two different bars for each value displaying the percentage share of each variable for each value. Please let me know if you need further information.

      Comment


      • #4
        This is puzzling as your desire

        So on the X-axis I want to have the values of the variable, which are the same, and then I want to have two different bars for each value displaying the percentage share of each variable for each value.
        is exactly what my code already did in #2.

        Naturally my code doesn't work for you because not only do you not have variables with those names, the layout of your data is not similar to that of #2. One way to get those graphs from that data layout would be to work on your real data. But I was not clear enough in giving the first link that the main implication of FAQ Advice #12 is that you should give us a data example using dataex.

        As it happens your problem seems close enough to that in a more recent thread https://www.statalist.org/forums/for...two-bar-graphs that the code there may help you reshape what you have.

        Comment


        • #5
          But in #2 you used a third variable, x, to generate the positions, which you later used in the twoway bar command, no? Again, the x-axis must not depend on a third variable, but the values of the two variables (which are the same). Again, thank you so much for your help, Nick!

          Comment


          • #6
            Sorry, but I don't know where you are going here. I am giving you some time and writing code to solve your problems, but your stance seems to be that you want it done differently. As said, I am also willing to write code to convert your existing data layout to one that will serve better for what you want, but you have yet to provide a data example in the desired manner, so no go there from me.

            It's your point of view that two variables share the same values and so can be plotted on the same axis. That's fine in logical, statistical and graphical terms, but you need to explain that in terms that Stata can understand. Congratulations if you, or anyone else, can get the plot you want without doing it my way, or something essentially equivalent. You could (e.g.) superimpose histograms, but the result would be a mess and achieving offset bars also requires new variables.

            Stata's graphics is highly flexible, but not infinitely so. It's a surprise sometimes even for experienced users (I've been using Stata for 30 years, and almost every working day) that a design simple to think about may be quite hard to achieve. (The opposite is true too, what looks hard can be surprisingly easy.)

            The thread cited in #4 is independent, but some similar points arise.

            Comment

            Working...
            X