Announcement

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

  • Bar labels for rbar

    Hi Statalists,

    I am plotting range bars, where each range bar represents a period spent in bed, starting from bedtime, ending at get-up time. I also plot three additional ranges: the time it takes to fall asleep (SOL), the amount of time wake between sleep onset and final awakenings (WASO), and the amount of time from final awakening to get-up time. So far so good!

    Click image for larger version

Name:	Screenshot 2020-09-24 at 10.21.45.png
Views:	1
Size:	340.5 KB
ID:	1574055


    Now I want to add labels inside the three additional range bars, with the value of the label being the width of the bar (which in my graph is time). I can find no option in doing so. The blabel options are only available for graph bar and graph hbar.

    My question is whether there is an option to add value labels in the three additional range bars, or if there is a way around it?

    Example data is as follows:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(day bedtimeh outbedtimeh_corrected sol_end wasfh_start waso_start waso_end)
    1.9149957e+12     23.25        32        24     31.25 27.416666 27.833334
    1.9152486e+12      22.5 29.833334      24.5      29.5     26.75     27.25
     1.915344e+12     25.25     34.25     25.75        32      28.5     29.25
    1.9154322e+12        23        33 24.166666      32.5  27.83333  28.83333
     1.915509e+12     22.75 29.866667     23.75 29.833334    26.375 27.208334
    1.9155978e+12 22.833334     32.75  23.16667      30.5  25.16667      28.5
    end
    format %tcDay_dd-Mon day
    And the line to create the graph is as follows:
    Code:
    summarize day
    twoway (rbar bedtimeh outbedtimeh_corrected day, lwidth(thick) fcolor(black) lcolor(black) horizontal) (rbar bedtimeh sol_end day, lwidth(thick) fcolor(gs16) lcolor(gs10) horizontal) (rbar wasfh_start outbedtimeh_corrected day, lwidth(thick) fcolor(gs16) lcolor(gs10) horizontal) (rbar waso_start waso_end day, lwidth(thick) fcolor(gs16) lcolor(gs10) horizontal), legend(off) xtitle("Tijd (24 uur)") ytitle("") xline(24, lwidth(thin) lcolor(black)) yscale(reverse) ylabel(`r(min)'(86400000)`r(max)', angle(horizontal) ticks nogrid) xlabel(20 "20" 21 "21" 22 "22" 23 "23" 24 "0" 25 "01" 26 "02" 27 "03" 28 "04" 29 "05" 30 "06" 31 "07" 32 "08" 33 "09" 34 "10" 35 "11")
    Thank you so much in advance.

    FYI: the vertical blue lines are the average bedtime and get-up times, which I removed from the example code.


  • #2
    Holding datetime variables as float is dangerous, as help datetime advises repeatedly, but it doesn't seem to be biting you here.

    Otherwise, the best way to show text on the graph is likely to be as marker labels using mlabel() Your bars are too thin to show anything inside, but you could show text above or below. Would need to specify via an extra scatter command what to show and where to show it. I suspect that you would need to round the time suitably, which means pushing a numeric variable through strofreal() with a format. So you need to subtract end from start to get duration, and so on.

    I can't see that your question has anything to do with value labels in Stata's sense.

    Comment


    • #3
      Thanks for your reply.

      Your suggestion (the scatter one) has allowed me to solve my problem, i.e. I can now include the duration of each additional range in the respective bar. Unfortunately, I was unable to increase the bar width (actually bar height, since bars are plotted horizontally). The barwidth option doesn't behave as I expect it to.
      Click image for larger version

Name:	Screenshot 2020-09-24 at 13.08.13.png
Views:	1
Size:	411.1 KB
ID:	1574079



      For those interested, here is the code (xlines removed again). Just note that the code has some additional variables, which are currently not in the example code of my previous post:
      Code:
      twoway ///
          (rbar bedtimeh outbedtimeh_corrected day, lwidth(thick) fcolor(black) lcolor(black) horizontal) ///
          (rbar bedtimeh sol_end day, lwidth(thick) fcolor(gs16) lcolor(gs10) horizontal) ///
          (rbar wasfh_start outbedtimeh_corrected day, lwidth(thick) fcolor(gs16) lcolor(gs10) horizontal) ///
          (rbar waso_start waso_end day, lwidth(thick) fcolor(gs16) lcolor(gs10) horizontal) ///
          (scatter day midpoint_of_sleep, mcolor(gs10) msymbol(point) mlabel(waso) mlabsize(tiny) mlabcolor(black) mlabposition(0)) ///
          (scatter day loc_sol, mcolor(gs10) msymbol(point) mlabel(sol) mlabsize(tiny) mlabcolor(black) mlabposition(0)) ///
          (scatter day loc_wasf, mcolor(gs10) msymbol(point) mlabel(wasfmin) mlabsize(tiny) mlabcolor(black) mlabposition(0)), ///
          legend(off) ///
          xtitle("Tijd (24 uur)") ///
          ytitle("") xline(24, lwidth(thin) lcolor(black)) ///
          ylabel(`r(min)'(`msofday')`r(max)', angle(horizontal) ticks nogrid) ///
          xlabel(20 "20" 21 "21" 22 "22" 23 "23" 24 "0" 25 "01" 26 "02" 27 "03" 28 "04" 29 "05" 30 "06" 31 "07" 32 "08" 33 "09" 34 "10" 35 "11")
      Thank you so much for your quick reply and solution.

      Apologies, question had nothing to do with value labels as you pointed out correctly. Wrong choice of words.

      Comment


      • #4
        it's the barwidth() option you need. lwidth() will only get you so far.

        Comment


        • #5
          Thanks Nick!

          Click image for larger version

Name:	Screenshot 2020-09-24 at 14.09.40.png
Views:	1
Size:	431.5 KB
ID:	1574092


          Code:
          twoway ///
              (rbar bedtimeh outbedtimeh_corrected day, barwidth(`barwidth') fcolor(black) lcolor(black) horizontal) ///
              (rbar bedtimeh sol_end day, barwidth(`barwidth') fcolor(gs10) lcolor(black) horizontal) ///
              (rbar wasfh_start outbedtimeh_corrected day, barwidth(`barwidth') fcolor(gs10) lcolor(black) horizontal) ///
              (rbar waso_start waso_end day, barwidth(`barwidth') fcolor(gs10) lcolor(black) horizontal) ///
              (scatter day midpoint_of_sleep, mcolor(gs10) msymbol(point) mlabel(waso) mlabsize(small) mlabcolor(white) mlabposition(0)) ///
              (scatter day loc_sol, mcolor(gs10) msymbol(point) mlabel(sol) mlabsize(small) mlabcolor(white) mlabposition(0)) ///
              (scatter day loc_wasf, mcolor(gs10) msymbol(point) mlabel(wasfmin) mlabsize(small) mlabcolor(white) mlabposition(0)), ///
              legend(off) ///
              xtitle("Tijd (24 uur)") ///
              ytitle("") xline(24, lwidth(thin) lcolor(black)) ///
              ylabel(`r(min)'(`msofday')`r(max)', angle(horizontal) ticks nogrid) ///
              xlabel(20 "20" 21 "21" 22 "22" 23 "23" 24 "0" 25 "01" 26 "02" 27 "03" 28 "04" 29 "05" 30 "06" 31 "07" 32 "08" 33 "09" 34 "10" 35 "11")

          Comment

          Working...
          X