Announcement

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

  • overlapping labels on the same point

    Hi all,

    I saw many related problems to the following but, as far as I have understood, such issues are not quite thee same of mine.
    In particular, I have something like this:

    Code:
    levelsof newid
    twoway scatter trials_jan newid, xlabel(1(1)3, angle(90) valuelabel) xline(`r(levels)') mlabel(year) mlabposition("12") mlabangle(horizontal)
    I am trying to mlabel the scatter markers with year. The problem is that for a unique marker there can be many years, which overlap. What I would like to do is to avoid such overlapping maybe placing the years for the same marker one above the other.

    Than you,

    Federico

  • #2
    Code:
    by newid: gen label = string(year) if _n == 1
    by newid: replace label = label[_n-1] + " " + string(year) if _n > 1
    twoway scatter trials_jan newid, mlab(label) ...

    Comment


    • #3
      Andrew Musau Thank you very much! A last question. Do you know a way in which I can construct multiple overlaid scatter plots either within a loop or using locals?
      E.g. I would like to do something like
      Code:
      twoway (scatter a1 year) (scatter a2 year) (scatter a3 year) ...(scatter a50 year)
      where a1...a50 ae the values of a variable. The aim is to have a different color for each observation...
      tana you again
      Last edited by Federico Nutarelli; 17 Nov 2020, 06:39.

      Comment


      • #4
        Code:
        local wanted
        forval i= 1/50{
            local wanted "`wanted' (scatter a`i' year)"
        }
        di "`wanted'"
        Results in

        Code:
        . 
        . di "`wanted'"
         (scatter a1 year) (scatter a2 year) (scatter a3 year) (scatter a4 year) (scatter a5 year) (scatter a6 year) (scatter a7 year) (scatter a8 year) (scatter 
        > a9 year) (scatter a10 year) (scatter a11 year) (scatter a12 year) (scatter a13 year) (scatter a14 year) (scatter a15 year) (scatter a16 year) (scatter a
        > 17 year) (scatter a18 year) (scatter a19 year) (scatter a20 year) (scatter a21 year) (scatter a22 year) (scatter a23 year) (scatter a24 year) (scatter a
        > 25 year) (scatter a26 year) (scatter a27 year) (scatter a28 year) (scatter a29 year) (scatter a30 year) (scatter a31 year) (scatter a32 year) (scatter a
        > 33 year) (scatter a34 year) (scatter a35 year) (scatter a36 year) (scatter a37 year) (scatter a38 year) (scatter a39 year) (scatter a40 year) (scatter a
        > 41 year) (scatter a42 year) (scatter a43 year) (scatter a44 year) (scatter a45 year) (scatter a46 year) (scatter a47 year) (scatter a48 year) (scatter a
        > 49 year) (scatter a50 year)
        So you want

        Code:
        twoway `wanted'

        Comment


        • #5
          Oh I see. Thank you a lot!

          Comment


          • #6
            There is sepscatter from SSC and perhaps other programs which can do this, but the suggestion in #4 is not that hard to implement. Here is an example.

            Code:
            sysuse auto, clear
            keep in 1/20
            scatter mpg weight, scheme(s1color) saving(gr1.gph, replace)
            gen obsno=_n
            separate weight, by(obs)
            local levels
            foreach var of varlist weight*{
                if "`var'"!="weight"{
                    local levels "`levels' (scatter mpg `var')"
                }
            }
            tw `levels', scheme(s1color) leg(off) saving(gr2.gph, replace)
            graph combine gr1.gph gr2.gph, scheme(s1color)
            Click image for larger version

Name:	Graph.png
Views:	1
Size:	18.5 KB
ID:	1582315

            Last edited by Andrew Musau; 18 Nov 2020, 09:38.

            Comment


            • #7
              Forgive me for being sceptical but the punishment here may that you get what you ask and find it is not what you really want. You are not likely to get 50 different colours unless you specify them all separately. What good are 50 different colours without a legend and how could you show a legend with 50 different colours without severe pressure on real estate?

              Comment


              • #8
                Hi all,

                thank you for thee kind replies. Eventually I ended up using sepscatter. I know a plot with 50 colors might be a mess, but it was actually to my own reference. Indeed it all ended up with a scatter on collapsed variables. Thank you anyway Nick for the always very good advices. And thank you also Andrew!

                Comment

                Working...
                X