Announcement

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

  • Multiple line plot of long data

    Hello all,

    I’m working with a dataset in the long format. My observations are grouped within ids (id) and weeks (week), the combination of id and week make an observation unique. I have a probabilities (prob) that I would like to plot as a line graph for different observations. This would be done stratified (separate graphs) for different groups (group).

    Please find below a mock dataset similar to what my real data looks like:

    Code:
    clear
    input long id str10 week float prob byte group
    1 "1/10/2020"   .14303502 2
    1 "1/3/2020"    .12902983 2
    1 "12/20/2019"  .04932104 2
    1 "12/27/2019"  .04299045 2
    2 "1/10/2020"    .1206926 1
    2 "1/3/2020"    .05439886 1
    2 "12/20/2019" .032277483 1
    2 "12/27/2019"  .05520606 1
    3 "1/10/2020"    .1378371 1
    3 "1/3/2020"     .1603722 1
    3 "12/20/2019"  .06625363 1
    3 "12/27/2019"   .1458157 1
    4 "1/10/2020"   .11175546 1
    4 "1/3/2020"     .0779216 1
    4 "12/20/2019"   .1137393 1
    4 "12/27/2019" .064635806 1
    5 "1/10/2020"   .09516551 1
    5 "1/3/2020"     .1502394 1
    5 "12/20/2019"  .12006584 1
    5 "12/27/2019"  .12520134 1
    end
    I’ve tried using linkplot – from SSC (sencode also came from SSC):

    Code:
    gen date = date(week, "MDY")
    sort date
    sencode week, gen(week2)
     
    linkplot prob week2 if group==1, link(id)
    But I wasn’t able to display different lines (ids) using different colors, nor to display a legend that says which line is which id (if lines had different colors, then this legend would display that the navy line is id 1, the maroon is id 2 and so on). Other than the color and legend, this is the kind of graph I have in mind - it displays the prob variation through time for each id - this is the essence of the graph I want.

    Am I overlooking something simple here? Does anyone have suggestions on how to proceed on this? I appreciate any tips or hints.

    Cheers

  • #2
    You could try -xtline-:

    Code:
    drop if group == 2
    xtset id week2
    xtline prob, overlay recast(connected)

    Comment


    • #3
      As expected, there was something simple. Thank you very much Scott!

      Comment

      Working...
      X