Announcement

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

  • change bin size in twoway bar graph

    Hi, I am trying to plot a graph that conveys how extra worked hours (when people worked overtime and in a 2nd job if they do it) relate to basic usual hours worked main job (pooled data for a few years, but I am only interested in average hours for the whole time period). I managed to get the graph but I would like the X-axis to gross up the results in different bin sizes than 1 (say in 5hours bins for example).

    The code I did for this purpose was

    bysort basic_hours: egen avg_basic_hours_males=mean(basic_hours) if working==1 & basic_hours<70 & sex==1

    bysort avg_basic_hours_males: egen avg_extra_hours_males=mean(extra_hours) if working==1 & sex==1

    twoway (bar avg_extra_hours_males basic_hours) ///
    if working==1 & sex==1 & basic_hours<70 & ///
    avg_extra_hours_males>=0 & avg_extra_hours_males<=70, ///
    ytitle(Extra hours) ///
    xtitle(Basic hours) ///
    xlabel(0(5)70, labels) ///
    xmtick(0(1)70,) ///
    title(Male, size(small))

    Would I have to recode the basic_hours variable and then do everything again?

    Thanks for the help!

  • #2
    Yes, twoway bar just plots what you feed it. It won't do such calculations on the fly.

    Comment


    • #3
      Thanks Nick. As the hist command allows you to change bin size, is it possible doing this with twoway bar? I see the only thing is changing bwidth, but that is not what I intend to do.
      I guess it is probably worth it recoding basic_hours in 10 bin sizes and then run the code again, isn“t it?

      Comment


      • #4
        You can change barwidth using the barwidth() option, but that has no effect on what is shown otherwise.
        So, as implied, you need to recalculate for different bin widths.

        I don't have access to your dataset, but I see no great difficulty here.

        Code:
        sysuse auto, clear
        
        egen mean1 = mean(price), by(mpg)
        label var mean1 "Price USD in mpg bins, width 1"
        clonevar mpg_x = mpg
        replace mpg_x = mpg + 0.5
        twoway bar mean1 mpg_x, base(0) yla(0(2000)12000, ang(h)) name(g1, replace)
        
        gen mpg_2 = 2 * floor(mpg/2)
        egen mean2 = mean(price), by(mpg_2)
        label var mean2 "Price USD in mpg bins, width 2"
        replace mpg_x = mpg_2 + 1  
        twoway bar mean2 mpg_x, barw(2) base(0) yla(0(2000)12000, ang(h))  name(g2, replace)
        
        graph combine g1 g2 , col(1) xcommon


        Click image for larger version

Name:	binbar.png
Views:	3
Size:	14.5 KB
ID:	1326174

        Last edited by Nick Cox; 09 Feb 2016, 10:00.

        Comment


        • #5
          Thanks Nick, Why did you type the following lines?

          I guess you merely are introducing an assumption you made for these variables.

          replace mpg_x = mpg + 0.5
          ...

          gen mpg_2 = 2 * floor(mpg/2)

          ...

          replace mpg_x = mpg_2 + 1

          Comment


          • #6
            I define each bin by its lower limit using a floor() call but plot each bin centred on its midpoint. Thus add 0.5 for bins of width 1, 1 for bins of width 2, and so on.

            Comment


            • #7
              Floor function really useful to build n-size bins distriubtion

              Comment


              • #8
                See also http://www.stata-journal.com/sjpdf.h...iclenum=dm0002

                Comment

                Working...
                X