Announcement

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

  • Applying different colors to data points in scatter plot

    Hi! I am trying to create a graph like the one below where I can apply different colors to data points. The variable that I am using to assign different colors is a binary variable named "significance" . I want the zeros be red color and 1s blue. Could someone please help? Thank you very much in advance.
    Click image for larger version

Name:	stata.gif
Views:	1
Size:	22.0 KB
ID:	1570040

  • #2
    Code:
    // open example data
    sysuse auto, clear
    
    // create another indicator (dummy) variable
    gen good = (rep78 > 3) if rep78 < .
    label define good 0 "acceptable or bad" 1 "good"
    label value good good
    
    // method 1
    
    twoway scatter price weight if good == 0 || ///
           scatter price weight if good == 1    ///
           , by(foreign)
    
    // method 2
    separate price, by(good) veryshortlabel
    
    twoway scatter price0 price1 weight , by(foreign)
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      See also sepscatter from SSC, which is really just a wrapper for @Maarten Buis's method 2.

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

      Here are two ways of getting the same graph.

      Code:
      use https://stats.idre.ucla.edu/stat/stata/faq/hsb2, clear 
      set scheme s1color 
      
      scatter read write if schtyp==1, by(female, note("")) mc(red) ms(Oh) || scatter read write if schtyp==2, mc(blue) ms(+) legend(order(1 "public" 2 "private"))
      
      sepscatter read write, by(female, note("")) mc(red blue) ms(Oh + ) separate(schtyp)

      sepscatter ​​​​​​​applies separate temporarily.

      Comment


      • #4
        Amazing! Thank you so very much, Nick!

        Comment

        Working...
        X