Announcement

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

  • Placing bar labels in twoway histogram

    Dear Stata Community,
    I am having difficulty in my histogram graph to add labels to the height of the bar. I have two variables named "pre_math_score" and "post_math_score". I have created a histogram with these two variables and they overlap each other. My problem is when I want to put addlabels command, stata says that addlabel option is not allowed. Help me please, how to addlabels in each bar.

    Here is my code
    twoway (histogram math_score_pre, percent width(1) color(none) lcolor(black)) ///
    (histogram math_score_post, percent width(1) color(gs8) lcolor(black)), ///
    xtitle("Math Score") ///
    ylabel(#10) ///
    xlabel(1(1)10) ///
    legend(label(1 "Before Training Math Score") label(2 "After Training Math Score")) ///
    scheme(white_tableau)
    Last edited by Tush Jon; 30 Jan 2024, 09:46.

  • #2
    Nothing in your code shows an attempt to addlabel().

    You don't give a data example or show the graph but my guess is that code should work.

    Comment


    • #3
      Thanks Nick for your reply. Whenever I add the addlabels command, stata says that addlabels option is not allowed and no graph is viewed. Here is the code I run for addlabels:
      twoway (histogram math_score_pre, percent addlables width(1) color(none) lcolor(black)) ///
      (histogram math_score_post, percent addlabels width(1) color(gs8) lcolor(black)), ///
      xtitle("Math Score") ///
      ylabel(#10) ///
      xlabel(1(1)10) ///
      legend(label(1 "Before Training") label(2 "After Training")) ///
      scheme(white_tableau)
      The below graph is from the stata code I have mentioned above.
      Attached Files

      Comment


      • #4
        Clearly addlables is a typo. Otherwise a bare addlabels should work.

        Comment


        • #5
          I am extremely sorry for the typo. I have corrected the addlabels spelling, but stata still says option addlabels not allowed.
          This is the stata output result

          . twoway (histogram math_score_pre, percent addlabels width(1) color(none) lcolor(black)) ///
          > (histogram math_score_post, percent addlabels width(1) color(gs8) lcolor(black)), ///
          > xtitle("Math Score") ///
          > ylabel(#10) ///
          > xlabel(1(1)10) ///
          > legend(label(1 "Before Training") label(2 "After Training")) ///
          > scheme(white_tableau)
          option addlabels not allowed
          r(198);
          Last edited by Tush Jon; 30 Jan 2024, 11:09.

          Comment


          • #6
            addlabels is an option of histogram the command, not graph histogram the plot type.

            Comment


            • #7
              Thanks Jeff. Is there any command to add height labels in percentage to this histogram graph?

              Comment


              • #8
                Jeff Pitblado (StataCorp) is naturally right. Just take off the twoway and see if that fixes it.

                Comment


                • #9
                  Here is an example that shows how the histogram command generates the labeled bars using a second twoway histogram plot with options.
                  Code:
                  sysuse auto
                  
                  * command with -addlabels- option
                  histogram mpg, percent addlabels name(cmd)
                  
                  * basically translates to this
                  twoway  (histogram mpg, percent)        /// plot bars
                          (histogram mpg, percent         /// plot labels
                                  recastas(scatter)       /// bars -> points
                                  msymbol(none)           /// hide marker symbols
                                  mlabel(_height)         /// system variable containing bar heights
                                  mlabposition(12)        /// position labels above the point
                                  start(12)               /// supplied by histogram command
                                  bin(8)                  ///
                          )                               ///
                          ,                               ///
                          plotregion(margin(b=0))         /// make y=0 flush with x-axis
                          legend(nodraw)                  /// hide the legend
                          name(plots)

                  Comment

                  Working...
                  X