Announcement

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

  • Scatter plot: do I have to choose between (weighted) marker size and marker label ?

    Hello Stata experts,
    I would like to make a scatter plot with marker size like in https://www.stata.com/support/faqs/g...ers/index.html but I am not able to apply this command when I also want to label each marker.

    Example with :
    Code:
    clear
    input long w byte x float y str3 l
    110815 81      499 "AUS"
    147025 95      491 "AUS"
    118100 96      500 "BEL"
     96722 90 516.6667 "CAN"
     64935 83 437.6667 "CHI"
     32365 78 405.3333 "COL"
     68168 91 495.3333 "CZE"
    end
    Code:
    scatter y x [w=w]
    gives the expected outcome with markers growing with w.

    But if I try
    Code:
    scatter y x [w=w], mlabel(l)
    , I do have the labels as expected but I lose the varying marker size.

    Is there anything I can do to fix this? (Stata 16 for Mac OS X, by the way)

    Thank you for your help

  • #2
    Overlay a plot of the labels on the plot of the data points
    Code:
    scatter y x [w=w] ,  msymbol(circle_hollow) || scatter y x, m(none) mlabel(l)

    Comment


    • #3
      That could help but the risk is that the marker label will be poorly readable if written on top of the large markers....

      Comment


      • #4
        You can shift the position of the labels by defining a new x variable to be used in the second scatter plot.

        Code:
        tw scatter y x [w=w] ,  msymbol(circle_hollow) || scatter y x, m(none) mlabel(l) leg(off) saving(one)
        gen x2= x+0.4
        tw scatter y x [aw=w] ,  msymbol(circle_hollow) || scatter y x2, m(none) mlabel(l) leg(off) saving(two)
        gr combine one.gph two.gph

        Res.:
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	23.6 KB
ID:	1527744

        Last edited by Andrew Musau; 06 Dec 2019, 04:13.

        Comment


        • #5
          actually, there is no need to define a new variable; see
          Code:
          help scatter##marker_label_options
          and especially the write-up on mlabposition

          Comment


          • #6
            Rich Goldstein, what is needed here is a shift in the central position of the label. So unless I am missing something, I do believe that you need to define the variable. The option -mlabpos()- will not help much here, see below

            Code:
            *TOP OF THE HOUR
            tw scatter y x [w=w] ,  msymbol(circle_hollow) || scatter y x, m(none) mlabel(l) leg(off) saving(one) mlabpos(12)
            tw scatter y x [w=w] ,  msymbol(circle_hollow) || scatter y x, m(none) mlabel(l) leg(off) saving(two) mlabpos(3)
            *BOTTOM OF THE HOUR
            tw scatter y x [w=w] ,  msymbol(circle_hollow) || scatter y x, m(none) mlabel(l) leg(off) saving(three) mlabpos(6)
            tw scatter y x [w=w] ,  msymbol(circle_hollow) || scatter y x, m(none) mlabel(l) leg(off) saving(four) mlabpos(9)
            gr combine one.gph two.gph three.gph four.gph
            Click image for larger version

Name:	Graph.png
Views:	1
Size:	32.7 KB
ID:	1527761

            Comment

            Working...
            X