Announcement

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

  • Stripplot with 2 variables and grouping variable

    Hi,
    I hope you are all well and safe!

    I am plotting standardized outcome measures at admission and discharge from a rehabilitation stay grouped by age. Age is ordinal with 4 categories and the outcome measures are score_admission and score_discharge. I can generate a boxplot but would like something closer to what would could be generated with stripplot.

    This is the boxplot:


    I would like to use stripplot, but I only seem to be able to use one of the scores:

    This is the code for the stripplot:
    Code:
    stripplot score_admission, over(age) box blcolor(white) iqr  jitter(3) stack h(0.5) vertical mcolor(blue)
    Data in case it helps (sorry for the length - needed a few observations in each category):
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(age score_admission score_discharge)
    1 64  82
    1 50  80
    1 40  75
    1 69  87
    1 62 104
    1 53  85
    1 78 118
    1 31  76
    1 48  47
    1 59  96
    2 50  69
    2 40  46
    2 71 113
    2 83 117
    2 51  85
    2 60  99
    2 54  50
    2 84 117
    2 81 104
    2 50  49
    3 66  78
    3 34  55
    3 67  71
    3 59  76
    3 85 103
    3 91 112
    3 57  68
    3 49  60
    3 46  71
    3 53  83
    4 80  78
    4 61  58
    4 62  95
    4 52  63
    4 76 102
    4 71  97
    4 26  37
    4 48  58
    4 35  59
    4 83  75
    end
    label values age age
    label def age 1 "18-44", modify
    label def age 2 "45-64", modify
    label def age 3 "65-74", modify
    label def age 4 "75+", modify
    label var age "Age Category" 
    label var score_admission "Score at Admission" 
    label var score_discharge "Score at Discharge"
    Thanks!
    Marc

  • #2
    The immediate problem here is documented in the help for stripplot (which is from SSC, as you are asked to explain: https://www.statalist.org/forums/help).


    over(groupvar) specifies that values of varname are to be shown separately by groups defined by groupvar. This option may only be specified with a single variable.


    The solution is to use a by() option instead -- or better, I think, to restructure your data for this purpose so that your two variables become one. Here is complete code for something similar to what you tried. Note that to get back to your original data you should type restore.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(age score_admission score_discharge)
    1 64  82
    1 50  80
    1 40  75
    1 69  87
    1 62 104
    1 53  85
    1 78 118
    1 31  76
    1 48  47
    1 59  96
    2 50  69
    2 40  46
    2 71 113
    2 83 117
    2 51  85
    2 60  99
    2 54  50
    2 84 117
    2 81 104
    2 50  49
    3 66  78
    3 34  55
    3 67  71
    3 59  76
    3 85 103
    3 91 112
    3 57  68
    3 49  60
    3 46  71
    3 53  83
    4 80  78
    4 61  58
    4 62  95
    4 52  63
    4 76 102
    4 71  97
    4 26  37
    4 48  58
    4 35  59
    4 83  75
    end
    label values age age
    label def age 1 "18-44", modify
    label def age 2 "45-64", modify
    label def age 3 "65-74", modify
    label def age 4 "75+", modify
    label var age "Age Category"
    label var score_admission "Score at Admission"
    label var score_discharge "Score at Discharge"
    
    set scheme s1color
    
    stripplot score_*, by(age) box blcolor(white) iqr  jitter(3) stack h(0.5) vertical mcolor(blue) name(G1, replace)
    
    preserve
    
    gen long id = _n
    reshape long score_, i(id) j(event) string
    
    rename score_ score
    replace event = substr(event, 1, 3)
    stripplot score, over(event) by(age, compact note("") row(1)) vertical box(barw(0.2)) boffset(-0.1) iqr cumul cumprob height(0.5) yla(50(25)125, ang(h)) name(G2, replace)


    Click image for larger version

Name:	strip_box.png
Views:	1
Size:	37.7 KB
ID:	1557549



    Comment


    • #3
      That is perfect. Thank you!

      Comment

      Working...
      X