Announcement

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

  • Bivariate scatter plot with observations colored according to the clusters

    I performed K-Means clustering and am trying to graph two of my variables on a scatter plot, but need to color code them by their cluster. Can anyone tell me how to do this in Stata?

  • #2
    You don't seem to have any takers, so I'll give it a try: maybe something like the following.
    Code:
    version 15.1
    
    clear *
    
    set seed `=strreverse("1484344")'
    
    quietly sysuse auto
    
    cluster kmeans weight length, k(3) name(Sizes) generate(clusters) start(krandom)
    
    *
    * Begin here
    *
    local colors yellow blue red // Choose colors for the palette
    
    forvalues cluster = 1/3 {
        local color : word `cluster' of `colors'
        local command `command' (scatter weight length if cluster == `cluster', mcolor(`color') msize(small))
    }
    graph twoway `command', ylabel( , angle(horizontal) nogrid) legend(off)
    
    exit
    There could be better ways. You'll get better chances of someone's responding if you post in the General forum. This one's for Mata-related questions and many list members might not look here often.

    Comment

    Working...
    X