Announcement

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

  • How to show different colour for each observation in a scatter plot?

    Hi All,

    May I please ask how I can give each observation a different colour in a scatter plot?

    I have generated scatter plots for a numerical viable (W) change by years (Year) in various industries, respectively. As each dot represents W in an individual company, how the W of a particular company changes over the years can be detected if the W of each company shows a different colour in the scatter plot.

    I am not sure how it would work. May I please ask your advice? Thank you very much for your help.

    The code I am using right now is:
    scatter W Year if Industry==1

    Kind regards
    Yong

  • #2
    Yong, lines are better than scatters in illustrating the time trends of W for each firm. You may plot lines of W over year, firm by firm. BUT please rethink - the graph may not be what you want particularly when you have trends of many firms within one single graph.

    Comment


    • #3
      Fei, thank you very much for the suggestion. It may be a good idea to use the line. There are not many firms in each industry, so there are not many dots in each graph.

      By using the code:

      scatter W Year if Industry==1

      I am only able to see the dots in the same colour.

      May I please ask what code should I use to show the firms of each W in each year a different colour?

      If there are 8 dots (8 W of firms) for each year, do I need to write 8 colours in the code?


      I tried to use the code below (using separate), but it does not work.

      separate W, by (name) veryshortlabel
      scatter W year if industry==1, mcolor(blue bluishgray brown)


      It would be much appreciated if anyone would help me out.
      Last edited by Yong Zhang; 09 Jun 2022, 15:53.

      Comment


      • #4
        See https://www.statalist.org/forums/for...lable-from-ssc for more help.

        Comment


        • #5
          Hi Nick, Thank you very much. I have tried the code suggested in the thread you referred to. The problem I have now is that the number of dots/observations is reduced after using the separate command. There must be something wrong with the code I am using.

          Would you please let me know what would be possible the problem? Much appreciate your help!

          The original code:

          scatter w year if industry==1

          The updated code:

          separate w, by(id) veryshortlabel
          scatter w? year, if industry==1

          Here are what I got
          Click image for larger version

Name:	1.png
Views:	1
Size:	11.8 KB
ID:	1668717

          Click image for larger version

Name:	2.png
Views:	1
Size:	12.9 KB
ID:	1668718




          The two graphs should show the same number of dots/observations, with the second having coloured dots representing numerical variable (w) for different firms. But it is not. I tried to replace the variable (id) with the categorical variable (firm name), as I would like to see the change of w for each firm across 4 years. However, the same results were generated.

          The numerical variable (w) for each firm year may be different or the same (not many so) in my data. Is that because I have the same firm with multiple years of w causing the problem?

          I would be much appreciated it if anyone would help me fix the problem!
          Last edited by Yong Zhang; 10 Jun 2022, 14:47.

          Comment


          • #6
            There is no data example here, contrary to our longstanding request, One detail is that the wildcard w? may not catch all the variable names you want shown.

            The syntax

            Code:
            scatter w? year, if industry==1
            is illegal as an if qualifier is not an option.

            You aren't using sepscatter from SSC -- which is the point of the link in #4. You don't have to, but it would simplify your code.

            Comment


            • #7
              Thank you very much, Nick! The sepscatter command works!

              Comment

              Working...
              X