Announcement

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

  • Make number of the observations appear in graph bar or twoway bar

    Hi,
    Let me explain what characterize my observations to better understand what I want to realize.
    I have two variables which characterize my treatment: framing (equal to 0 or 1) & sanctions (0, 1 or 2).
    For each observation there are a deviation level (dev_inter1 variable between 1 and 7) and the punishment stage (Punished equal to 0 or to 1).
    I want to make a graphic that reflects the likelihood of being punished depending on the deviation level but also of the framing (for the treatment sanctions==2). In this graphic I also want the numbers of observations (by deviation level and framing) to appear.

    So I made these codes :
    Code:
    drop if sanctions != 2
    egen percent = mean(100 * Punished), by (dev_inter1 framing)
    collapse percent (count) n=percent, by(framing sanctions dev_inter1)
    egen axis = axis(dev_inter1), gap label(dev_inter1)
     
    
    *** Test 1 : I could not make the number of observations appear.
    #delimit ;
    graph bar percent if sanctions==2, over(framing) over(dev_inter1) asyvars bar(1, color(red)) bar(2, color(blue)) blabel(n)
    title("Likelihood of being punished by deviation level and framing")
    ytitle("Likelihood of being punished")
    name(test1, replace);
    #delimit cr
     
    ***Test 2 : Here the number of observation appear but I could not add different color by framing and I find this graphic less clear.
    #delimit ;
    graph twoway bar percent axis if sanctions==2 ||  ///
    scatter percent axis if sanctions==2, by(framing, title("Likelihood of being punished by deviation level") legend(off)) ms(i) mlab(n) mlabpos(12) mlabcolor(black) ||  ///
    , ytitle("Average punishment points received")
    ylabel(0 (10) 110)
    xtitle("Deviation level")
    xlabel(1 "[-20,-14)" 2 "[-14,-8)" 3 "[-8,-2)" 4 "[-2,2]" 5 "(2,8]" 6 "(8,14]" 7 "(14,20]", labsize(small))
    name(Likelihood_Punished_test, replace);   
    #delimit cr
    Do you know how could I make the number of observations appear in the test 1 ?
    (But also add different color by framing in the test 2? )

    PS: I am sorry I could not upload the graphic.

  • #2
    I don't know why you can't upload the graph but there isn't a data example here either. So in effect you are asking people to read the code and imagine the results, or to translate the code into an equivalent example, or to invent data for the purpose.

    Comment


    • #3
      Sorry, here are the graphics

      In figure1, I would like to add the number of observations by deviation and framing.

      .
      Click image for larger version

Name:	figure1.JPG
Views:	1
Size:	25.8 KB
ID:	1499423


      And if it is not possible, I would have to add different color depending on the framing variable in figure 2

      Click image for larger version

Name:	figure2.JPG
Views:	1
Size:	26.6 KB
ID:	1499422

      Comment


      • #4
        As an exemple, I created a fake database.
        HTML Code:
        sanctions    framing    dev_inter1    Punished
        2    0    1    0
        2    1    2    0
        2    0    3    0
        2    1    4    0
        2    0    5    0
        2    1    6    1
        2    0    7    1
        2    1    1    1
        2    0    2    1
        2    1    3    1
        2    0    4    1
        2    1    5    1
        2    0    6    1
        2    1    7    1
        2    0    3    1
        2    1    4    1
        2    0    5    0
        2    1    3    0
        2    0    4    0
        2    1    5    0
        2    0    1    0
        2    1    2    0

        Comment


        • #5
          Thanks for the example. I think the main thing you're missing is separate.

          This shows some technique:

          Code:
          clear 
          set obs 14
          egen x1 = seq(), block(7) 
          label def x1 1 negative 2 positive 
          label val x1 x1 
          egen x2 = seq(), to(7) 
          set seed 2803 
          gen y = runiform()
          gen show = _n 
          
          separate y, by(x1) veryshortlabel 
          
          twoway bar y1 x2, fcolor(red*0.2) lcolor(red) xla(1/7, valuelabel) ///
            || bar y2 x2, fcolor(blue*0.2) lcolor(blue) xla(1/7, valuelabel)   ///
          || scatter y1 x2, ms(none) mla(show) mlabpos(12) /// 
          || scatter y2 x2, ms(none) mla(show) mlabpos(12) ///
          subtitle(, fcolor(eltgreen*0.2)) by(x1, legend(off) note(x1, size(medsmall) pos(12)))

          Click image for larger version

Name:	twocolourbar.png
Views:	1
Size:	16.6 KB
ID:	1499429

          Comment


          • #6
            Dear Dr. Cox,

            Thank you for your answer, but I don’t get why it does not work properly when I adapt it to my datas.

            I have got this message or error
            Code:
            invalid 'mlabels'
            r(198);
            Doing this code:
            Code:
            use data_FRAMING.dta, replace
            drop if sanctions != 2
             
            egen percent = mean(100 * Punished), by (dev_inter1 framing)
            collapse percent (count) show=percent, by(framing sanctions dev_inter1)
            egen axis = axis(dev_inter1), gap label(dev_inter1)
            separate percent, by(framing) veryshortlabel
            Code:
            #delimit ;
            graph twoway bar percent0 axis if sanctions==2, color(red) xla(, valuelabel) ///
            || bar percent1 axis if sanctions==2, color(blue) xla(, valuelabel) ///
            || scatter percent0 axis if sanctions==2, ms(none) mla(show) mlabpos(12) ///
            || scatter percent1 axis if sanctions==2, ms(none) mla(show) mlabpos(12) ///
            , subtitle(, fcolor(eltgreen*0.2)) by(framing, legend(off) note("Likelihood of being punished by deviation level", size(medsmall) pos(12)))
            ytitle("Average punishment points received")
            ylabel(0 (10) 100)
            xtitle("Deviation level")
            xlabel(1 "[-20,-14)" 2 "[-14,-8)" 3 "[-8,-2)" 4 "[-2,2]" 5 "(2,8]" 6 "(8,14]" 7 "(14,20]", labsize(small))
            name(Likelihood_Punished_test, replace);   
            #delimit cr

            It works when I take the scatter lab off, but doing so the number of observation do not appear. I don’t get what I am not doing properly
            Code:
            #delimit ;
            graph twoway bar percent0 axis if sanctions==2, color(red) xla(, valuelabel) ///
            || bar percent1 axis if sanctions==2, color(blue) xla(, valuelabel) ///
            || scatter percent0 axis if sanctions==2, ms(none) mlabpos(12) ///
            || scatter percent1 axis if sanctions==2, ms(none) mlabpos(12) ///
            , subtitle(, fcolor(eltgreen*0.2)) by(framing, legend(off) note("Likelihood of being punished by deviation level", size(medsmall) pos(12)))
            ytitle("Average punishment points received")
            ylabel(0 (10) 100)
            xtitle("Deviation level")
            xlabel(1 "[-20,-14)" 2 "[-14,-8)" 3 "[-8,-2)" 4 "[-2,2]" 5 "(2,8]" 6 "(8,14]" 7 "(14,20]", labsize(small))
            name(Likelihood_Punished_test, replace);   
            #delimit cr
            Another question : Does you think it will be possible to make the number of observation appear using graph bar? I find the results presented this way easier to understand.

            Comment


            • #7
              I don't think there is an easy way to get what you want with graph bar. There is a fiddly way to do it with lots of text() options, which I don't recommend.

              Meanwhile I can't see a problem with your code but to test it I need a data example. It seems that showing the results of

              Code:
              dataex percent0 percent1 axis framing show
              should be enough. See also FAQ Advice #12.

              For the benefit of anyone else wanting to follow the egen function axis() mentioned in #1 and #6 should be explained as community-contributed and as coming from egenmore (SSC). FAQ Advice #12 again explains how to report such mentions.

              Comment


              • #8
                Ok, here there is a database exemple

                Code:
                * Example generated by -dataex-. To install: ssc install dataex
                clear
                input byte framing float(dev_inter1 percent0 percent1) long show float axis
                0 -3       100         .  13 1
                0 -2  92.30769         .  52 2
                0 -1  52.20126         . 159 3
                0  0 24.313726         . 255 4
                0  1  22.55639         . 133 5
                0  2 36.734695         .  49 6
                0  3 36.842106         .  19 7
                1 -3         .       100   8 1
                1 -2         .    59.375  32 2
                1 -1         .  55.42169  83 3
                1  0         .  19.66759 361 4
                1  1         . 32.876713  73 5
                1  2         .    28.125  32 6
                1  3         .         0  11 7
                end
                label values framing Framing
                label def Framing 0 "Negative Framing", modify
                label def Framing 1 "Positive Framing", modify
                label values dev_inter1 dev_inter1_label
                label def dev_inter1_label -3 "[-20,-14)", modify
                label def dev_inter1_label -2 "[-14,-8)", modify
                label def dev_inter1_label -1 "[-8,-2)", modify
                label def dev_inter1_label 0 "[-2,2]", modify
                label def dev_inter1_label 1 "(2,8]", modify
                label def dev_inter1_label 2 "(8,14]", modify
                label def dev_inter1_label 3 "(14,20]", modify
                label values axis axis
                label def axis 1 " [-20,-14)", modify
                label def axis 2 " [-14,-8)", modify
                label def axis 3 " [-8,-2)", modify
                label def axis 4 " [-2,2]", modify
                label def axis 5 " (2,8]", modify
                label def axis 6 " (8,14]", modify
                label def axis 7 " (14,20]", modify

                Comment


                • #9
                  This works for me. The problem was that you put a spurious comma in front of subtitle(). These things can be hard to spot.


                  Code:
                  graph twoway bar percent0 axis, color(red) xla(, valuelabel) ///
                  || bar percent1 axis, color(blue) xla(, valuelabel) ///
                  || scatter percent0 axis, ms(none) mla(show) mlabpos(12) ///
                  || scatter percent1 axis, ms(none) mla(show) mlabpos(12) ///
                  subtitle(, fcolor(eltgreen*0.2)) by(framing, legend(off) ///
                  note("Likelihood of being punished by deviation level", size(medsmall) pos(12))) ///
                  ytitle("Average punishment points received") ylabel(0 (10) 100) xtitle("Deviation level") ///
                  xlabel(1 "[-20,-14)" 2 "[-14,-8)" 3 "[-8,-2)" 4 "[-2,2]" 5 "(2,8]" 6 "(8,14]" 7 "(14,20]", labsize(small)) ///
                  name(Likelihood_Punished_test, replace)

                  Comment


                  • #10
                    Thank you very much for your insight.

                    Comment

                    Working...
                    X