Announcement

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

  • Different colors on ascending and descending connect lines in scatter plot

    Dear All,

    Is it possible to color connecting lines in a scatter plot in different colors ?

    I plottet 566 sets of pre and post biomarker concentrations and connected sample sets with connect(l). I wish to color ascending lines red and descending lines black.


    Code:
    rename PreTBI_biomark01a sample1
    rename PreTBI_biomark02a sample2

    reshape long sample,i(cpr) j(time)
    sort cpr time


    set scheme s1mono
    scatter sample time, yscale(log) /
    msy(O) ///
    aspect(1) ///
    connect(l) ///
    xscale(range(0.8 2.2)) ///
    xlabel(1 "Prehospital" 2 "Inhospital") ///
    ytitle("S100B concentration, log scale") ///
    xtitle("") /* Legend for x-axis (none here) */

    I use STATA15. Thank you.

    Kind regards,
    Sophie Seidenfaden

  • #2
    See https://journals.sagepub.com/doi/pdf...867X0900900408 for a discussion that may be interesting or even useful.

    There is no data example in #1 (see https://www.statalist.org/forums/help#stata and in passing also https://www.statalist.org/forums/help#spelling) but this shows some technique.

    The data are a subset of weights for some patients with anorexia as discussed in the 2009 paper I just referenced. I reshaped them to long first.

    As it happens red for increasing values is wrong psychologically for this application but presumably right for yours.

    See also the separate command.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float id byte time int treatment float weight
     1 1 3  83.8
     1 2 3  95.2
     2 1 3  83.3
     2 2 3  94.3
     3 1 3    86
     3 2 3  91.5
     4 1 3  82.5
     4 2 3  91.9
     5 1 3  86.7
     5 2 3 100.3
     6 1 3  79.6
     6 2 3  76.7
     7 1 3  76.9
     7 2 3  76.8
     8 1 3  94.2
     8 2 3 101.6
     9 1 3  73.4
     9 2 3  94.9
    10 1 3  80.5
    10 2 3  75.2
    11 1 3  81.6
    11 2 3  77.8
    12 1 3  82.1
    12 2 3  95.5
    13 1 3  77.6
    13 2 3  90.7
    14 1 3  83.5
    14 2 3  92.5
    15 1 3  89.9
    15 2 3  93.8
    16 1 3    86
    16 2 3  91.7
    17 1 3  87.3
    17 2 3    98
    end
    label values treatment treatment
    label def treatment 3 "family", modify
    label var weight "weight (lb)"
    
    bysort id (time) : gen increase = weight[2] > weight[1]
    
    line weight time if increase, lc(red) || line weight time if !increase, lc(black) xla(1 2) legend(off) aspect(1.5) yla(, ang(h))


    Click image for larger version

Name:	anorexia.png
Views:	1
Size:	63.7 KB
ID:	1558176


    Comment

    Working...
    X