Announcement

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

  • Custom labels in bar graph

    Hi all,

    I am trying to add custom labels to a bar graph. Currently, the labels are simply the variable labels:

    graph bar (mean) EXP, over(MONTH) blabel(name, pos(outside))

    What I want to do is change the labels above each bar to equal the observation count by MONTH. So, if there are 150 values of EXP being averaged in MONTH == 1, the bar label would be "150" in MONTH == 1. As months increase, my observations diminish. I would like the labels to reflect that.

    I have already generated a variable (MO_N) with the monthly observation counts. For context, I have 24 months in the graph. I need to automate the process, as I will be looping over hundreds of values for a different variable.

    Your help would be appreciated. Thanks!



  • #2
    Hi William,

    Welcome to Statalist.

    This is not possible with blabel() on graph bars, as i'm sure you've already discovered by looking at the help file for graph bar. Instead, I suggest you collapse your data, add in some kind of sequential id (gen seqid = _n), and then use twoway bar to make your plots. You can then overlay a twoway scatter, and as options set the marker symbol to nothing (mysym(i)), and add the count within each value of your variable as the mlabel().

    If you're able to provide a snippet of your data I can probably put together some code. I wouldn't want to try without seeing your data though, as I could be sending you down a dead end! If you do share some data, please use dataex (ssc install dataex).

    Chris

    Comment


    • #3
      Chris,

      Thank you for the suggestion. It worked brilliantly! The core pieces of the code are below:

      twoway (bar EXP MONTH) (scatter EXP MONTH, m(i) mlabel(N) mlabposition(12))

      And the resulting graph, with some labels suppressed:



      Click image for larger version

Name:	824.png
Views:	1
Size:	7.3 KB
ID:	1372585

      Will

      Comment


      • #4
        See also tabplot (SJ). http://www.statalist.org/forums/foru...updated-on-ssc

        tabplot always puts the numbers underneath the bars. If you don't like that, no problem, as you know how to do it otherwise.

        Comment


        • #5
          Looks nice. I think you probably want to add bgcol(white) in addition to your graphregion(col(white)). It'll get rid of that faint blue line you have around the graph region - unless, of course, you want to keep it

          Comment

          Working...
          X