Announcement

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

  • Improving custom box plot

    Hi,

    Because I wanted to customize the properties of the boxplots, I abandoned using --graph box-- and tried the approach presented by Nick Cox in https://www.stata-journal.com/sjpdf....iclenum=gr0039

    For each of my 78 values of 'id', I want to graph a box plot for variable 'duration.' I want the figure to be -by(group)-.

    I adapted his code and used the following code:
    Code:
    egen p975 = pctile(duration), p(97.5) by(group)
    egen p025 = pctile(duration), p(2.5) by(group)
    
    
    twoway (rbar median upq id, pstyle(p1) bfc(gs15) blc(gs8) barw(0.35)) /*
    */ (rbar median loq id, pstyle(p1) bfc(gs15) blc(gs8) barw(0.35)) /*
    */ (rspike upq p975 id, pstyle(p1)) /*
    */(rspike loq p025 id, pstyle(p1)) /*
    */ (scatter duration id if !inrange(duration, p025, p975), msize(vsmall) ms(Oh) legend(off)), by(group, rows(1))
    This results in the following figure.
    Click image for larger version

Name:	test_customboxplot.png
Views:	1
Size:	89.2 KB
ID:	1463517



    Obviously, the legibility of this figure is limited. There are two things that I want to improve on:
    - In each subgraph, omit empty values of 'id' (x-axis)
    - In each subgraph, sort the boxes on the median value

    Unfortunately I am unable to provide a dataset as it is confidential data.

  • #2
    Dougie Jones
    It might be useful to explain what you are attempting to create as an end result.

    This part is self-promotion, but have you tried using brewscheme to manage the aesthetics of your graphs? Part of the problem I was trying to solve is what you are facing now where any one of several different aesthetic parameters get included when trying to construct a graph and then they are all there while you are trying to debug other aspects of the command to generate the end result you are looking for.

    To omit the cases where the value of id is missing you’ll need to add something like:

    Code:
    if !mi(id)
    to the different graph commands. The sorting part of it might be a bit more challenging, but one possible solution I can think of would be to sort the data by median values and create a new variable storing that order. Assign value labels to the sorted value variable using the ID variable (so the ID would be displayed on the x-axis). Then you should be able to create the graph for each group separately and can use graph combine to generate the example you provided above.

    Comment


    • #3
      Thank you wbuchanan
      using -!mi(id)- indeed removes the empty values of 'id', but it's now apparent that the spacing between boxes does not get adjusted once you do that.

      The example below is what I want to imitate. It was made with -graph box- but that command does not support custom specification of the box plot (and whiskers).

      Click image for larger version

Name:	test_customboxplot2.png
Views:	1
Size:	138.0 KB
ID:	1463542
      Last edited by Dougie Jones; 26 Sep 2018, 06:04.

      Comment

      Working...
      X