Announcement

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

  • Twoway scatter: supress/allow marker labels based on some conditon?

    Hi Stata users,

    I am interested in suppressing marker labels in scatter plots based on some condition because the labels become unreadable in clusters.

    Here is a replicable example:

    Code:
     sysuse auto, clear
    twoway scatter mpg price, mlabel(make)
    What I had hoped would work was:

    Code:
     twoway scatter mpg price, mlabel(make) if price >= 10000
    However, -if- is not allowed in the options for -scatter-.

    Another way I tried was

    Code:
     twoway scatter price mpg if price < 10000 , overlay addplot(twoway scatter price mpg if price >=  10000, mlabel(make)  )
    but the -overlay- option is not allowed.


    Any suggestions?
    Thank you!

  • #2
    Two solutions that work:


    Code:
     scatter price mpg if price < 10000 || scatter price mpg if price >=  10000, mlabel(make)  legend(off) 
    You will get different marker symbols and colours by default, which can be fixed if not desired.

    Code:
    gen make2 = make if price >= 10000
     scatter price mpg, mlabel(make2) 
    make2 is empty when the if condition is not satisfied. So there's always a marker label; it is just that some are empty strings.

    Comment


    • #3
      Fantastic. Thanks a lot, Nick!

      Comment

      Working...
      X