Announcement

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

  • How to correctly label "Bar chart with multiple bars graphed over *years*" using command: "graph bar"?

    I am trying to plot a bar chart with two bars graphed over consecutive years. While the following may compose a correct plot, however, the labeling are *wrong*. In brief, the goal is to add: x-axis label + the value of bars.

    Code:
    graph bar executions sentences , over(year, label(angle(forty_five) ))
    (On the structure of the data set: observations are yearly, with "executions" and "sentences" being the total number of such action in the year.)

    The difficulties I face are:

    1. The x-axis is populated with all the years! I tried to manipulate the value labels for the variable "year" but failed to enforce the desirable result. Labeling those years in between (like 1971-1974) as empty string (or " ", a space) does not help eliminate the label on x-axis.
    On the other hand, there is not an option through the graphical interface that stipulates how to make those awkward years-in-between vanish. The only available option is to "suppress" the x-axis label, which eliminates everything marking the x-axis.

    2. Label the value/magnitude of the one of the two bars. (I would like to put the exact number stored by variable "sentences" on top of the bar.)

    Any suggestion on how to solve either one of the above issue?

    Thank you in advanced

    All the best,

    -Linfeng

  • #2
    Switch to twoway bar or indeed line. Then it's easy to get the axis labels you want. The numbers can be added using marker labels.

    http://www.stata-journal.com/sjpdf.h...iclenum=gr0026 may also help.

    Here is a happier example to show technique:

    Code:
     
    webuse grunfeld
    keep if company == 10 
    gen year1 = year - 0.15
    gen year2 = year + 0.15
    gen mvalue2 = string(mvalue, "%4.0f")
    twoway bar invest year1, xla(1935(2)1954) barw(0.3) || bar mvalue year2 if company == 10, barw(0.3)     ///
    || scatter mvalue year2 if company == 10, mla(mvalue2) ms(none) mlabpos(12) scheme(s1color) legend(order(1 2)) ysc(r(0, 90))

    Comment


    • #3
      Thanks a lot! I am able to mute those unneeded years. However, is there a way to *enlarge* the distance between two year-categories? The nature of the data that I try to plot is that: num has more than 3 digits. This makes the display a little bit awkward. The attached pic shows what I am exactly having problem with.

      What follows is my attempt to make the font-size for marker-labels smaller. However, it would be more natural if year-bins could be separated with longer distance.

      Code:
      #delimit ;
      use Death_penalty.dta, clear ;
      gen year1 = year - 0.15;
      gen year2 = year + 0.15;
      gen sentences2 = string(sentences, "%4.0f");
      twoway
      bar sentences year1, xla(1975(4)2015) barw(0.6)  lcolor(gs4) fcolor(red*0.8)
      ||
      bar executions year2 , barw(0.5) lcolor(gs4) fcolor(blue*0.8)
      ||
      scatter sentences year2 ,
              mla(sentences2) mlabv(sentences2)
              ms(none) mlabpos(12)
              mlabsize(2)
      legend(order(1 2)) ysc(r(0, 90))
      ytitle("")
      ;
      #delimit cr
      Note that, adding option "mlabsize(2)" to the sub-scatter plot does the trick of shrinking font-size.

      Thank you again!

      All the best,

      -Linfeng

      Comment


      • #4
        Having bigger gaps between bars won't help your space problem for marker label text, but study of my example show that you just need to reduce 0.15 to something smaller.

        Comment


        • #5
          Originally posted by Nick Cox View Post
          Having bigger gaps between bars won't help your space problem for marker label text, but study of my example show that you just need to reduce 0.15 to something smaller.
          In reply to the quoted message:

          Reducing the "0.15" to any other things will only *shift* all bars horizontally in a uniform way. That is, the distance between two year-bins are not changed.

          The goal is to enlarge the gap between year-bins in a uniform fashion, so as to be enough for displaying 3-digit numbers. This shall be a more elegant approach compared to shrinking the font-size of marker-labels (those number sitting on top of the bars, which came in "physical" form as part of a scatter plot).

          Nick Cox: is the description of the practical need clear enough? How would you approach the "plotting challenge"?

          Thanks a lot! ^.^

          Comment


          • #6
            Sorry, but I don't understand what you are getting at. If the labels are too big relatively, then you need smaller marker label size. The gaps between bars are immaterial.

            Comment


            • #7
              You could also write the labels vertically by adding the option mlabangle(90) to the scatter command.

              Comment


              • #8
                Thank Nick Cox for pointing out that gaps between bars are immaterial. Thank Friedrich Huebler for providing the tilting option. Question solved, partially. It would still be great if the relative size of gaps between two year bins (I guess they are physical distance that is hard to alter) could be enlarged.

                Comment


                • #9
                  But it can, as already explained.

                  Code:
                  webuse grunfeld
                  keep if company == 10  
                  gen year1 = year - 0.05
                  gen year2 = year + 0.05
                  gen mvalue2 = string(mvalue, "%4.0f")
                  twoway bar invest year1, xla(1935(2)1954) barw(0.1) || bar mvalue year2 if company == 10, barw(0.1)     ///
                  || scatter mvalue year2 if company == 10, mla(mvalue2) ms(none) mlabpos(12) scheme(s1color) legend(order(1 2)) ysc(r(0, 90))

                  Comment

                  Working...
                  X