# st: AW: Problematic twoway lineplot with -by-

 From "Martin Weiss" To Subject st: AW: Problematic twoway lineplot with -by- Date Thu, 13 Aug 2009 15:09:19 +0200

```<>

*************
// Create dataset
clear*
set obs 40
gen id = int((_n-1)/10)+1
gen group = (id>=3) + 1
bysort id: gen time = _n
replace time = time -.5 if id  == 4
gen value = uniform()
bysort id (time): replace value = /*
*/ value[_n-1] + uniform() if _n > 1

separate value if group==1, by(id)

tw (line value1 time if group==1, sort) /*
*/ (line value time if group==2, sort), /*
*/ legend(off) nodraw

graph rename graph1

tw (line value2 time if group==1, sort) /*
*/ (line value time if group==2, sort), /*
*/ legend(off) nodraw

graph rename graph2

gr combine graph1 graph2,
rows(1)
*************

HTH
Martin

-----Ursprüngliche Nachricht-----
Von: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Steinar Fossedal
Gesendet: Donnerstag, 13. August 2009 14:56
An: statalist@hsphsun2.harvard.edu
Betreff: st: Problematic twoway lineplot with -by-

Hi, listers

I'm trying to create a twoway graph using two line graphs, and have it
split into groups using -by- _on_only_one_of_the_graphs_. I'm finding
this a bit tricky to pull off, and I've cooked up an example below
which illustrates the problem. My goal is to have graph 2 included on
each of the plots in graph 1, resulting in a graph with two plots
having two lines each. Instead, I get a graph with four plots in it,
and only a single line per plot.

Is there any way to produce the graph that I want without rearranging my data?

Hope you can help,
-Steinar

Example :

// Create dataset
clear
set obs 40
gen id = int((_n-1)/10)+1
gen group = (id>=3) + 1
bysort id: gen time = _n
replace time = time -.5 if id  == 4
gen value = uniform()
bysort id (time): replace value = value[_n-1] + uniform() if _n > 1

// Create plots
// Graph 1 (using by)
twoway (line value time if group==1, sort by(id))
graph rename graph1
// Graph 2 (not using by)
twoway (line value time if group==2, sort)
graph rename graph2
// Graph 3 (Attempt to merge the two graphs together, getting graph 2
on each of the plots in graph 1)
twoway (line value time if group==1, sort by(id)) (line value time if
group==2, sort)
graph rename graph3

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```