Announcement

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

  • Adding two bar labels to hbar graph

    I am trying to add two bar labels to this frequency hbar graph. One with the totals of each category inside each bar and another label outside with the name of the category. But the code is only choosing to pick one barlabel, group or bar but not both.
    Honestly, just the label is probably enough from purely data depiction purposes, but I was curious if there is an option.

    graph hbar (count), over(dcat, sort(1) label(nolabels)) blabel(group) ///
    title("Frequencies of breast hematopoietic neoplasm") ///
    subtitle("Between 2000-2021") ///
    blabel(bar, position(inside) format(%9.0f) color(white)) ///
    note("Most of these were diagnosed on needle core biopsies")
    graph export Vickerybar.png, replace



    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte obs str28 dcat
     1 "PTCL, NOS"                   
     2 "DLBCL"                       
     3 "MZL"                         
     4 "DLBCL"                       
     5 "MZL"                         
     6 "MZL"                         
     7 "FL"                          
     8 "DLBCL"                       
     9 "FL"                          
    10 "MZL"                         
    11 "FL"                          
    12 "MZL"                         
    13 "MZL"                         
    14 "DLBCL"                       
    15 "FL"                          
    16 "DLBCL"                       
    17 "MZL"                         
    18 "CLL/SLL"                     
    19 "DLBCL"                       
    20 "DLBCL"                       
    21 "MZL"                         
    22 "DLBCL"                       
    23 "FL"                          
    24 "DLBCL"                       
    25 "MZL"                         
    26 "B-lymphoblastic"             
    27 "ALCL, ALK+"                  
    28 "MZL"                         
    29 "HGBCL, NOS"                  
    30 "HGBCL, NOS"                  
    31 "MZL"                         
    32 "MZL"                         
    33 "DLBCL"                       
    34 "Mycosis Fungoides"           
    35 "DLBCL"                       
    36 "MZL"                         
    37 "Histiocytic Sarcoma"         
    38 "CLL/SLL"                     
    39 "MZL"                         
    40 "Extramedullary myeloid Tumor"
    41 "DLBCL"                       
    42 "FL"                          
    43 "T-lymphoblastic"             
    44 "DLBCL"                       
    45 "MZL"                         
    46 "DLBCL"                       
    47 "MZL"                         
    48 "MZL"                         
    49 "FL"                          
    50 "DLBCL"                       
    51 "DLBCL"                       
    52 "B-lymphoblastic"             
    53 "FL"                          
    54 "DLBCL"                       
    55 "DLBCL"                       
    56 "MZL"                         
    57 "CLL/SLL"                     
    58 "MZL"                         
    59 "FL"                          
    end

  • #2
    I don't think this is possible using just the bar graph, but consider this instead:

    Code:
    contract dcat
    sort _freq
    gen x = _n
    
    
    #delimit ;
    twoway bar _freq x, horizontal ||
        scatter x _freq, msymbol(i) mlabel(dcat) ||
        scatter x _freq, msymbol(i) mlabel(_freq) mlabpos(9) mlabcolor(white) mlabformat(%9.0f)
        ytitle("") ylabel(none)    
        title("Frequencies of breast hematopoietic neoplasm")
        subtitle("Between 2000-2021")
        note("Most of these were diagnosed on needle core biopsies")
        legend(off)
        scheme(scolor2)
        ;
    #delimit cr
    which produces:
    Click image for larger version

Name:	Screenshot 2022-09-11 at 9.32.59 PM.png
Views:	1
Size:	184.7 KB
ID:	1681501

    Comment


    • #3
      Originally posted by Hemanshu Kumar View Post
      I don't think this is possible using just the bar graph, but consider this instead:

      Code:
      contract dcat
      sort _freq
      gen x = _n
      
      
      #delimit ;
      twoway bar _freq x, horizontal ||
      scatter x _freq, msymbol(i) mlabel(dcat) ||
      scatter x _freq, msymbol(i) mlabel(_freq) mlabpos(9) mlabcolor(white) mlabformat(%9.0f)
      ytitle("") ylabel(none)
      title("Frequencies of breast hematopoietic neoplasm")
      subtitle("Between 2000-2021")
      note("Most of these were diagnosed on needle core biopsies")
      legend(off)
      scheme(scolor2)
      ;
      #delimit cr
      which produces:
      [ATTACH=CONFIG]n1681501[/ATTACH]
      Thanks much, Hemanshu. I would never have guessed a way like this. Also, I like your layout better with the most frequent category as the topmost bar.

      Comment

      Working...
      X