Announcement

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

  • How to add a cross or filled cirkel at the end of a bar of the hbar graph

    Hi there

    I would like to add e.g. a cross or a filled cirkle at the end of some of the bars in the hbar graph to indicate that the individual person has died (var died: yes=1, no=0).
    I have great difficulties finding the solution/ command for this. Anyone who knows how to do it?
    I have created an example to visualize similar data:

    use http://www.stata-press.com/data/r14/diet, clear

    rename dob date_first_diagnosis
    label var date_first_diagnosis date

    rename doe date_second_diagnosis
    label var date_second_diagnosis date

    rename dox date_end
    label var date_end date

    rename hienergy died
    label var died "died yes=1 no=0"

    keep id died date_first_diagnosis date_second_diagnosis date_end

    gen time1= date_second_diagnosis-date_first_diagnosis
    gen time2= date_end-date_second_diagnosis

    drop if id>50

    generate total= time1+time2

    graph hbar (asis) time1 time2, legend(off) ///
    over(id, gap(*.9) label(labsize(*0.6)) sort(total) descending) stack nofill ///
    ytitle("Days") ///
    ylabel ("5000 10000 15000 20000 25000 30000") ///
    ysize(7)






  • #2
    You can do this with twoway bar, rbar, and scatter (note this uses labmask part of Nick Cox's labutil: modules for managing value and variable labels) :

    Code:
    use http://www.stata-press.com/data/r14/diet, clear
    
    rename dob date_first_diagnosis
    label var date_first_diagnosis date
    
    rename doe date_second_diagnosis
    label var date_second_diagnosis date
    
    rename dox date_end
    label var date_end date
    
    rename hienergy died
    label var died "died yes=1 no=0"
    
    keep id died date_first_diagnosis date_second_diagnosis date_end
    
    gen time1= date_second_diagnosis-date_first_diagnosis
    gen time2= date_end-date_second_diagnosis
    
    drop if id>50
    
    generate total= time1+time2
    
    graph hbar (asis) time1 time2, legend(off) ///
    over(id, gap(*.9) label(labsize(*0.6)) sort(total) descending) stack nofill ///
    ytitle("Days") ///
    ylabel ("5000 10000 15000 20000 25000 30000") ///
    ysize(7) name(gr1,replace)
    
    
    sort total
    gen id2 = _n
    labmask id2, values(id)
    gen total2 = total + 350 
    twoway bar total id2 , base(0) barw(0.5) fin(50) hori || rbar time1 total id2 ,  /// 
       barw(0.5) fin(50) hor ylabel(1/50, valuelabel labsize(*0.6)) ytitle("")  /// 
       xlabel(5000(5000)30000) ysize(7) xtitle(Days) legend(off) /// 
       || scatter id2  total2 if died == 1, ms(+)  mlabsize(medlarge) mcolor(black) /// 
       || scatter id2  total2 if died == 0, ms(O)  mlabsize(medlarge) mcolor(black)
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	36.5 KB
ID:	1448815

    Comment


    • #3
      Hi Scott

      Thank you very much for your solution. I made it work. However, my example was a little simplified... I used the bar because I have 3 different groups that should be separated. I have incorporated this in the following. Would you suggest me to use the hbar or the twoway bar, rbar, and scatter?

      Again, thank you so much!
      Johanne



      use http://www.stata-press.com/data/r14/diet, clear

      rename dob date_first_diagnosis
      label var date_first_diagnosis date

      rename doe date_second_diagnosis
      label var date_second_diagnosis date

      rename dox date_end
      label var date_end date

      rename hienergy died
      label var died "died yes=1 no=0"

      keep id died date_first_diagnosis date_second_diagnosis date_end

      gen time1= date_second_diagnosis-date_first_diagnosis
      gen time2= date_end-date_second_diagnosis

      drop if id>50

      generate total= time1+time2

      gen group="AAA"
      replace group="BBB" if id>10
      replace group="CCC" if id>30

      gsort group -total
      by group:gen idd=_n

      graph hbar (asis) time1 time2, legend(off) ///
      over(id, gap(*.9) label(labsize(*0.6)) sort(total) descending) over(group) stack nofill ///
      bar(1, color(81 31 17 39)) ///
      bar(2, color(52 43 39 1)) ///
      ytitle("Days") ///
      ylabel ("5000 10000 15000 20000 25000 30000") ///
      ysize(7)

      Comment


      • #4
        Yes you will still need to use twoway bar if you want to place markers at the end of the bars. In this case you will have to produce three graphs and combine them. You might need to play around with the fysize() option to get your desired result.

        Also if you use the #button on the tool bar it will wrap the code in CODE mark-up and make it easier to read.

        Code:
        use http://www.stata-press.com/data/r14/diet, clear
        
        rename dob date_first_diagnosis
        label var date_first_diagnosis date
        
        rename doe date_second_diagnosis
        label var date_second_diagnosis date
        
        rename dox date_end
        label var date_end date
        
        rename hienergy died
        label var died "died yes=1 no=0"
        
        keep id died date_first_diagnosis date_second_diagnosis date_end
        
        gen time1= date_second_diagnosis-date_first_diagnosis
        gen time2= date_end-date_second_diagnosis
        
        drop if id>50
        
        generate total= time1+time2
        
        gen group="AAA"
        replace group="BBB" if id>10
        replace group="CCC" if id>30
        
        gsort group -total
        by group:gen idd=_n
        
        graph hbar (asis) time1 time2, legend(off) ///
        over(id, gap(*.9) label(labsize(*0.6)) sort(total) descending) over(group) stack nofill ///
        bar(1, color(81 31 17 39)) ///
        bar(2, color(52 43 39 1)) ///
        ytitle("Days") ///
        ylabel ("5000 10000 15000 20000 25000 30000") ///
        ysize(7) name(gr1,replace) 
        
        sort group total
        gen total2 = total + 350 
        bys group: gen id2 = _n
        
        labmask id2 if group == "AAA", values(id)
        
        twoway bar total id2 if group == "AAA" , base(0) barw(0.5) fin(50) hori fysize(20) xlabel(5000(5000)30000)   /// 
           || rbar time1 total id2 if group == "AAA",  /// 
           barw(0.5) fin(50) hor ylabel(1/10, valuelabel labsize(*0.6) nogrid)  /// 
            ytitle("AAA", orientation(horiz) size(medlarge))  /// 
           xscale(off)  xtitle("") legend(off) /// 
           || scatter id2  total2 if died == 1 & group == "AAA", xscale(off) /// 
                ms(+)  mlabsize(medlarge) mcolor(black) /// 
           || scatter id2  total2 if died == 0 & group == "AAA",  xscale(off) /// 
                ms(O)  mlabsize(medlarge) mcolor(black) name(gr2,replace)
        
        labmask id2 if group == "BBB", values(id)        
        twoway bar total id2 if group == "BBB" , base(0) barw(0.5) fin(50) hori fysize(40) xlabel(5000(5000)30000) /// 
           || rbar time1 total id2 if group == "BBB",  /// 
           barw(0.5) fin(50) hor ylabel(1/20, valuelabel labsize(*0.6)  nogrid)  /// 
           ytitle("BBB" , orientation(horiz) size(medlarge))  /// 
           xscale(off)  ysize(7) xtitle("") legend(off) /// 
           || scatter id2  total2 if died == 1 & group == "BBB", xscale(off) /// 
                ms(+)  mlabsize(medlarge) mcolor(black) /// 
           || scatter id2  total2 if died == 0 & group == "BBB", xscale(off) /// 
                ms(O)  mlabsize(medlarge) mcolor(black) name(gr3,replace)        
        
        labmask id2 if group == "CCC", values(id)
        twoway bar total id2 if group == "CCC" , base(0) barw(0.5) fin(50) hori fysize(40) xlabel(5000(5000)30000) /// 
           || rbar time1 total id2 if group == "CCC",  /// 
           barw(0.5) fin(50) hor ylabel(1/20, valuelabel labsize(*0.6)  nogrid) /// 
            ytitle("CCC" , orientation(horiz) size(medlarge))  /// 
             xtitle("") legend(off) /// 
           || scatter id2  total2 if died == 1 & group == "CCC",  /// 
                ms(+)  mlabsize(medlarge) mcolor(black) /// 
           || scatter id2  total2 if died == 0 & group == "CCC",  /// 
                ms(O)  mlabsize(medlarge) mcolor(black) name(gr4,replace)            
            
        graph combine gr2 gr3 gr4 , col(1) imargin(0 0 0 0)
        Click image for larger version

Name:	Graph2.png
Views:	1
Size:	42.9 KB
ID:	1448908

        Comment


        • #5
          Hi Scott,
          Wow, you have been such a help; thank you very much!! As you predicted, I am playing with the length of the x-axix. It seems that a difference of the length of title of the y axis makes the combined graphs differs. Again thank you very much for all your help!
          Best, Johanne

          Comment

          Working...
          X