Announcement

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

  • yline in front of a chart.

    HI,

    The following code,

    Code:
    graph hbar a1 a2 a3 a4, yline(50, )  stack percent bar(1, fcolor(0 107 167*.1)lcolor(none))  bar(2, fcolor(0 107 167*.2)lcolor(none))  bar(3, fcolor(0 107 167*.8)lcolor(none))  bar(4, fcolor(0 107 167*1.8)lcolor(none)) graphregion(color(white)) bgcolor(white) legend(order(1 "Men 2 or More Inter. than Women" 2 "Men 0 to 2 More Inter. than Women" 3 "Women 0 to 2 More Inter. than Men" 4 "Women 2 or More Inter. than Men") size(small)) ytitle("Percentage of Seminar Series")
    produces this graph

    Click image for larger version

Name:	Graph.png
Views:	2
Size:	92.7 KB
ID:	1735901

    How can I place the yline in red on top of the graph and not in the back as it's now?

    Thanks!
    Attached Files
    Last edited by Jean Jacques; 03 Dec 2023, 04:04.

  • #2
    Stata's idea of added line options such as xline() and yline() is very rigid: they should never be placed on top of a data element.

    I don't know of a way to do this except by re-creating your graph in twoway. I have to say that I think an unstacked bar chart would work better.

    There is no data example here, so I made up some data.


    Code:
    clear 
    set obs 1
    tokenize "40 30 20 10"
    
    forval j = 1/4 { 
        gen a`j' = ``j''
    }
    
    label var a1 "frog"
    label var a2 "toad"
    label var a3 "newt"
    label var a4 "dragon"
    
    * start here 
    
    gen total = a1 + a2 + a3 + a4 
    gen X1 = 100 * a1 / total
    gen x1 = X1 
    _crcslbl x1 a1 
    forval j = 2/4 {
        local jm1 = `j' - 1
        gen X`j' = X`jm1' + 100 * a`j' / total
        gen x`j' = 100 * a`j' / total 
        _crcslbl x`j' a`j'
    }
    
    gen y = 0 
    
    twoway bar X1 y, base(0) horizontal || ///
    rbar X1 X2 y, horizontal ||    ///
    rbar X2 X3 y, horizontal ||    ///
    rbar X3 X4 y, horizontal ||    ///
    scatteri 0.6 50 -0.6 50, recast(line) ///
    yscale(off) xla(0(25)100) xtitle(% of whatever) xsc(alt) /// 
    legend(order(1 "`: var label a1'" 2 "`: var label a2'" 3 "`: var label a3'" 4 "`: var label a4'") pos(6) row(1)) ///
    name(G1, replace)
    
    graph hbar (asis) x1 x2 x3 x4 , ascategory ysc(alt) ytitle(% of whatever) name(G2, replace)
    Click image for larger version

Name:	stacked_with_added_line_1.png
Views:	1
Size:	29.6 KB
ID:	1735907
    Attached Files

    Comment


    • #3
      Great, thanks!

      I think you're right about the problem with the stacked bar so following the idea I tried to present the data differently.

      Code:
      graph bar (mean) count_diff, over(magnitude, label(labsize(small)))  over(diff_sign, label(labsize(small)))   nofill bar(1, bcolor(0 107 167*.2)) bar(2, bcolor(0 107 167*1.5)) graphregion(color(white)) bgcolor(white) ytitle("Percentage of Sem. Series") ylab(0 "0%" 0.1 "10%" 0.2 "20%" 0.3 "30%" 0.4 "40%")
      which leads to this graph:

      Click image for larger version

Name:	Graph.png
Views:	2
Size:	101.2 KB
ID:	1735923


      However, I can't modify the color of the bars. If I include the "asyvars" the colors change, but the chart as well clearly. How can I modify the color of the bars as it's intended in the code?

      Thanks!

      Attached Files

      Comment


      • #4
        Data example please https://www.statalist.org/forums/help#stata

        Comment


        • #5
          Sorry

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input float(count_diff magnitude diff_sign)
           .1573034 1 1
          .25842696 1 2
          .25842696 2 1
           .3258427 2 2
          end
          label values magnitude magnitude_l
          label def magnitude_l 1 "More than 2", modify
          label def magnitude_l 2 "Between 0 and 2", modify
          label values diff_sign diff_sign_l
          label def diff_sign_l 1 "Series w/ More Inter. to Male", modify
          label def diff_sign_l 2 "Series w/ More Inter. to Female", modify

          Comment


          • #6
            Here are some ideas. You can add options to have different colours. I haven't felt committed to your choices. For example, the word "inter." looks important and should surely not be abbreviated. Conversely, "Series w/" is repetitive and should surely be clear in context.

            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input float(count_diff magnitude diff_sign)
             .1573034 1 1
            .25842696 1 2
            .25842696 2 1
             .3258427 2 2
            end
            label values magnitude magnitude_l
            label def magnitude_l 1 ">2", modify
            label def magnitude_l 2 "0-2", modify
            label values diff_sign diff_sign_l
            label def diff_sign_l 1 "More Interesting to Male", modify
            label def diff_sign_l 2 "More Interesting to Female", modify
            
            replace count_diff = count_diff * 100 
            
            gen xaxis = magnitude + 3 * (diff_sign - 1)
            
            local opts barw(0.8) base(0) 
            twoway bar count_diff xaxis in 1, `opts' ///
            || bar count_diff xaxis in 2, `opts'    ///
            || bar count_diff xaxis in 3, `opts'    ///
            || bar count_diff xaxis in 4, `opts'    ///
            legend(off) xmla(1.5 "More interesting to Male" 4.5 "More interesting to Female", labsize(medium) tlc(none) tlength(*6)) ///
            xla(1 ">2" 2 "0-2" 4 ">2" 5 "0-2", tlength(*0.5) tlc(none)) yla(0(10)40) xtitle("") ytitle(Percent)
            Click image for larger version

Name:	whatever.png
Views:	1
Size:	33.2 KB
ID:	1735943

            Comment


            • #7
              Thanks a lot, it worked perfectly!

              Comment

              Working...
              X