Announcement

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

  • dot plot question

    Hello
    this is my data set
    Code:
    clear
    input year str4 quintile cpm
    1995 q1 28.2
    1995 q2 39
    1995 q3 47.1
    1995 q4 52
    1995 q5 57.4
    2000 q1 42.7
    2000 q2 50
    2000 q3 54.3
    2000 q4 58.3
    2000 q5 61.1
    2005 q1 50
    2005 q2 54.4
    2005 q3 57.2
    2005 q4 60
    2005 q5 59.6
    end
    i need to draw graph like this
    Click image for larger version

Name:	1.jpg
Views:	1
Size:	24.8 KB
ID:	1455507


    i used this code
    Code:
    graph dot cpm, over (quintile) over (year) asyvars  exclude0 yscale (range (25 65)) linetype (line) lines (lwidth (thin))
    but my problem is about extremes lines! i want to hide or eliminate extremes lines but i dont know how

  • #2
    My inclination would be to use twoway graphs as they tend to be more flexible. If you play with this code, you may be able to get closer to your preferred graph:

    Code:
    bysort year: egen left=min(cpm)
    bysort year: egen right=max(cpm)
    twoway dot year cpm, ndots(0) xlab(,grid) xscale(range(25 65)) xlab(25(5)65) yscale(reverse) || rcap left right year  , hor legend(off) yscale(reverse)
    Last edited by Carole J. Wilson; 28 Jul 2018, 13:19. Reason: added yscale(reverse) to both graphs
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      I appreciate for your advice Carole but how i can differentiate the circles with different colors?
      thank you.

      Comment


      • #4
        I've changed it up a bit, but try this:

        Code:
        bysort year: egen left=min(cpm)
        bysort year: egen right=max(cpm)
        twoway rcap left right year  , hor legend(off) lwidth(medium) lcolor(gs10) ///
            || scatter year cpm if quintile=="q1", msize(large)  ///
            || scatter year cpm if quintile=="q2", msize(large)  ///
            || scatter year cpm if quintile=="q3", msize(large)  ///
            || scatter year cpm if quintile=="q4", msize(large)  ///
            || scatter year cpm if quintile=="q5", msize(large)  ///
                xscale(range(25 65)) xlab(25(5)65) xlab(,grid) ylab(,angle(horizontal))
        Stata/MP 14.1 (64-bit x86-64)
        Revision 19 May 2016
        Win 8.1

        Comment


        • #5
          that's amazing carole. it's great. thank you so so so much.

          Comment


          • #6
            one more question carole, how i can add legend to introduce quintile circle colors?
            thank you

            Comment


            • #7
              Code:
              twoway rcap left right year  , hor  lwidth(medium) lcolor(gs10) ///
                  || scatter year cpm if quintile=="q1", msize(large)  ///
                  || scatter year cpm if quintile=="q2", msize(large)  ///
                  || scatter year cpm if quintile=="q3", msize(large)  ///
                  || scatter year cpm if quintile=="q4", msize(large)  ///
                  || scatter year cpm if quintile=="q5", msize(large)  ///
                      xscale(range(25 65)) xlab(25(5)65) xlab(,grid) ylab(,angle(horizontal)) ///
                      legend(order(2 "Quintile 1" 3 "Quintile 2" 4 "Quintile 3" ///
                      5 "Quintile 4" 6 "Quintile 5") rows(1))
              For more details and options for formatting the legend, see
              Code:
              help legend_options
              Stata/MP 14.1 (64-bit x86-64)
              Revision 19 May 2016
              Win 8.1

              Comment


              • #8
                I would not use a dot plot here. To distinguish quintiles the markers need to be shown differently. Different colours (or markers) can only be arbitrary and the arbitrariness requires a legend and the legend obliges "back and forth" from the reader. In contrast one of my mottos is "lose the legend if you can" (or "kill the key"). Consider this alternative:

                Code:
                clear
                input year str4 quintile cpm
                1995 q1 28.2
                1995 q2 39
                1995 q3 47.1
                1995 q4 52
                1995 q5 57.4
                2000 q1 42.7
                2000 q2 50
                2000 q3 54.3
                2000 q4 58.3
                2000 q5 61.1
                2005 q1 50
                2005 q2 54.4
                2005 q3 57.2
                2005 q4 60
                2005 q5 59.6
                end
                
                gen pos = cond(q == "q4", 2, 3) 
                set scheme s1color 
                local lc lc(black)
                line cpm year if quintile == "q1", `lc' || /// 
                line cpm year if quintile == "q2", `lc' || /// 
                line cpm year if quintile == "q3", `lc' || /// 
                line cpm year if quintile == "q4", `lc' || /// 
                line cpm year if quintile == "q5", `lc' || /// 
                scatter cpm year if year == 2005,    ///
                ms(none) mla(quintile) xsc(r(. 2005.5)) xtitle("") aspect(1) mlabvpos(pos) yla(, ang(h)) legend(off)
                Click image for larger version

Name:	cpm.png
Views:	1
Size:	25.4 KB
ID:	1455557

                Comment


                • #9
                  I like the way that the lines are labeled, Nick. I'll have to remember that technique.

                  Comment


                  • #10
                    Joseph: Thanks!

                    To spell it out, add marker labels to the last values (default clock position 3) and stretch the x axis accordingly. Sometimes, adding marker labels to the first values helps too: use a clock position of 9 in the first instance.

                    The idea is easy to understand, and certainly not original. The small Stata details are worth a short story and that is on my list for writing up in the Stata Journal in the near future.

                    Comment


                    • #11
                      thank you Ccarole! thank you Nick
                      also i add position (6) to legend to set legend below the graph.

                      Comment

                      Working...
                      X