Announcement

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

  • Underlaying histogram on marginsplot

    Hello Statalisters,

    I would like to underlay a histogram onto a marginsplot so conceptually it looks something like the attached.

    In an ideal world there are a few things I would like to change: 1) i'd like the yaxis for the histogram (currently the outermost yaxis) to be on the right hand side of the graph; 2) i'd like the xaxis to start at 1 and not 0; and 3) i'd like the bars to be much fainter and underlay (not overlay) the marginsplot.

    My code to produce this is


    Code:
    reg ct_proctmsec i.taker_consec_worked if taker_consec_worked<12
    matrix a = r(table)
    loc ylin = a[1,`= colsof(a)']
    margins, at(taker_consec_worked=(1 2 3 4 5 6 7 8 9 10 11))
    #delimit ;
    marginsplot, plotopts(mcol("0 174 239") msize(2) lcol("0 174 239")) ciopts(lcol("237 125 49")) graphregion(col(white)) 
                yline(`ylin', lcol(maroon)) xlabel(1 2 3 4 5 6 7 8 9 10 11, valuelabel labsize(small) angle(0.01)) 
                ylab(, angle(0)) ytitle("Seconds", size(medsmall)) bgcol(white) 
                xtitle("Number of consecutive days worked", size(medsmall)) 
                ysc(titlegap(*5)) xsc( titlegap(*10))
                title("Time between call receipt and queue", col(black) size(medsmall))
                addplot(hist taker_consec_worked if taker_consec_worked<=11, freq yaxis(2) 
                below start(1) width(1) legend(off))
                name(gph_26, replace)
                ;
    #delimit cr
    The below option for addplot seems to be what I need to make the histogram underlay and not overlay, but it is not having any effect. W.r.t. moving the yaxis to the r.h.s. and forcing the histogram to start at 1, my investigation has shown this doesn't seem possible using addplot.

    I tried using parmest (SCC) and something like this

    Code:
    reg ct_proctmsec i.taker_consec_worked if taker_consec_worked<12
    matrix a = r(table)
    loc ylin = a[1,`= colsof(a)']
    margins, at(taker_consec_worked=(1 2 3 4 5 6 7 8 9 10 11)) post
    
    preserve    
    parmest, norestore // SCC
    gen eqno = _n
    #delimit ;
    twoway (scatter estimate eqno, color("0 174 239") msize(3))
           (rcap min95 max95 eqno, col("237 125 49"))
           (bar N eqno, freq), //I don't have this N value yet... but would like it
            ;
    #delimit cr
    restore
    but cannot find an option on parmest to get N by groups of
    taker_consec_worked which I would need in order to add the twoway bar for frequency.

    Click image for larger version

Name:	gph_26.png
Views:	1
Size:	110.9 KB
ID:	1385264

  • #2
    Have you figured this out? Can you please try to add yscale(alt axis(2)) in the code?

    Code:
    addplot(hist taker_consec_worked if taker_consec_worked<=11, freq yaxis(2) yscale(alt axis(2)) below start(1) width(1) legend(off))

    Comment


    • #3
      You can use the marhis command available at SSC

      Code:
       
       ssc install marhis

      Comment


      • #4
        Yeah nice program Enrique. Thanks for putting it together (and sharing). I've taken the liberty of passing through standard twoway options and look forward to using it in the future. If interested in adding this feature to your next distribution to SSC, the syntax now looks like this:

        Code:
        syntax varlist(max=1),[*] [mar(varname)] [cate(varname)] [points(integer 15)] [percent] [discrete] [level(integer 95)] [label(integer 4)]  [summaryno] [confidenceno] [atmeans]
                        _get_gropts , graphopts(`options') gettwoway
                        local cmd = e(cmd)
        And then I add the `options' local to the end of every twoway call.

        Comment


        • #5
          Excellent suggestion Chris. I will certainly incorporate it to the next distribution.

          Comment


          • #6
            marhis looks useful, but I'm trying to do this with melogit -- so marhis won't work. BUT -- Yosuke's suggestion worked for me once I changed the fill color for the histogram bars to none. (The option to add is fcolor(none)). Then it doesn't really matter that the histogram is overlaid (instead of underlaid).

            Comment


            • #7
              Dear Enrique, It it possible for your interesting -marhis- command to be used after -xtreg- command?
              Ho-Chuan (River) Huang
              Stata 17.0, MP(4)

              Comment


              • #8
                Hi all,

                thank you for the awesome package, Enrique.

                Chris Larkin: May I ask you how exactly I can "pass through the standard twoway options" to adjust labels, ranges, etc.? I am fairly new to programming in Stata so I would highly appreciate it if you would be willing to share your code or point me to some concise hints on how to adapt a program like the marhis-package to enable more graph options.

                Thank you so much!

                Comment


                • #9
                  Hi all,

                  I think the previous two posts have been lost.

                  Chris Larkin: I have managed to adapt the syntax of the "marhis"-command the way you suggested. However, I still have some trouble generating a graph with adjusted labels, etc. when using the command: Stata now doesn't throw an error anymore but it still fails to adjust labels and ranges. May I thus ask you if you would be willing to share your code for generating such a graph?

                  If that is of any help, here is some example code:

                  Code:
                  sysuse auto.dta, clear
                  
                  local vars            length weight
                  
                  foreach X of varlist `vars' {
                      local labelX: var label `X'
                      local labelY: var label price
                      
                      reg price `X'
                      marhis `X', ///
                          percent ///
                          yscale(range(3000 16000)) ///
                          ylabel("labelY") ///
                          xlabel("labelX")
                      }
                  Thank you so much!

                  Comment

                  Working...
                  X