Announcement

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

  • sort using twoway line vs. twoway scatter, connected(L)

    Hi everyone,

    This may have a very simple solution that I'm missing, but I've gotten stuck.

    Basically, I want to replicate what I've done with twoway scatter with connected option with twoway line. (I have a good reason for this.) But I'm not getting the same results using the sort option between both. The two sets of code are:

    Code:
    twoway(scatter efvn timepoint, connect(L) lwidth(medium) lcolor(green%50) sort(study_id) ///
        ytitle(" ") xscale(titlegap(*10)) xtitle("Control Group") xlabel(0.8 " "  1 "Pre-Intervention" 1.5 "Post-Intervention" 1.7 " ", notick labsize(small) angle(0)))
    
    
    twoway(line efvn timepoint, lwidth(medium) lcolor(green%50) sort(study_id)  ///
        ytitle(" ") xscale(titlegap(*10)) xtitle("Control Group") xlabel(0.8 " "  1 "Pre-Intervention" 1.5 "Post-Intervention" 1.7 " ", notick labsize(small) angle(0)))
    And the two plots are below. I cannot figure out why the second plot (the line plot) has extra lines between points, given that the code is the same between the two, except one uses (scatter ... , connected(L)) and the other uses (line ..., ).

    Thanks in advance!
    John

    Click image for larger version

Name:	plot1.png
Views:	1
Size:	58.4 KB
ID:	1498103


    Click image for larger version

Name:	plot2.png
Views:	1
Size:	88.7 KB
ID:	1498104



  • #2
    Just realized I could get around this limitation by using scatter ..., connected(L) and specifying the markers to be invisible. But I'd still like to hear if anyone has an idea about why twoway (line...) is behaving this way.

    Code:
    twoway(scatter efvn timepoint, connect(L) msymbol(i) lwidth(medium) lcolor(green%50) sort(study_id) ///
        yscale(titlegap(*10)) ytitle(" ") xscale(titlegap(*10)) xtitle("Control Group") xlabel(0.8 " "  1 "Pre-Intervention" 1.5 "Post-Intervention" 1.7 " ", notick labsize(small) angle(0)))
    Click image for larger version

Name:	plot3.png
Views:	1
Size:	54.5 KB
ID:	1498106

    Comment


    • #3
      No data example here. But watch that sort(id) doesn't mess up your sort order. Perhaps sort(id timepoint) c(L) is what you need.

      See also https://journals.sagepub.com/doi/pdf...867X0900900408 for a wider discussion and further linkplot (SSC). On the latter see e.g.
      https://www.statalist.org/forums/for...using-linkplot
      Last edited by Nick Cox; 13 May 2019, 05:17.

      Comment


      • #4
        Thanks for the resources! I will take a look. As I quickly scan them right now, it looks like they will be very useful as I continue to create these types of plots in the future.

        Comment


        • #5
          Hello everyone! I am trying to generate a graph like the one posted above. I want to plot two variables (Var1 and Var2) that are quantitative variables paired pre and post intervention to see the change in each individual. I can't seem to get the graph like the one above. What syntax do I need?

          Comment


          • #6
            Hi Eva. The syntax I have above assumes your data are in long format. There is likely a way to create this same type of graph with wide format data. I did quite a bit of pre-processing of my data, but if you reshape your data to long format so you have two records for each individual and one variable ("var") that measures your outcome, you should be able to use similar syntax to what I used above.

            Welcome to Statalist!

            Comment

            Working...
            X