Announcement

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

  • overlap of bars using rbar

    Dears
    I am trying to place side by side box for the year 2016 and 2006. I was not able to use by with rbar and with the following command the two sets of information overlap. Is there a way to put the two boxes side by side? I copy the data I used and the code.

    Thank you in advance

    Code:
     twoway   rbar lqt med Reg, pstyle(p1) barw(.6) || ///
           rbar med uqt Reg, pstyle(p1) barw(.6) || ///
           rspike lqt ls Reg, pstyle(p1) || ///
           rspike uqt us Reg, pstyle(p1) || ///
           rcap ls ls Reg, msize(*6) pstyle(p1) || ///
           rcap us us Reg, msize(*6) pstyle(p1) || ///
           scatter highest Reg, msymbol(Oh) msize(*2)  || ///
        scatter lowest Reg, msymbol(Oh) msize(*2)   ///
           xlabel( 1 "A" 2 "B" 3 "C" 4 "D" 5 "E") ytitle(price)

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(Regions med lqt uqt ls us highest lowest year)
    1   28 27 30  22.5 34.5 55 10 2016
    2   32 30 35  22.5 42.5 65 24 2016
    3   30 29 33    23   39 48 10 2016
    4   27 26 30    20   36 57 19 2016
    5   30 20 45 -17.5 82.5 86  0 2016
    1   35 32 38    23   47 56 10 2006
    2 36.5 33 40  22.5 50.5 70 21 2006
    3   35 33 36  28.5 40.5 49 24 2006
    4   36 34 39  26.5 46.5 49 22 2006
    5   29 10 43 -39.5 92.5 90  0 2006
    end
    label values Regions reg
    label def reg 1 "A", modify
    label def reg 2 "B", modify
    label def reg 3 "C", modify
    label def reg 4 "D", modify
    label def reg 5 "E", modify

  • #2
    This is legal, but I can't tell whether it is what you want.


    Code:
    twoway   rbar lqt med Reg, pstyle(p1) barw(.6) || ///
           rbar med uqt Reg, pstyle(p1) barw(.6) || ///
           rspike lqt ls Reg, pstyle(p1) || ///
           rspike uqt us Reg, pstyle(p1) || ///
           rcap ls ls Reg, msize(*6) pstyle(p1) || ///
           rcap us us Reg, msize(*6) pstyle(p1) || ///
           scatter highest Reg, msymbol(Oh) msize(*2)  || ///
        scatter lowest Reg, msymbol(Oh) msize(*2)   ///
           xlabel( 1 "A" 2 "B" 3 "C" 4 "D" 5 "E") ytitle(price) by(year)
    That code looks horribly reminiscent of http://www.stata-journal.com/sjpdf.h...iclenum=gr0039 (corrected by http://www.stata-journal.com/sjpdf.h...lenum=gr0039_1), "horribly" for me because if I want box plots I now usually go for stripplot (SSC).

    With 5 regions and 2 years, I would

    1. Tend to put years side by side, not what the code above does.

    2. Tend to show more of the data.

    Here is a reproducible example:


    Code:
    clear
    set obs 100
    set seed 2803
    
    gen year = _n <= 50
    label def year 0 2006 1 2016
    label val year year
    
    egen seq = seq(), block(10) to(5)
    gen region = word("A B C D E", seq)
    
    gen outcome = rnormal()
    
    stripplot outcome, over(year) by(region, row(1) compact) centre vertical box
    Click image for larger version

Name:	region_year.png
Views:	1
Size:	35.5 KB
ID:	1419930

    Last edited by Nick Cox; 28 Nov 2017, 04:49.

    Comment


    • #3
      Dear Nick

      thank you very much for your suggestion this is what I needed.

      Notwithstanding I have another issue to solve. In my dataset I have two additional and separate variables that corresponds to the highest and lowest level in the year ( what I represent in the boxes are the value of a specific month). I would like to include these information as well in graph. I have tried the following but the dots are not in line with boxes. Is there a way to include these additional info?


      Code:
      stripplot myvar, over(year) by(Reg, row(1) compact) centre vertical box ///
      addplot( ///
      scatter highest_2016 Reg if year==1  || ///
      scatter lowest_2016  Reg if year==1 || ///
      scatter highest_2006 Reg if year==0 ||  ///
      scatter lowest_2006 Reg if year==0 ///
       )
      Thanks

      Federica

      Comment


      • #4


        If you look at my example, the over() variable defines the x axis.

        I guess you need therefore


        Code:
        stripplot myvar, over(year) by(Reg, row(1) compact) centre vertical box ///
        addplot(scatter highest* lowest* year)
        but without a data example I cannot be sure.

        Comment


        • #5
          I did a small change because in each year it appeared the two different dots (that is in the year 2006 I had both the lowest/higher of 2006 and 2016). Basically this is the code

          Code:
          stripplot myvar over(year) by(Reg, row(1) compact) centre vertical box  ///
           addplot(scatter highest_2006 lowest_2006 year if year==0 || ///
           scatter highest_2016 lowest_2016 year if year==1)
          Thank you very much for helping!!!

          Comment


          • #6
            Dears,

            I did a small change in the command above (post #5 ) but apparently I do something wrong considering that I get the following error message:
            Code:
            variable highest_2006 not found
            r(111);



            Code:
            stripplot myvar1  myvar2, by(Reg, row(1) compact) centre vertical box  ///
             addplot (scatter highest_2006 lowest_2006 Reg  || ///
             scatter highest_2016 lowest_2016 Reg )
            Can anyone tell me what the issue is with my current command?

            Thank you in advance

            Comment


            • #7
              Is it true? That is, do you have a variable highest_2016 as you need?

              Otherwise it would do no harm to close up

              Code:
              addplot (
              to

              Code:
              addplot(

              Comment


              • #8
                when I run the command
                Code:
                 
                 scatter highest_2006 lowest_2006 Reg
                it gives me the graph i need.
                Apparently it is not a matter of space.

                Comment


                • #9
                  OK. Looking again at

                  Code:
                   
                   stripplot myvar1  myvar2, by(Reg, row(1) compact) centre vertical box  ///  addplot(scatter highest_2006 lowest_2006 Reg  || ///  scatter highest_2016 lowest_2016 Reg )
                  it's not obvious to me what you're expecting. The stripplot is going to have an x axis that is devised for the purpose of showing myvar1 myvar2 distributions side by side -- and you also want that done separately by(Reg). So far, so good.

                  But you also want to superimpose scatter plots in the same space with Reg on the x axis.

                  I don't see that that would be illegal and don't see why you got that error message, but it is a puzzling specification.

                  Can you post a minimal data example that allows the error to be reproduced?

                  Comment

                  Working...
                  X