Announcement

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

  • Need to Highlight specific points on scatter plot, trying to create new variable

    Hello, beginner to STATA and coding in general. I have a dataset with countries. I created a new variable earlier to get another group of countries in that variable, then used mlabel so those countries could be seen on the graph. Now I am being ask for my homework to highlight three specific countries on the graph. However, unlike the other group of countries they don't have anything in common.

    So I have been trying multiple things such as

    Code:
    generate tri = country if country== "Brazil", "United States" & "China"
    
    replace tri= country if GINIindex = 42.06 | GINIindex= 41.12 | GINIindex= 52.67
    After I create the new variable my plan was to use mlabel and mlabcolor to create the graph I need.

    I've been at this for maybe three hours, gone through Youtube and pages on this site through Google but have not been able to figure it out. I don't even know if it's possible to add those thee specific countries into a variable. I don't know if I'm just missing a comma or something. I even tried destring, then add a number a value to the country but that also gave me an error.

    Any help is appreciated, thank you.

  • #2
    That is several things tried and several implied errors, and I won't address them all.

    Your question is often asked e.g. see this from last Friday https://www.statalist.org/forums/for...n-twoway-graph

    I'd start with the help for if.

    Code:
    help if
    That tells you

    Equality tests are performed by two equal signs (==), not one =; see operators.
    So, that is one of your errors. It also directs you to

    Code:
    help operators
    The two together tell you that & can used to combine logical conditions, not values. Also, and this is implicit, the comma is not a logical operator.

    You don't give a data example (FAQ Advice #12 https://www.statalist.org/forums/help#stata) but I would guess that you just need something broadly like


    Code:
    scatter y x, ms(Oh)  || scatter y x if inlist(country, "Brazil", "United States", "China"). mlabel(country)
    This should work too and is closer to what you were trying.

    Code:
    scatter y x, ms(Oh)  || scatter y x if country == "Brazil" | country == "United States"  | country == "China", mlabel(country)
    Note that the operator you need is | not &. That test is sensitive to different spelling in the data, including stray spaces.

    You don't need a new variable for the three countries, but it does no harm.

    EDIT Cross-posted at https://www.reddit.com/r/stata/comme...ecific_points/ Please note our policy on cross-posting, which is that you are asked to tell us about it.





    Last edited by Nick Cox; 21 Jan 2019, 02:23.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      That is several things tried and several implied errors, and I won't address them all.

      Your question is often asked e.g. see this from last Friday https://www.statalist.org/forums/for...n-twoway-graph

      I'd start with the help for if.

      Code:
      help if
      That tells you



      So, that is one of your errors. It also directs you to

      Code:
      help operators
      The two together tell you that & can used to combine logical conditions, not values. Also, and this is implicit, the comma is not a logical operator.

      You don't give a data example (FAQ Advice #12 https://www.statalist.org/forums/help#stata) but I would guess that you just need something broadly like


      Code:
      scatter y x, ms(Oh) || scatter y x if inlist(country, "Brazil", "United States", "China"). mlabel(country)
      This should work too and is closer to what you were trying.

      Code:
      scatter y x, ms(Oh) || scatter y x if country == "Brazil" | country == "United States" | country == "China", mlabel(country)
      Note that the operator you need is | not &. That test is sensitive to different spelling in the data, including stray spaces.

      You don't need a new variable for the three countries, but it does no harm.

      EDIT Cross-posted at https://www.reddit.com/r/stata/comme...ecific_points/ Please note our policy on cross-posting, which is that you are asked to tell us about it.




      Sorry for not reading the rules and giving as much information as possible. I was not aware of them, I made my account last night out of frustration, just hoping to find answer which is why I double posted. I felt it would increase the chances of someone responding and helping.

      These were some other commands that I was trying use to try and figure it out for myself.

      Code:
      list country if country == "United States" | "Brazil" | "China"
      
      generate tri= country=="United States", "Brazil" "China"
      
      generate tri = country if ("Brazil", "United States" & "China")
      
      replace tri = country if country== "United States" | "Brazil" | "China"
      The code you sent provided did work, so thank you very much. I definitely need to better understand operators and will work on that.

      Thank You once again

      Comment


      • #4
        The examples at the help for if and the linked manual section all show that you must specify the variable in each condition specified.

        Good that you solved your problem, but the Statalist home page and the prompt on each new posting both enjoin reading the FAQ Advice before writing anything.

        Next time you post, please first read all of https://www.statalist.org/forums/help

        Comment

        Working...
        X