Announcement

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

  • Particular labeling of a bar chart

    Dear Stata forum,

    I am trying to build a specific bar chart (below is the code). What I am trying to do is that the yellow numbers are just dots or something, while the red numbers stay as they are or ideally can be labeled values (like "Country A" and "Country B").

    I guess it has something to do with blabel(group), and I would like to change what Stata uses as label. But I don't know how I can do that. If I use something else than "group", no data is drawn.


    Click image for larger version

Name:	Screenshot 2024-03-11 190518.png
Views:	1
Size:	17.1 KB
ID:	1746258



    Any help is much appreciated!




    Here is an MWE for the data:
    Code:
    clear
    set obs 4
    
    * Create variables
    gen Period = .
    gen Reporter = .
    gen Y1 = .
    gen Y4 = .
    gen Y12 = .
    
    
    
    * Assign values based on the sample
    replace Period = 10 in 1/2
    replace Period = 20 in 3/4
    
    replace Reporter = 1 in 1
    replace Reporter = 2 in 2
    replace Reporter = 1 in 3
    replace Reporter = 2 in 4
    
    replace Y1 = 0.91 in 1
    replace Y1 = 0.63 in 2
    replace Y1 = 0.87 in 3
    replace Y1 = 0.81 in 4
    
    replace Y4 = 0.84 in 1
    replace Y4 = 0.40 in 2
    replace Y4 = 0.78 in 3
    replace Y4 = 0.66 in 4
    
    replace Y12 = 0.77 in 1
    replace Y12 = 0.31 in 2
    replace Y12 = 0.74 in 3
    replace Y12 = 0.60 in 4
    And here is the graph I am trying to build:
    Code:
    graph bar Y1 Y4 Y12 ,  over(Reporter) over(Period) bargap(-50) legend(off) ///
        bar(1, color(none)) bar(2, color(none)) bar(3, color(none)) blabel(group, position(inside))

    Last edited by Friedrich Hula; 11 Mar 2024, 13:53. Reason: #barchart #bar

  • #2
    Only the following are allowed:


    Syntax

    graph {bar|hbar} ..., ... blabel(what [, where_and_how]) ...

    what Description
    ---------------------------------------------------------------
    none no label; the default
    bar label is bar height
    total label is cumulative bar height
    name label is name of yvar
    group label is first over() group
    ----------------------------------------------------------------
    See

    Code:
    help blabel_option
    Why not ask your actual question rather than your attempted solution? What is your final objective?

    Comment


    • #3
      Thanks, I was afraid that this is the answer. I want to build a graph that looks like this Excel graph (not necessarily with the import/export differentiation):
      Click image for larger version

Name:	imagen.png
Views:	1
Size:	45.0 KB
ID:	1746304


      The problem Stata seems to have is that it does not skip the years 2 to 3 and 5 to 11, when I would draw some twoway dot chart or the like.

      Do you have any idea?

      Comment


      • #4
        You would not use the actual values on the axis, but some other values and then add labels.

        Code:
        search labmask
        and install from the Stata Journal if you already have a variable containing the wanted labels. The x axis in your graph can be replicated as below:

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input float(x y)
         4  .3390499
         8   .993872
        12   .781655
        16 1.2848227
        20   .961148
        24 1.0478545
        28  .5088348
        32 .18740295
        36  1.849152
        end
        
        scatter y x, msy(t) xtitle("") xsc(r(2 38) noextend) ///
        xlab( 4 "1 year" 8 "4 year" 12 "12 year" 16 "1 year" 20 "4 year" 24 "12 year" 28 "1 year" 32 "4 year" 36 "12 year", noticks) ///
        xtick(2(12)38, tlength(*7.5)) plotregion(margin(zero)) ytitle("Something")
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	54.1 KB
ID:	1746332

        Comment


        • #5
          Code:
          graph dot
          has an undocumented vertical option.

          Comment


          • #6
            Brilliant, thank you both! I didn't know about graph dot vertical, but this allows me to use twice the "over" part, which I need. This, unfortunately, is not possible in the scatter graph.

            Thank you so much again!

            Comment

            Working...
            X