Announcement

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

  • Combining two graphs when one is not a two-way graph type

    Hi All, I'm helping a friend with a project and agreed (foolishly) to help her with some graphs. She wants to combine a bar graph with a dot plot for to show the individual data points and for the bar graph to show the mean for each group.

    Here is the data

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int pt double value str5 type str6 cell
    107 .39823008849557523 "blood" "CD69"  
    106                  . "blood" "CD69"  
    101  .3185840707964602 "blood" "CD69"  
     11  .7441860465116279 "blood" "CD69"  
    107  .9407407407407408 "graft" "CD69"  
    106                  . "graft" "CD69"  
    101  .6619718309859155 "graft" "CD69"  
     11  .7857142857142857 "graft" "CD69"  
    107  .5803571428571429 "blood" "CD45RA"
    106  .7317073170731707 "blood" "CD45RA"
    101  .5398230088495575 "blood" "CD45RA"
     11  .3488372093023256 "blood" "CD45RA"
    107 .08148148148148149 "graft" "CD45RA"
    106  .1111111111111111 "graft" "CD45RA"
    101  .4788732394366197 "graft" "CD45RA"
     11  .2857142857142857 "graft" "CD45RA"
    107 .36607142857142855 "blood" "CD45RO"
    106 .18699186991869918 "blood" "CD45RO"
    101 .23008849557522124 "blood" "CD45RO"
     11  .6046511627906976 "blood" "CD45RO"
    107                 .8 "graft" "CD45RO"
    106  .8395061728395061 "graft" "CD45RO"
    101 .22535211267605634 "graft" "CD45RO"
     11  .6190476190476191 "graft" "CD45RO"
    107 .03571428571428571 "blood" "CTLA4" 
    106                  0 "blood" "CTLA4" 
    101                  0 "blood" "CTLA4" 
     11                  0 "blood" "CTLA4" 
    107   .362962962962963 "graft" "CTLA4" 
    106 .06172839506172839 "graft" "CTLA4" 
    101 .07746478873239436 "graft" "CTLA4" 
     11 .14285714285714285 "graft" "CTLA4" 
    end
    To explain the context that was a small study where 4 patients (patient ids listed in variable "patient") where tested for various cell markers (listed in variable "cell") in samples from either blood or tissue grafts (listed in variable "type")

    Because I needed to create 8 distinct groups for her graph I used concat

    Code:
    egen group =concat(cell type)
    and then encoded the variable to numeric

    Code:
    rename group _group
    encode _group, gen(group)
    drop _group
    So she essentially wants a bar graph overplayed with a dot plot, but she also wanted every patient to have a unique marker so I opted to use "graph dotplot" rather than "graph dot" because with graph dot plot I can specify specific markers for each value of pt

    Essentially this:
    Code:
    label define symbol 107 "o" 106 "x" 101 "+" 11 "*"   
    label values pt symbol
    So I can generate the dot plot

    Code:
    dotplot value, over(group) m(none) mlabel(pt) xlabel( 1 `""CD45RA" "blood""' 2 `""CD45RA" "graft""'            3 `""CD45RO" "blood""'          4 `""CD45RO" "graft""'           5 `""CD69" "blood""'          6 `""CD69" "graft""'          7 `""CTLA4" "blood""'          8 `""CTLA4" "graft""') xtitle("")
    and generate the bar graph

    Code:
    graph bar (mean) value, over(group, relabel( 1 `""CD45RA" "blood""' 2 `""CD45RA" "graft""'            3 `""CD45RO" "blood""'          4 `""CD45RO" "graft""'           5 `""CD69" "blood""'          6 `""CD69" "graft""'          7 `""CTLA4" "blood""'          8 `""CTLA4" "graft""'))
    but I cannot overlay one on the other because dotplot is not a two-way graph type.

    Thus after my long introduction, my question is basically is there some way to overlay these graphs one on the other. Failing that, does anyway know how I can specify marker options for the regular graph dot such that every patient has unique marker. This doesn't seem to be an option for graph dot as it is for dotplot.

    Much thanks in advance. Using Stata v13

    Best,

    Chris


  • #2
    Thanks for the data example. Whatever you create using graph bar can be reproduced using twoway.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int pt double value str5 type str6 cell
    107 .39823008849557523 "blood" "CD69"  
    106                  . "blood" "CD69"  
    101  .3185840707964602 "blood" "CD69"  
     11  .7441860465116279 "blood" "CD69"  
    107  .9407407407407408 "graft" "CD69"  
    106                  . "graft" "CD69"  
    101  .6619718309859155 "graft" "CD69"  
     11  .7857142857142857 "graft" "CD69"  
    107  .5803571428571429 "blood" "CD45RA"
    106  .7317073170731707 "blood" "CD45RA"
    101  .5398230088495575 "blood" "CD45RA"
     11  .3488372093023256 "blood" "CD45RA"
    107 .08148148148148149 "graft" "CD45RA"
    106  .1111111111111111 "graft" "CD45RA"
    101  .4788732394366197 "graft" "CD45RA"
     11  .2857142857142857 "graft" "CD45RA"
    107 .36607142857142855 "blood" "CD45RO"
    106 .18699186991869918 "blood" "CD45RO"
    101 .23008849557522124 "blood" "CD45RO"
     11  .6046511627906976 "blood" "CD45RO"
    107                 .8 "graft" "CD45RO"
    106  .8395061728395061 "graft" "CD45RO"
    101 .22535211267605634 "graft" "CD45RO"
     11  .6190476190476191 "graft" "CD45RO"
    107 .03571428571428571 "blood" "CTLA4"
    106                  0 "blood" "CTLA4"
    101                  0 "blood" "CTLA4"
     11                  0 "blood" "CTLA4"
    107   .362962962962963 "graft" "CTLA4"
    106 .06172839506172839 "graft" "CTLA4"
    101 .07746478873239436 "graft" "CTLA4"
     11 .14285714285714285 "graft" "CTLA4"
    end
    
    egen gr =concat(cell type)
    encode gr, gen(group)
    drop gr
    bys group: egen mvg= mean(value)
    gen gr2= group-1
    egen id= group(pt)
    
    twoway (bar mvg gr2 , hor ylabel(, valuelabel angle(0)) xlab(0 (.2) 1) ylabel(0 `""CD45RA" "blood""' 1 ///
    `""CD45RA" "graft""' 2 `""CD45RO" "blood""'  3 `""CD45RO" "graft""' 4 `""CD69" "blood""' 5 `""CD69" "graft""' ///
    6 `""CTLA4" "blood""' 7 `""CTLA4" "graft""', labsize(small))  barw(0.9) xscale(axis(1) range(0)) bcolor(none) ///
    ytitle("") xtitle("Mean value") scheme(s1color)) (dot value gr2 if id==1, msymbol(X) msize(large) hor) ///
    (dot value gr2 if id==2, msymbol(O) hor) (dot value gr2 if id==3, msymbol(+) msize(large) hor) (dot value gr2 if ///
    id==4, msymbol(T)  hor legend(order(2 "Patient 1" 3 "Patient 2" 4 "Patient 3" 5 "Patient 4")))

    Result:
    Attached Files
    Last edited by Andrew Musau; 14 Feb 2019, 06:10.

    Comment


    • #3
      Your only interest in graph bar is to show means, which can be done in other ways (including by dotplot). Here is one take, using stripplot (SSC), in effect a functional superset of dotplot. You can separate() the different patients too.


      Code:
      
      stripplot value, by(cell, note("") row(1)) over(type) refline center cumul cumprob vertical ///
      xtitle("") xli(1.5, lc(gs12) lw(vthin)) yla(0 "0" 0.2(0.2)0.8 1 "1", format(%02.1f))
      Click image for larger version

Name:	blood.png
Views:	1
Size:	25.8 KB
ID:	1483650

      Comment


      • #4
        Indeed! Much obliged to you both. I'm so used to using graph bar, it was hard to take a step back and look at other possible graph types. I suppose there always are multiple other ways to skin the cat. Thanks for the solutions.

        Comment

        Working...
        X