Announcement

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

  • Single line chart with a grouping variable

    How can I plot the following series in one plot, without specifying the grouping variable (i.e., province) one by one?
    Province year n
    A 2010 1
    A 2011 2
    A 2012 3
    B 2010 4
    B 2011 5
    B 2012 6
    Code:
    graph twoway line n year if province== "A" || line n year if province== "B", legend(label(1 "A ") label(2 "B"))
    Last edited by Hossein Jebeli; 24 May 2024, 10:40.

  • #2
    Here are two solutions. Naturally it's immediate and trivial that a single plot is likely to work well for two series.

    That is not at all obvious if the real number of provinces here is more like 20, or 200....

    Code:
    clear
    input str1 Province    year    n
    A    2010    1
    A    2011    2
    A    2012    3
    B    2010    4
    B    2011    5
    B    2012    6
    end 
    
    encode Province, gen(province)
    xtset province year 
    xtline n, overlay name(G1, replace)
    
    * sepscatter is from SSC 
    sepscatter n year, recast(line) separate(Province) name(G2, replace)

    Comment

    Working...
    X