Announcement

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

  • Nested foreach loops to overlay two-way graphs

    Dear Statalisters,

    I am trying to plot a number of separate connected scatter plots and on top of these I am superimposing multiple horizontal lines. The code below works:

    Code:
    levelsof Assay2, local(levels)
    foreach var of local levels {  
        graph twoway scatter Mean Concentration if USBC=="S" & n1==1 & Assay2=="`var'", title("`var'") msymbol(X) connect(l) legend(off)|| ///
        line Mean Concentration if Dilution==25 & MucosalID==1 & Assay2=="`var'", lcolor(green)    || ///
        line Mean Concentration if Dilution==25 & MucosalID==2 & Assay2=="`var'", lcolor(green)    || ///
        line Mean Concentration if Dilution==25 & MucosalID==3 & Assay2=="`var'", lcolor(green)    || ///
        line Mean Concentration if Dilution==25 & MucosalID==4 & Assay2=="`var'", lcolor(green)    || ///
        line Mean Concentration if Dilution==25 & MucosalID==5 & Assay2=="`var'", lcolor(green)    || ///
        line Mean Concentration if Dilution==25 & MucosalID==6 & Assay2=="`var'", lcolor(green)    || ///
        line Mean Concentration if Dilution==25 & MucosalID==7 & Assay2=="`var'", lcolor(green)    || ///
        line Mean Concentration if Dilution==25 & MucosalID==8 & Assay2=="`var'", lcolor(green)    || ///
        line Mean Concentration if Dilution==25 & MucosalID==9 & Assay2=="`var'", lcolor(green)    || ///
        line Mean Concentration if Dilution==50 & MucosalID==1 & Assay2=="`var'", lcolor(blue)    || ///
        line Mean Concentration if Dilution==50 & MucosalID==2 & Assay2=="`var'", lcolor(blue)    || ///
        line Mean Concentration if Dilution==50 & MucosalID==3 & Assay2=="`var'", lcolor(blue)    || /// 
        line Mean Concentration if Dilution==50 & MucosalID==4 & Assay2=="`var'", lcolor(blue)    || ///
        line Mean Concentration if Dilution==50 & MucosalID==5 & Assay2=="`var'", lcolor(blue)    || ///
        line Mean Concentration if Dilution==50 & MucosalID==6 & Assay2=="`var'", lcolor(blue)    || ///
        line Mean Concentration if Dilution==50 & MucosalID==7 & Assay2=="`var'", lcolor(blue)    || ///
        line Mean Concentration if Dilution==50 & MucosalID==8 & Assay2=="`var'", lcolor(blue)    || ///
        line Mean Concentration if Dilution==50 & MucosalID==9 & Assay2=="`var'", lcolor(blue)    || ///
        line Mean Concentration if Dilution==100 & MucosalID==1 & Assay2=="`var'", lcolor(orange)    || ///
        line Mean Concentration if Dilution==100 & MucosalID==2 & Assay2=="`var'", lcolor(orange)    || ///
        line Mean Concentration if Dilution==100 & MucosalID==3 & Assay2=="`var'", lcolor(orange)    || ///
        line Mean Concentration if Dilution==100 & MucosalID==4 & Assay2=="`var'", lcolor(orange)    || ///
        line Mean Concentration if Dilution==100 & MucosalID==5 & Assay2=="`var'", lcolor(orange)    || ///
        line Mean Concentration if Dilution==100 & MucosalID==6 & Assay2=="`var'", lcolor(green)    || ///
        line Mean Concentration if Dilution==100 & MucosalID==7 & Assay2=="`var'", lcolor(orange)    || ///
        line Mean Concentration if Dilution==100 & MucosalID==8 & Assay2=="`var'", lcolor(orange)    || ///
        line Mean Concentration if Dilution==100 & MucosalID==9 & Assay2=="`var'", lcolor(orange)    || ///
        line Mean Concentration if Dilution==200 & MucosalID==1 & Assay2=="`var'", lcolor(red)    || ///
        line Mean Concentration if Dilution==200 & MucosalID==2 & Assay2=="`var'", lcolor(red)    || ///
        line Mean Concentration if Dilution==200 & MucosalID==3 & Assay2=="`var'", lcolor(red)    || ///
        line Mean Concentration if Dilution==200 & MucosalID==4 & Assay2=="`var'", lcolor(red)    || ///
        line Mean Concentration if Dilution==200 & MucosalID==5 & Assay2=="`var'", lcolor(red)    || ///
        line Mean Concentration if Dilution==200 & MucosalID==6 & Assay2=="`var'", lcolor(red)    || ///
        line Mean Concentration if Dilution==200 & MucosalID==7 & Assay2=="`var'", lcolor(red)    || ///
        line Mean Concentration if Dilution==200 & MucosalID==8 & Assay2=="`var'", lcolor(red)    || ///
        line Mean Concentration if Dilution==200 & MucosalID==9 & Assay2=="`var'", lcolor(red)
        graph export "/Users/rob/Documents/OVG/Lab work/SC_`var'.jpg", replace
    }
    However, I am clearly missing a trick in terms of nested foreach loops to run through Mucosal ID and Dilution. I could not figure out how to make these work in the context of a two-way plot.
    Additionally, is there a clever way to modify the colour per one of the foreach loops - or would this require a further nested foreach loop.

    Thanks in advance
    Best wishes
    Robert

  • #2
    I note that

    Code:
    line Mean Concentration if Dilution==100 & MucosalID==6 & Assay2=="`var'", lcolor(green)    || /// 
    breaks a simple pattern, but I suspect that is a typo. Otherwise this seems to boil down to something like

    Code:
    local ID inlist(MucosaID, 1, 2, 3, 4, 5, 6, 7, 8, 9)  
    
    levelsof Assay2, local(levels)  
    
    foreach var of local levels {      
        graph twoway scatter Mean Concentration if USBC=="S" & n1==1 & Assay2=="`var'", title("`var'") msymbol(X) connect(l) legend(off)|| ///  
        line Mean Concentration if Dilution==25 & `ID' & Assay2=="`var'", lcolor(green)    || ///    
        line Mean Concentration if Dilution==50 & `ID' & Assay2=="`var'", lcolor(blue)    || ///    
        line Mean Concentration if Dilution==100 & `ID' & Assay2=="`var'", lcolor(orange)    || ///    
        line Mean Concentration if Dilution==200 & `ID' & Assay2=="`var'", lcolor(red)        
        graph export "/Users/rob/Documents/OVG/Lab work/SC_`var'.jpg", replace
    }
    with a warning that you may need to add extra code to inhibit spurious connections between different identifiers.
    Last edited by Nick Cox; 08 Nov 2021, 12:53.

    Comment


    • #3
      Dear Nick,
      Many thanks for your rapid response - it is very appreciated. And thank you for picking up the typo. I have implemented the code you suggested and had a think about how it works, but the output does not replicate the previous code and I am not sure I understand how it works to modify it to change it to work (I am at least, a step closer to my desired outcome). The underlying desire is to produce a linked scatter graph of each of the (eg 10) assays and on each graph display all 9 samples at all 4 dilutions = 36 horizontal lines. This isn't so much for public data display, as much as to use as a quick range-finder for appropriate experimental planning.

      An example output (only one of the 10 graphs) from the original code:
      Click image for larger version

Name:	example Rob.png
Views:	1
Size:	294.6 KB
ID:	1635526

      An example output from the code you have kindly posted for me:
      Click image for larger version

Name:	example Nick.png
Views:	1
Size:	295.8 KB
ID:	1635527


      The code you have written (as you have hinted at, appears to have linked only to a subset of the lines).
      Any help you might provide in unpicking this would, again, be hugely appreciated
      Best wishes
      Rob

      Comment

      Working...
      X