Announcement

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

  • Connect points from two scatterplots with lines by group variable

    Hello,
    I'm trying to connect the points between two separate scatterplots to show change between t1 and t2 across countries.
    The data example below shows a subset of cases with country, year, favorability score toward one superpower, favorability score toward second superpower:
    input int country double year float(favor_c1 favor_c2)
    40 2019 36.347603 42.74438
    40 2022 36.261555 48.29513
    41 2019 22.248415 25.96489
    41 2022 19.409565 41.10046
    42 2019 47.42112 43.82443
    42 2022 39.93524 52.72119
    43 2019 21.285133 31.4922
    43 2022 24.30714 34.981216
    44 2019 26.4181 20.78422
    44 2022 18.096584 4.002804
    45 2019 12.7282 -4.856491
    45 2022 24.727976 31.05799
    46 2019 -56.16412 -52.11881
    46 2022 -71.222824 -31.25629
    47 2019 -36.33735 -44.41511
    47 2022 -76.46397 -30.76732
    48 2019 -47.29562 26.31731
    48 2022 -29.733597 37.913708
    49 2019 34.979336 30.27762
    49 2022 47.82521 42.22549

    I'm partway there, but not sure how to connect each country's 2019 and 2022 dots with a line. Here is what I did so far:

    reshape wide favor_c1 favor_c2, i(country) j(year)

    scatter favor_PRC2019 favor_US2019, mc(green) m(X) || ///
    scatter favor_PRC2022 favor_US2022, mc(red) m(Dh) ///
    xtitle("Favorability score country 2") ytitle("Favorability score country 1") ///
    legend(label(1 "2019") label(2 "2022"))

    Greatly appreciate any tips and apologies in advance if I missed something in the documentation.
    Attached Files

  • #2
    Thanks for the interesting question.

    Your code chunks are not quite consistent. Please use CODE delimiters and post a .png attachment, not a .gph (FAQ Advice #12).

    I would avoid the default scheme s2color and try arrows here. See also https://www.stata-journal.com/articl...article=gr0015


    Code:
    clear 
    input int country double year float(favor_c1 favor_c2)
    40 2019 36.347603 42.74438
    40 2022 36.261555 48.29513
    41 2019 22.248415 25.96489
    41 2022 19.409565 41.10046
    42 2019 47.42112 43.82443
    42 2022 39.93524 52.72119
    43 2019 21.285133 31.4922
    43 2022 24.30714 34.981216
    44 2019 26.4181 20.78422
    44 2022 18.096584 4.002804
    45 2019 12.7282 -4.856491
    45 2022 24.727976 31.05799
    46 2019 -56.16412 -52.11881
    46 2022 -71.222824 -31.25629
    47 2019 -36.33735 -44.41511
    47 2022 -76.46397 -30.76732
    48 2019 -47.29562 26.31731
    48 2022 -29.733597 37.913708
    49 2019 34.979336 30.27762
    49 2022 47.82521 42.22549
    end 
    
    reshape wide favor_c1 favor_c2, i(country) j(year)
    
    set scheme s1color 
    
    twoway pcarrow favor_c12019 favor_c22019 favor_c12022 favor_c22022, lc(black) mc(black) ///
    xtitle("Favorability score country 2") ytitle("Favorability score country 1") ///
    xli(0, lc(gs12) lw(thin)) yli(0, lc(gs12) lw(thin))

    Click image for larger version

Name:	arrows.png
Views:	1
Size:	22.4 KB
ID:	1706371


    Comment


    • #3
      Nick,
      Thank you. First time posting. Next time, I will review the FAQ sheet more carefully to avoid unforced mistakes on my side.
      Code worked perfect. Thank you.

      Comment


      • #4
        There could be a 2 x 2 classification from both scores went down or stayed the same (here 1) to both scores went up (here 4). That may ease interpretation if you want to add country names to the display. In practice you should use the detail you have half-hidden in #1

        Code:
        gen which = 1 + (favor_c12022 > favor_c12019) + 2 * (favor_c22022 > favor_c22019)
        label def which 1 "both down" 4 "both up" 2 "c1 up" 3 "c2 up"
        label val which which 
        
        twoway pcarrow favor_c12019 favor_c22019 favor_c12022 favor_c22022, lc(black) mc(black) ///
        xtitle("Favorability score country 2") ytitle("Favorability score country 1") ///
        xli(0, lc(gs12) lw(thin)) yli(0, lc(gs12) lw(thin)) by(which)

        Comment


        • #5
          Hi Nick,
          Thanks - that is definitely a nice option. I'm still toying with best way to visualize this. Right now, still busy - but getting there. Thank you so much for the cool tips!

          twoway pcarrow favor_PRC2019 favor_US2019 favor_PRC2022 favor_US2022 if REG2_GLOBAL==1, lc(blue) mc(blue) msiz(.75) mlw(vthin) || ///
          pcarrow favor_PRC2019 favor_US2019 favor_PRC2022 favor_US2022 if REG2_GLOBAL==2, lc(gold) mc(gold) msiz(.75) mlw(vthin) || ///
          pcarrow favor_PRC2019 favor_US2019 favor_PRC2022 favor_US2022 if REG2_GLOBAL==3, lc(green) mc(green) msiz(.75) mlw(vthin) || ///
          pcarrow favor_PRC2019 favor_US2019 favor_PRC2022 favor_US2022 if REG2_GLOBAL==4, lc(gray) mc(gray) msiz(.75) mlw(vthin) || ///
          pcarrow favor_PRC2019 favor_US2019 favor_PRC2022 favor_US2022 if REG2_GLOBAL==5, lc(orange) mc(orange) msiz(.75) mlw(vthin) || ///
          pcarrow favor_PRC2019 favor_US2019 favor_PRC2022 favor_US2022 if REG2_GLOBAL==6, lc(red) mc(red) msiz(.75) mlw(vthin) ///
          xtitle("US Leadership Favorability Score", si(small)) ytitle("China Leadership Favorability Score", si(small)) ///
          xlab(, labs(small)) ylab(, labs(small)) ///
          legend(label(1 "Europe") label(2 "FSU") label(3 "Asia") label (4 "Americas") label (5 "MENA") label (6 "SSA") ///
          order (1 2 3 4 5 6) r(2) region(lp(blank)) si(small)) ///
          xli(0, lc(gs12) lw(thin)) yli(0, lc(gs12) lw(thin)) by(which, note("")) ///
          ysize(8) xsize(8)
          Click image for larger version

Name:	Shifting Views on Global Leaders.png
Views:	1
Size:	70.2 KB
ID:	1706729

          Comment


          • #6
            That's very nice but I have to advise against combining red and green in the same graph. as many people have difficulty distinguishing them. A Google on colour blindness (the most common term. but professionals use more precise terms for different conditions) should find advice on this. When this is mentioned most people emit some variant on "Doh! I did know about that really...".

            I have used a set of colours suggested by Okabe and Ito and there are other suggestions out there. One starting point is the mycolours package from SSC

            https://www.statalist.org/forums/for...ailable-on-ssc

            Comment

            Working...
            X