Announcement

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

  • Generate scatter plot with different colors for different groups

    I have a database with information from 358 observations from 10 different schools. There are 4 variables: student ID, school ID, student performance and student study hours.

    I generated a scatterplot between student performance and student study hours, using the command:

    twoway scatter performance hours

    However, I would like to plot a scatterplot between the variables performance and hours of study, but with a trend line for each of the schools and different colors for each point cloud for each school. Like this:


    Help me, please?

  • #2
    Your images don't show correctly in two ways of accessing this forum that I tried. Perhaps you posted in some file format other than .png (FAQ Advice #12).

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

    However, unless 10 schools differ a great deal, the result is likely just to be a confused mess. If so, a "front-and-back plot" may work as well or better:

    The thread https://www.statalist.org/forums/for...ailable-on-ssc offers several examples, but skim and skip through the thread knowing that subsetplot has been superseded by fabplot.

    https://journals.sagepub.com/doi/pdf...6867X211025838

    Comment


    • #3
      Here is some different technique. Fortuitously, but fortunately, the Grunfeld dataset has 10 companies.

      Code:
      webuse grunfeld, clear 
      
      gen ln_invest = ln(invest)
      
      tokenize "Oh Dh Th Sh X O D T S +"
      
      forval j = 1/10 { 
          local call `call' || scatter ln_invest year if company == `j', ms(``j'') pstyle(p`j') || lfit ln_invest year if company == `j', pstyle(p`j') 
      }
      
      twoway `call' legend(order(1 "1" 3 "2" 5 "3" 7 "4" 9 "5" 11 "6" 13 "7" 15 "8" 17 "9" 19 "10")) ytitle(ln invest) name(G1, replace)
      
      scatter ln_invest year || lfit ln_invest year, by(company) name(G2, replace)

      Comment

      Working...
      X