Announcement

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

  • Frequency labels in a horizontal histogram

    Hi,

    I want to draw a histogram that meets two requirements:
    1. displaying value labels readable
    2. displaying the frequency of each category.
    When a draw the histogram vertical, I don't have readable value labels. When I draw it horizontal the frequencies look very strange.

    Code:
        qui sum V3_2
        local mw = r(mean)
        hist V3_2, addlabels xline(`mw', lwidth(vthick)) frequ /*percent*/ discrete xlabel(1 (1) 5, valuelabel angle(horizontal)) /*
            */ color(none) lwidth(medthick) lcolor(black)  /*
            */ xtitle("Häufigkeit") ytitle("")
    
        hist V3_2, addlabels yline(`mw', lwidth(vthick)) frequ /*percent*/ discrete ylabel(1 (1) 5, valuelabel angle(horizontal)) /*
            */ color(none) lwidth(medthick) lcolor(black)  /*
            */ xtitle("Häufigkeit") horizontal ytitle("")
    Click image for larger version

Name:	V3_2_vertical.png
Views:	1
Size:	13.0 KB
ID:	1409685
    Click image for larger version

Name:	V3_2_horizontal.png
Views:	1
Size:	9.1 KB
ID:	1409684

    Does anyone know how to modify the addlabels so that they look nice in a horizontal histogram?
    Thanks in advance!

    Kind regards
    Sabine

  • #2
    Unfortunately I'm out of office during the next days. So please excuse me, if I don't respond immediately.

    Comment


    • #3
      Hi Sabine,

      I think the easiest way to do this would be to use -graph hbar- instead. Using the auto dataset, this would look something like:


      Code:
      sysuse auto, clear
      graph hbar (count), over(foreign) blabel(total)
      Where the variable foreign would be replaced with V3_2, in your case (which is a categorical variable, right?). You could then of course play with the other graph options to get at something closer to what you have above, style-wise.

      Does this go some ways towards solving your problem?

      Isaac

      Comment


      • #4
        Hi Isaac,

        Thanks for the hint. The frequency labels work but unfortunately I despair of the horizontal line xlines(1.846153846153846) not allowed, xaxis1 does not exist and to plot the whole scale (with blank space if no one chose this category): xlabels(1(1)4) not allowed, xaxis1 does not exist.

        Kind regards
        Sabine

        Comment


        • #5
          This may help.

          Code:
          sysuse auto, clear 
          set scheme s1color 
          
          preserve 
          
          label def rep78 1 abysmal 2 appalling  3 adequate 4 appealing 5 amazing 
          label val rep78 rep78 
          su rep78, meanonly 
          local mean = r(mean) 
          
          contract rep78, nomiss 
          
          twoway bar _freq rep78, horizontal yla(1/5, valuelabel ang(h) tlc(none))barw(0.8) bfcolor(none) /// 
          || scatter rep78 _freq, ms(none) mla(_freq) mlabpos(9) yli(`mean') ysc(titlegap(*5)) legend(off)  
          
          restore
          Click image for larger version

Name:	yabar.png
Views:	1
Size:	19.4 KB
ID:	1412384

          Comment


          • #6
            Or see tabplot (SSC; SJ 17(3) and so downloadable with Stata 15 updated to 25 Sept 2017).

            See e.g. https://www.statalist.org/forums/for...updated-on-ssc for an overview.
            Last edited by Nick Cox; 28 Sep 2017, 03:43.

            Comment


            • #7
              Hi Nick,

              tabplot is really good! I like it! Thanks.
              I have two small questions about it left:
              1. Is it possible to rename the "percent" on top?
              2. Is it possible to display percent and frequencies in the labels?
              Thanks,
              Sabine

              Comment


              • #8
                Glad you like it.

                Both questions are already answered in the help. Make sure that you get the updated files as released 25 September, but the questions were answered before the latest revision.

                #1 is a matter of the subtitle() option and just entails overwriting the default.

                #2 There is a reproducible example showing how to do exactly that. Search for "radiologist assessment data" in the help.

                Comment


                • #9
                  Hi Nick,

                  The more I know, the more I don't know ...
                  The help file says: showval specifies that numeric values be shown beneath (or if horizontal is specified, to the left of) bars. Unfortunately the bar label is (with frequencies and percents) too long for the left side. Is there an opportunity to rearrange the bar label?

                  Regards
                  Sabine

                  Comment


                  • #10
                    Please show example data and exact code. (FAQ Advice #12)

                    What does your longest label look like?

                    Comment


                    • #11
                      Code:
                      qui sum G1
                      local mw = r(mean)
                      qui inspect G1
                      local nm=r(N_pos)
                      cap drop show
                      bysort G1: gen show = string(_N) + " | " + string(_N*100/`nm', "%2.1f") + " %"
                      tabplot G1, horizontal showval(show) yline(`mw') ylabel(1 (1) 5) yasis xreverse percent  /*
                          */ ytitle("") color(none) lwidth(medthick) lcolor(black) subtitle("Häufigkeit und Prozent")
                      Attached Files

                      Comment


                      • #12
                        No example data there, but it allows a suggestion. You can get more space by using e.g.

                        Code:
                        xsc(r(0.8 .))
                        where 0.8 is the number to tune.

                        Comment


                        • #13
                          Perfect! Thanks a lot!

                          Comment

                          Working...
                          X