Announcement

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

  • Color Individual Points on a Plot with the "By" Option

    Hello,

    I have a twoway scatter plot with confidence intervals that uses the "by" option and I'm trying to figure out a way to color individual points and confidence intervals (rather than having all points and CIs appear as the same color). When I tried separating the variables using the "separate" command I wasn't able to get the "by" option to work correctly. Any help would be much appreciated; thanks! Reproducible code and example data are below.


    Code:
    twoway ///
    (rcap low high identity, horizontal) ///
    (scatter identity amce), by(public)

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(amce low high) byte(identity public)
              .           .           .  1 .
              .           .           .  2 0
     -.09425743  -.13203828  -.05647658  3 0
     -.07900782  -.11693051  -.04108513  4 0
              .           .           .  5 0
              .           .           .  6 0
      .07890056   .04290051   .11490061  7 0
      .07140599   .03359305   .10921893  8 0
              .           .           .  9 0
              .           .           . 10 0
      .03607676 -.000187095  .072340615 11 0
     .069494694   .03252826   .10646113 12 0
              .           .           . 13 0
              .           .           . 14 0
     -.13493314   -.1737773  -.09608895 15 0
      .04488726  .007060205    .0827143 16 0
              .           .           . 17 0
              .           .           . 18 0
      .02542619 -.009594023    .0604464 19 0
      .05704184   .02103981   .09304386 20 0
              .           .           . 21 0
              .           .           . 22 0
    -.063884445  -.10065311 -.027115773 23 0
     -.07840127  -.11685856    -.039944 24 0
              .           .           . 25 0
              .           .           . 26 0
     -.09472065  -.13136864  -.05807268 27 0
     -.11449168   -.1506369  -.07834645 28 0
              .           .           .  1 1
              .           .           .  2 1
     -.07765516  -.10568018  -.04963013  3 1
     -.05895327  -.08518407 -.032722466  4 1
              .           .           .  5 1
              .           .           .  6 1
     .026449384 -.001082228     .053981  7 1
     .023676107 -.004215147   .05156736  8 1
              .           .           .  9 1
              .           .           . 10 1
      .07859392   .05142182   .10576602 11 1
      .05168327     .024314   .07905253 12 1
              .           .           . 13 1
              .           .           . 14 1
     -.03586419 -.063366406 -.008361971 15 1
     .026215894 -.001501661   .05393345 16 1
              .           .           . 17 1
              .           .           . 18 1
     .005194362  -.02181358  .032202303 19 1
     .036742404  .009951157   .06353366 20 1
              .           .           . 21 1
              .           .           . 22 1
     -.02104568  -.04707841  .004987055 23 1
     -.02960496   -.0565303 -.002679614 24 1
              .           .           . 25 1
              .           .           . 26 1
     -.06072497  -.08841986 -.033030078 27 1
     -.04712034   -.0741455  -.02009518 28 1
    end


  • #2
    Do you want every scatterpoint/confidence interval to be a different color? Or public = 0 to be one color and public = 1 to be another? What is your grouping mechanism?

    Comment


    • #3
      Thanks for the quick reply Ali! Basically I want identity = 1-4 to be one color, identity = 5-8 to be a different color, and so on.

      Comment


      • #4
        Here is one way:

        Code:
        ssc install palettes, replace
        ssc install colrspace, replace
        sum identity, meanonly
        local max = r(max)
        colorpalette s2
        local n = 1
        forv i = `max'(-4)1{
            local plot `plot' scatter identity amce if identity <=`i' & identity>`=`i'-4' , msym(circle) mcolor("`=r(p`n')'") || rcap low high identity if identity <=`i' & identity>`=`i'-4' ,horizontal lcolor("`=r(p`n')'")||
            local ++n
        }
        twoway `plot', by(public, legend(off))
        I use Ben Jann's colorpalettes package to generate a list of colors according to the default s2color scheme. You can change the scheme by switching out s2 for whatever scheme you prefer.

        Comment


        • #5
          Thanks so much Ali this works perfectly!

          Comment


          • #6
            See also sepscatter from SSC.

            https://www.statalist.org/forums/for...lable-from-ssc

            Comment

            Working...
            X