Announcement

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

  • How to overlay 2 linkplots (linked scatter plots for paired data) into 1 graph?

    Hi, I want to create a graph showing linked scatterplots between observations, linked by ID. I came across the command
    Code:
    linkplot
    created by Nick Cox. However, I want to step up the complexity of the graph by stratifying the linkplots by an observation, but I can't find a way to do that, neither with pairplots. Is that possible? How can I overlay the two linkplots into a graph like twoway graphs?

    Here's a sample dataset to what I'm doing:
    Code:
    sysuse bplong, clear
    linkplot bp when, link(patient)
    Click image for larger version

Name:	Graph.jpg
Views:	1
Size:	68.5 KB
ID:	1719481


    Now, I want to stratify the link plots by sex (male vs female), how can I do that? Is that possible?

    Thank you very much in advance
    Last edited by Gilbert Lazarus; 05 Jul 2023, 22:32.

  • #2
    Mentioning authors (thanks) is not nearly so important as mentioning locations of code (FAQ Advice #12). linkplot and pairplot are from SSC but both were mentioned in https://www.stata-journal.com/articl...article=gr0005

    It's best here just to go back to first principles.


    Code:
    sysuse bplong, clear
    
    line bp when, c(L) by(sex, note("")) xla(1 2, valuelabel) aspect(1) ytitle(Systolic blood pressure (mm Hg)) name(G1, replace)
    
    separate bp, by(sex) veryshortlabel 
    
    line bp? when, c(L) by(sex, note("") legend(off)) xla(1 2, valuelabel) aspect(1) ytitle(Systolic blood pressure (mm Hg)) name(G2, replace) 
    
    line bp when if sex == 1, c(L)  || line bp when if sex == 0, xla(1 2, valuelabel) ytitle(Systolic blood pressure (mm Hg)) legend(order(1 "female" 2 "male")) aspect(1) name(G3, replace)
    
    bysort patient (when) : gen bp_1 = bp[1]
    bysort patient (when) : gen bp_2 = bp[2]
    
    local opts aspect(1) xla(120(10)190) yla(120(10)190) xtitle(Before) ytitle(After) subtitle(Systolic blood pressure (mm Hg)) legend(order(2 "male" 3 "female")) 
    twoway function equality=x, ra(120 190) lc(stc3) || scatter bp_2 bp_1 if sex == 0 & when == 1, ms(Oh) mc(stc1) || scatter bp_2 bp_1 if sex == 1 & when == 1 ,  `opts' mc(stc2) ms(+) name(G4, replace)
    Of the graphs here, G2 is equivalent to G1 but more colourful; G3 is just a mess; and G4 is an alternative. as is plotting change versus mean.

    Click image for larger version

Name:	bp_G2.png
Views:	1
Size:	222.5 KB
ID:	1719493
    Click image for larger version

Name:	bp_G4.png
Views:	1
Size:	52.5 KB
ID:	1719494


    Comment


    • #3
      Hi Nick Cox Thank you very much for the insights! This is the sample dataset, and in the real dataset G3 makes much more sense as the data I have is very small. But in the real dataset, the variable is in days (since last dose) and not binary (before-after). I tried adapting your codes for the third graph but it doesn't work -- the lines are not linked by ID. This is my current dataset and code:



      Code:
      separate y, by(a) veryshortlabel
      line y x if a == 0, c(L) || line y x if a == 1, legend(order(1 "Subgroup A" 2 "Subgroup B")) aspect(1)
      I tried plotting the graph using this but it turns out like this:
      Click image for larger version

Name:	Screenshot 2023-07-06 183831.png
Views:	1
Size:	142.2 KB
ID:	1719511


      I'm not sure what went wrong though. Do you perhaps have any advice? Thanks

      Best,
      Gilbert

      Comment


      • #4
        Sorry, but I am travelling for a few days and won’t be able to answer queries like this, which may need a realistic data example in any case.

        Comment


        • #5
          I am back from my travels but still need a data example to make decent guesses on what you need.

          Comment

          Working...
          X