Announcement

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

  • How to label individual points on a scatterplot

    I'm comparing the change in Global Peace Index scores for 162 nations from 2010 - 2014. I know how to label every single country in the scatterplot, however, I want to just highlight the positions of 5 countries in 2010 and 14 whilst still keeping the other 157 points on the graph but unlabelled.

    Ben

  • #2

    Code:
    sysuse auto.dta
    tab1 make
    gen make2 = make if strpos(make,"Plym")
    scatter mpg weight , mlabel(make2)

    Comment


    • #3
      Thanks Svend, thats fantasic. There just one problem I hope you can help with. The GPI score is numerical, but I want to label those 5 points as SYR, IRQ, UK Ect.

      Comment


      • #4
        The GPI score is numerical, but that should not be a problem. If the country identifier is not a string variable, but is numerical:
        Code:
        gen country2 = "SYR" if country==17
        replace country2 = "IRQ" if country==23
        etc.
        scatter ... , mlabel(country2)

        Comment


        • #5
          In fact Svend's first post solved precisely this problem. He used a string variable to label points defined by two numeric variables.

          Comment


          • #6
            Thats fantasic. I'm slowly uncovering the problems as we work along it. I'm plotting a graph using GPI scores from 2014 and 2010, hence I want labels called SYR14 and SYR10, however because both sets of data are in the same row, when using the above technique you create a label for SYR14 but when you go to create one for SYR10 it just overwrites the SYR14 one. It's a pain you cannot use 2 labels when constructing a graph.

            Comment


            • #7
              I don't know the exact structure of your data, but I believe you need to -reshape long- so that SYR10 and SYR14 are not in the same observation (or row, as you call it).

              Comment


              • #8
                There is no rule that you can't have two or more marker labels.

                Code:
                 
                sysuse auto
                separate mpg , by(foreign) veryshortlabel
                su mpg
                l mpg foreign make if mpg == r(min)
                l mpg foreign make if mpg == r(max)
                gen pos = 12 in 26
                replace pos = 6 in 27
                scatter mpg0 mpg1 weight, mcolor(blue red) || scatter mpg0 weight if mpg==12, ms(none) mla(make) mlabcolor(blue) mlabvpos(pos)  || scatter mpg1 weight  if mpg==41, ms(none)  mla(make) mlabcolor(red) legend(off) ytitle("`: var label mpg'")
                .

                Comment


                • #9
                  One more quick question - I can't get to mlabel all of the countries. I tried different syntax forms (mlabel(country1, country2); mlabel(country1 country2); mlabel(country1) mlabel(country2); mlabel(country1), mlabel(country2)... etc)... None works! It seems to only label the last one.

                  Comment


                  • #10

                    Alberto: In post # 1 you wrote
                    I know how to label every single country in the scatterplot
                    Now you write that you cannot. How many country variables do you have? Hopefully one which you want to use for marker labels. mlabel() only takes a single variable name.

                    Comment


                    • #11
                      The question may be quick but it isn't clear. Do you want to label all countries but claim you can't? Or want to label some countries? You don't tell us anything about your data here except that you have countries. The arguments for mlabel() must be variable names. Nothing else.

                      Please see FAQ Advice, especially Section 12.

                      Comment


                      • #12
                        Svend, I wasn't the author of the post you quoted.
                        Sorry if I wasn't clear - I was referring to the first post, asking to label 5 of the countries. I made one example about how to label two countries after using the syntax you suggested.

                        I will try and state it again from the beginning:

                        I have a large dataset, I want to scatter two overall variables and label 5 different countries on the graph. I use the commands you gave to OP to generate new variables so that I can then mlabel them one by one, but none of the syntax that I tried (see post above) seem to work. My question is: once I have several countryX variables, what's the correct syntax for the mlabel command to show all 5 of these "countryX" variables but NOT the 162-observations "country" variable?

                        To be more specific, those are the commands that I used:

                        gen Kenya = Country if strpos(Country, "Kenya")
                        gen Syria = Country if strpos(Country, "Syria")
                        ...
                        scatter overall2014 overall2010, mlabel(Kenya Syria) (doesn't work!)
                        scatter overall2014 overall2010, mlabel(Kenya, Syria) (doesn't work!)
                        scatter overall2014 overall2010, mlabel(Kenya) mlabel(Syria) (only shows Syria!)
                        scatter overall2014 overall2010, mlabel(Kenya), mlabel(Syria) (doesn't work!)

                        I want the scatter to show both Kenya and Syria. (& three more countries, but I guess that would be the same for 2+)
                        Last edited by Alberto Camus; 29 Oct 2014, 14:58.

                        Comment


                        • #13
                          OK, I didn't look with enough care. But it also invites mistakes to hijack a sequence initiated by another person.

                          A variable is not a constant, on the contrary, it can take several values. I get the impression that you are creating a new variable for each country. Don't. Your attempts to generate a separate variable for each country are futile.

                          You have a string variable Country. You want to label Kenya and Syria, but not other countries. Make a variable country2:

                          Code:
                          generate Country2 = Country if Country == "Kenya" | if Country=="Syria"
                          scatter overall2014 overall2010 , mlabel(Country2)

                          Comment


                          • #14
                            In Svend's last post, the second if is a typo and should be removed.

                            Comment


                            • #15
                              Hello,
                              I have the same problem, and I implemented Svend's first solution. The labels are showing correctly, but to my concern the number of data points in the scatter has reduced. I'm posting my code and both the scatterplots here. My Y is digserv_exp, while X is gdp. The first plot has all the countries labelled, while the second plot only contains labels corresponding to the strings "India" and "China."

                              Code:
                              gen Country2 = Country if strpos(Country,"India") | strpos(Country,"China")
                              (540 missing values generated)


                              Code:
                              graph twoway (scatter digserv_exp gdp, mlabel(Country)) (lfit digserv_exp gdp) if Year==2019, ytitle("Digital Services Exports") legend(off)
                              Graph1_All Country Labels.gph


                              Code:
                              graph twoway (scatter digserv_exp gdp, mlabel(Country2)) (lfit digserv_exp gdp) if Year==2019, ytitle("Digital Services Exports") legend(off)
                              Graph2_India_China.gph


                              Could you tell me what's happening here? Why is the data shrinking?

                              Regards,
                              Saunok

                              Comment

                              Working...
                              X